Very close,
First create your workbook, then create your base styles that include your
fonts and basic styles. Then when you create a cell you apply the style you
want to it.
Workbook wb = WorkbookFactory.create(new File(""));
CellStyle cs1 = wb.createCellStyle();
CellStyle cs2 = wb.createCellStyle();
I usually have about a dozen of them: Title, SubTitle, Headers, left adjusted
text, centered text, right adjusted numbers (a style for each number format I
will be using), centered numbers, dates, and maybe a few others.
After creating a cell, I add the appropriate style:
Sheet s = wb.CreateSheet("Sheet1");
Row r = s.createRow(0);
Cell c = r.createCell(0);
c.setStyle(cs1);
Javen pointed you to CellUtil. There are some methods in there to help you
apply the same style to a bunch of cells. I usually do borders that way. You
can use CellUtil.setCellProperty, and coming soon setCellProperties so that you
don't have to set them one at a time (which adds a bunch of unused CellStyles
to your spreadsheet).
-----Original Message-----
From: Melody [mailto:[email protected]]
Sent: Friday, January 22, 2016 4:16 AM
To: user
Subject: CellStyle Help
Hi !
Thank you very much for creating such a wonderful project of Apache POI which
helps a lot. And I really appreciate it.
However, I have encountered a problem when using CellStyle.
At present, to create a CellStyle, the common way is:
Workbook wb = WorkbookFactory.create(new File("")); CellStyle cellStyle =
wb.createCellStyle(); // first create a Workbook then create a CellStyle
That means, all cells share the same CellStyle. So the problem is:
How can I set different CellStyle for different Cells ?
Say,
CellStyle cellStyle1;
CellStyle cellStyle2;
cell1.setCellStyle(cellStyle1);
cell2.setCellStyle(cellStyle2);
Very much appreciated for your reply.
Yours Sincerely
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]