Bug 55800 - cloneStyleFrom(CellStyle cellStyle) creates error "Unreadable content".
Summary: cloneStyleFrom(CellStyle cellStyle) creates error "Unreadable content".
Status: RESOLVED DUPLICATE of bug 58084
Alias: None
Product: POI
Classification: Unclassified
Component: XSSF (show other bugs)
Version: 3.9-FINAL
Hardware: PC All
: P2 major with 4 votes (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks: 58084
  Show dependency tree
 
Reported: 2013-11-20 04:29 UTC by Tien Duong
Modified: 2015-12-04 13:55 UTC (History)
2 users (show)



Attachments
Full java code of this copy. (1.36 KB, text/plain)
2013-11-20 04:29 UTC, Tien Duong
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Tien Duong 2013-11-20 04:29:30 UTC
Created attachment 31058 [details]
Full java code of this copy.

Source file: InStyleSheet.xlsx (existing)
Destination file: Output.xlsx (not existing yet)

Create new file Output.xlsx
Write data value to cell A1.
Clone CellStyle in cell A1 of InStyleSheet.xlsx to apply on A1 of Output.xlsx
This is done by using:
		Cell oldCell = inpWb.getSheetAt(0).getRow(0).getCell(0);
		CellStyle cellStyle = outWb.createCellStyle();
		cellStyle.cloneStyleFrom(oldCell.getCellStyle());
		newCell.setCellStyle(cellStyle);

Save Output.xlsx and exit.

Error: 
Java: no error.
Excel: Complains: Excel found unreadable content in "Output.xlsx". Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes.
Comment 1 Nick Burch 2013-11-20 11:59:34 UTC
Does this happen for all cell styles, or just this one?

Can you identify what about the cell style breaks it? Eg style in A1 clones fine, A2 is A1 plus XXXX, which then fails? That'll help narrow down what's going wrong
Comment 2 Tien Duong 2013-11-21 08:25:47 UTC
Hi. This problem happened for all cells in the sheet. 
I tried the sheet with few hundred cells data, all cells got formatting issue. 
In the code that I sent, I only checked 1 cell, it came with the same issue. 

Conclusion: Error happened for all cells.
Comment 3 Nick Burch 2013-11-21 09:21:11 UTC
All cells in that workbook, or all cells in all workbooks?

For example, if you create a new empty workbook, can you clone from that? What about if you start with an empty workbook, and begin styling a cell there. Does it fail immediately? When it almost looks like a cell in your problem file? When it exactly matches?
Comment 4 Tien Duong 2014-01-03 01:12:29 UTC
My test was:
- Create brand new One.xlsx file. Do formatting for cell A1 as follow:

cellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
myCell.setCellStyle(cellStyle);

- Save file. Close.

- Create brand new Two.xlsx file. Clone cellstyle from One.xlxs :
Cell oldCell = oneWorkbook.getSheetAt(0).getRow(0).getCell(0);
cellStyle2.cloneStyleFrom(oldCell.getCellStyle());
newCell.setCellStyle(cellStyle2);

- Save file. Close.

===> Error happened for Two.xlxs (Unreadable content.)
Comment 5 kiransringeri 2014-03-19 00:43:08 UTC
The reason is it won't copy the XSSFCellFill and XSSFCellBorder. This also gives problem with borders. I have added a method in org.apache.poi.xssf.model.StylesTable which will help in creating a copy of workbook. 

public void copyTo(StylesTable stylesTable){
	    stylesTable.numberFormats.clear();
	    stylesTable.fonts.clear();
	    stylesTable.fills.clear();
	    stylesTable.borders.clear();
	    stylesTable.styleXfs.clear();
	    stylesTable.xfs.clear();
	    stylesTable.dxfs.clear();
	    
	    for(String str : numberFormats.values())
	        stylesTable.putNumberFormat(str);
	    
	    for(XSSFFont font : fonts){
	        XSSFFont fontNew = new XSSFFont(font.getCTFont());
	        fontNew.registerTo(stylesTable);
	    }
	    for(XSSFCellFill fill : fills){
	        XSSFCellFill fillNew = new XSSFCellFill(fill.getCTFill());
	        stylesTable.putFill(fillNew);
	    }
	    for(XSSFCellBorder border : borders){
	        XSSFCellBorder borderNew = new XSSFCellBorder(border.getCTBorder());
	        stylesTable.putBorder(borderNew);
	    }
	    for(CTXf ctxf : styleXfs){
	        CTXf ctxfNew = (CTXf)ctxf.copy();
	        stylesTable.putCellStyleXf(ctxfNew);
	    }
	    for(CTXf ctxf : xfs){
	        CTXf ctxfNew = (CTXf)ctxf.copy();
	        stylesTable.putCellXf(ctxfNew);
	    }
	    for(CTDxf dxf : dxfs){
	        CTDxf dxfNew = (CTDxf)dxf.copy();
	        stylesTable.putDxf(dxfNew);
	    }
	}
Comment 6 j.zimmer 2014-11-17 07:30:49 UTC
Hello,

will there be a bugfix available in the near future?

Thanks in advance
Comment 7 Dominik Stadler 2015-03-18 19:06:44 UTC
There were two bugfixes done for bug 56295 which also could fix this, can you re-try this with a recent trunk-build, e.g. from https://builds.apache.org/job/POI/lastSuccessfulBuild/artifact/build/dist and report here if the problem still happens?
Comment 8 chinafool 2015-04-27 08:42:34 UTC
just as i say above.
you can do a simple demo to vertify my word.
Comment 9 Dominik Stadler 2015-09-14 18:03:26 UTC
This looks related to bug 58084 where similar corrupted files are reported after using cloneStyleFrom().
Comment 10 Dominik Stadler 2015-12-04 13:55:00 UTC
I believe bug 58084 will fix this one as well.

*** This bug has been marked as a duplicate of bug 58084 ***