Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Open Source Projects
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Other Open Source Projects
APACHE POI - Not able to give background colour to a cell
rammie singh
Ranch Hand
Posts: 122
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
HI,
i m trying a piece of code to give background colour to a cell by using POI. But result is not getting reflected..meaning the bckground color is not produced.
Can any body please help me ..or tell me, where i am going wrong.
my piece of code is below
import java.io.FileInputStream; import java.io.FileOutputStream; import javax.swing.text.html.HTMLDocument.Iterator; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.CellRangeAddress; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Row; public class EditCarol { public static String outputFile = "C:/Users/prasad/Desktop/ExcelPOI_2011.xls"; @SuppressWarnings("deprecation") public static void main(String args[]) { try{ HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(outputFile)); HSSFSheet sheet = wb.getSheetAt(5); int total_row = 0; int index = 0; java.util.Iterator<Row> rowItr = sheet.iterator(); while(rowItr.hasNext()) { total_row += 1; HSSFRow row = (HSSFRow) rowItr.next(); index = row.getRowNum(); System.out.println("index ="+row.getRowNum()); } System.out.println("total row ="+total_row); HSSFCellStyle style = wb.createCellStyle(); style.setFillBackgroundColor(IndexedColors.BROWN.getIndex()); HSSFRow row = sheet.getRow(index +1); if(row == null) { row = sheet.createRow(index +1); HSSFCell cell = row.getCell(0); if (cell == null) cell = row.createCell(0); cell.setCellValue("New Cell"); //style.setFillBackgroundColor(HSSFColor.GREEN.index); style.setBorderBottom((short) 1); style.setBorderTop((short) 1); style.setBorderLeft((short) 1); style.setBorderRight((short) 1); cell.setCellStyle(style); } FileOutputStream fout = new FileOutputStream(outputFile); wb.write(fout); fout.flush(); fout.close(); System.out.println("file created"); } catch(Exception e) { System.out.println(e); } } }
rammie singh
Ranch Hand
Posts: 122
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Can anybody guide on this ..Please help.
Mark Ryan
Greenhorn
Posts: 1
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
My code below should help...............
private final static XSSFColor PRE = new XSSFColor(new java.awt.Color(255, 226, 101)); private final static XSSFColor LICENSE_COMPLETE = new XSSFColor(new java.awt.Color(217, 217, 217)); private final static XSSFColor NON_CONTRACTUAL = new XSSFColor(new java.awt.Color(230, 0, 0)); XSSFCell cell = (XSSFCell) row.getCell(i); XSSFCellStyle cellStyle = (XSSFCellStyle) cell.getCellStyle().clone(); cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); cellStyle.setFillForegroundColor(LICENSE_COMPLETE); cell.setCellStyle(cellStyle);
Pradeep Kuppusamy
Greenhorn
Posts: 1
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
If you still want to use HSSFCellStyle, then use setFillForeGroundColor(); That will do. Though I dont understand why they have BackGround fill
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Writing on excel file using POI
how to store a file in the local hard disk
facing diffculty to highlight background color of an excel cell which is not reflected in file
How can I analize Excel file with Apache POI?
How do i format a string to date
More...