https://issues.apache.org/bugzilla/show_bug.cgi?id=51158
Bug #: 51158
Summary: Writing a workbook multiple times produces unreadable
content
Product: POI
Version: 3.8-dev
Platform: PC
OS/Version: Windows XP
Status: NEW
Severity: major
Priority: P2
Component: XSSF
AssignedTo: [email protected]
ReportedBy: [email protected]
Classification: Unclassified
Created attachment 26962
--> https://issues.apache.org/bugzilla/attachment.cgi?id=26962
"sheet1.xml" file of the Excel workbook "test2.xlsx"
When using the write() method of a XSSFWorkbook multiple times, the content of
the created Excel files is not readable by Excel. The reason for this problem
is that the content of some files (that are contained in the .xlsx zip archive)
is appended to the old content of those files.
Example code:
-----------------------------------------------------
// imports
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
// example code
FileOutputStream fos = null;
try {
// create a workbook
final XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Test Sheet");
XSSFRow row = sheet.createRow(2);
XSSFCell cell = row.createCell(3);
cell.setCellValue("test1");
// write the first excel file
fos = new FileOutputStream(new File("test1.xlsx"));
workbook.write(fos);
fos.flush();
fos.close();
// add a new cell to the sheet
cell = row.createCell(4);
cell.setCellValue("test2");
// write the second excel file
fos = new FileOutputStream(new File("test2.xlsx"));
workbook.write(fos);
fos.flush();
fos.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
-----------------------------------------------------
The content of the "sheet1.xml" file ("xl/worksheets/sheet1.xml") that has been
created in the first excel file is correct. In the second Excel file, an
incorrect "sheet1.xml" file is created. It includes the content of the first
"sheet1.xml" file and in addition appends the content that this file SHOULD
have. (The "sheet1.xml" file of the second Excel file of this example is
attached to this bug report.)
Further files that are affected in the same way in the posted example:
docProps/app.xml
xl/sharedStrings.xml
xl/styles.xml
xl/workbook.xml
I already searched for the cause of this bug but I could not find the source
code that is producing the appended XML code.
---
Used libraries:
poi-3.8-beta2-20110408.jar
poi-ooxml-3.8-beta2-20110408.jar
poi-ooxml-schemas-3.8-beta2-20110408.jar
Used external libraries:
dom4j-1.6.1.jar
stax-api-1.0.1.jar
xmlbeans-2.3.0.jar
--
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]