You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Amir Khan <am...@deliveredsolutions.co.uk> on 2006/09/25 15:23:54 UTC
Problem with cell formatting.
Hi,
I've noticed that when I run the following method that I've written all
the formats for all the unsed cells are changed to the format I pass in.
Is there a way to only affect the format of the cell?, I might be doing
this wrong?. I thought i'm only affecting the one cell.
/**
* set a cell in the work book (in right sheet) as date.
* <p/>
*
* @param sheet the sheet we want to update
* @param date is the date you want to set
* @param x is the X co-ord
* @param y is the y co-ord
* @param mask is the formatting mask used
*/
public void setValueOfCellAsDateInSheet(int sheet,
Date date,
int x,
int y,
String mask) {
HSSFCell theCell = ensureCellExists(sheet, x, y);
HSSFCellStyle dateCellStyle = theCell.getCellStyle();
HSSFDataFormat format = workbook.createDataFormat();
dateCellStyle.setDataFormat(format.getFormat(mask));
theCell.setCellValue(date);
}
Here is the method I used in the above method (in case it matters) ...
/**
* Given a sheet, x and y this method will create all the parents
bits and
* the cell to ensure it exists in the workbook
*/
protected HSSFCell ensureCellExists(int sheet,
int x,
int y) {
HSSFSheet theSheet = workbook.getSheetAt(sheet);
HSSFRow theRow = theSheet.getRow(y);
if (null == theRow) {
theRow = theSheet.createRow(y);
}
HSSFCell theCell = theRow.getCell((short) x);
if (null == theCell) {
theCell = theRow.createCell((short) x);
}
return theCell;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
Mailing List: http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project: http://jakarta.apache.org/poi/
Re: Problem with cell formatting.
Posted by de...@yahoo.com.
> Is there a way to only affect the format of the
cell?
Use HSSFWorkbook.createCellStyle to create a new cell
style and set the data format and use that for the
target cell.
Cell Styles are shared (technically, the underlying
extendedformatrecord is shared). So changing the
dataformat of a cell style will affect all cells that
share the same cell style (in your case, all the
cells)
hth,
~ amol
--- Amir Khan <am...@deliveredsolutions.co.uk> wrote:
> Hi,
>
> I've noticed that when I run the following method
> that I've written all
> the formats for all the unsed cells are changed to
> the format I pass in.
> Is there a way to only affect the format of the
> cell?, I might be doing
> this wrong?. I thought i'm only affecting the one
> cell.
>
> /**
> * set a cell in the work book (in right sheet)
> as date.
> * <p/>
> *
> * @param sheet the sheet we want to update
> * @param date is the date you want to set
> * @param x is the X co-ord
> * @param y is the y co-ord
> * @param mask is the formatting mask used
> */
> public void setValueOfCellAsDateInSheet(int
> sheet,
> Date
> date,
> int x,
> int y,
> String
> mask) {
> HSSFCell theCell = ensureCellExists(sheet,
> x, y);
> HSSFCellStyle dateCellStyle =
> theCell.getCellStyle();
> HSSFDataFormat format =
> workbook.createDataFormat();
>
> dateCellStyle.setDataFormat(format.getFormat(mask));
> theCell.setCellValue(date);
> }
>
> Here is the method I used in the above method (in
> case it matters) ...
>
> /**
> * Given a sheet, x and y this method will
> create all the parents
> bits and
> * the cell to ensure it exists in the workbook
> */
> protected HSSFCell ensureCellExists(int sheet,
> int x,
> int y) {
> HSSFSheet theSheet =
> workbook.getSheetAt(sheet);
> HSSFRow theRow = theSheet.getRow(y);
> if (null == theRow) {
> theRow = theSheet.createRow(y);
> }
> HSSFCell theCell = theRow.getCell((short)
> x);
> if (null == theCell) {
> theCell = theRow.createCell((short) x);
> }
> return theCell;
> }
>
>
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> poi-user-unsubscribe@jakarta.apache.org
> Mailing List:
> http://jakarta.apache.org/site/mail2.html#poi
> The Apache Jakarta Poi Project:
> http://jakarta.apache.org/poi/
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
Mailing List: http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project: http://jakarta.apache.org/poi/