You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Melody <47...@qq.com> on 2016/01/22 10:16:07 UTC

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

RE: CellStyle Help

Posted by "Murphy, Mark" <mu...@metalexmfg.com>.
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:475573071@qq.com] 
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: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: CellStyle Help

Posted by Javen O'Neal <ja...@gmail.com>.
See CellUtil
https://poi.apache.org/apidocs/org/apache/poi/ss/util/CellUtil.html
On Jan 22, 2016 1:25 AM, "Melody" <47...@qq.com> wrote:

> 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