I'm having similar problem, and despite checking the links you've provided I
haven't been able to copy cell styles from one workbook to another, but only
the content of the cell. I understood that this should copy the style, but
apparently it doesn't work:
cellNew.setCellStyle(cellOld.getCellStyle()) (in my copyCell() method)
Any advice would be appreciated.
private void copyFile (){
wbNew.createSheet(wbOld.getSheetName(0));
sheetOld = wbOld.getSheetAt(0);
sheetNew = wbNew.getSheetAt(0);
int rows = sheetOld.getLastRowNum();
for (int r = 0; r < rows; r++) {
rowOld = sheetOld.getRow (r);
rowNew = sheetNew.createRow(r);
rowNew.setHeight( rowOld.getHeight() );
if ( rowOld != null ) {
int cells = rowOld.getLastCellNum();
for (short c = 0; c < cells; c++){
cellOld = rowOld.getCell(c);
if ( cellOld != null ) {
cellNew =
rowNew.createCell(c);
copyCell(cellOld, cellNew);
cellNew.setCellType(cellOld.getCellType());
String content = checkType
(cellOld);
cellNew.setCellValue(content);
}
}
}
}
}
private void copyCell (HSSFCell cellOld, HSSFCell cellNew){
cellNew.setCellStyle(cellOld.getCellStyle());
cellNew.setEncoding(cellOld.getEncoding());
switch (cellOld.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
cellNew.setCellValue(cellOld.getStringCellValue());
break;
case HSSFCell.CELL_TYPE_NUMERIC:
cellNew.setCellValue(cellOld.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK:
cellNew.setCellValue(HSSFCell.CELL_TYPE_BLANK);
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
cellNew.setCellValue(cellOld.getBooleanCellValue());
break;
}
}
private String checkType (HSSFCell cell){
String value = null;
switch (cell.getCellType()){
case HSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
value = Double.toString(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK:
value = "";
break;
}
return value;
}
-Lenni
Gordon.Colburn wrote:
>
> I found one that looks promising - jXLS (http://jxls.sourceforge.net/).
>
> Thanks,
> Gordon
>
> -----Original Message-----
> From: Anthony Andrews [mailto:[EMAIL PROTECTED]
> Sent: Sunday, August 05, 2007 6:08 AM
> To: POI Users List
> Subject: Re: [[nosig]] How can I copy rows or cells and preserve
> formatting
>
> I think - and it is think so forgive me if I am mistaken - that someone
> has written a utility that will accomplish this sort of task. Have you
> searched through the archive of messages posted to the group yet? You
> may be able to find it.
>
> [EMAIL PROTECTED] wrote: I am using POI to generate
> reports from a Java application. For various
> reasons, I would like to be able to tweak the format of the report using
> excel rather than POI. My approach to this is to create a report
> template using excel. My application would then open the template and,
> for each row in the report, simply copy a row from the template onto the
> report, and then fill in the values. However, I have not found a way to
> copy a row or cell via POI in such a way that preserves formatting. Is
> it possible to do this with POI? The copying of rows or cells would be
> between worksheets but within the same workbook.
>
>
>
> Thanks in advance,
>
> Gordon
>
>
>
>
>
>
> ---------------------------------
> Got a little couch potato?
> Check out fun summer activities for kids.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
>
--
View this message in context:
http://www.nabble.com/--nosig---How-can-I-copy-rows-or-cells-and-preserve-formatting-tf4217899.html#a14128551
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]