Excel objects to <cols/> in a sheet, so avoid outputting that

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@639766 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2008-03-21 19:14:43 +00:00
parent 695dc4812e
commit 22c794c1ba
3 changed files with 30 additions and 2 deletions

View File

@ -17,6 +17,8 @@
package org.apache.poi.xssf.usermodel;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
@ -34,7 +36,10 @@ import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.extensions.XSSFComments;
import org.apache.poi.xssf.usermodel.helpers.ColumnHelper;
import org.apache.poi.xssf.util.CellReference;
import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBreak;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDialogsheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
@ -97,6 +102,24 @@ public class XSSFSheet implements Sheet {
public XSSFWorkbook getWorkbook() {
return this.workbook;
}
/**
* Tweaks the CTWorksheet to fit with what Excel
* will accept without a massive huff, and write into
* the OutputStream supplied.
*/
protected void save(OutputStream out, XmlOptions xmlOptions) throws IOException {
// Excel objects to <cols/>
if(worksheet.getColsArray().length == 1) {
CTCols col = worksheet.getColsArray(0);
if(col.getColArray().length == 0) {
worksheet.setColsArray(null);
}
}
// Save
worksheet.save(out, xmlOptions);
}
protected CTWorksheet getWorksheet() {
return this.worksheet;

View File

@ -612,7 +612,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
// XXX This should not be needed, but apparently the setSaveOuter call above does not work in XMLBeans 2.2
xmlOptions.setSaveSyntheticDocumentElement(new QName(CTWorksheet.type.getName().getNamespaceURI(), "worksheet"));
out = part.getOutputStream();
sheet.getWorksheet().save(out, xmlOptions);
sheet.save(out, xmlOptions);
out.close();
// Update our internal reference for the package part

View File

@ -245,9 +245,14 @@ public class TestXSSFWorkbook extends TestCase {
// Load up again, check all still there
XSSFWorkbook wb2 = new XSSFWorkbook(tmpFile.toString());
assertEquals(3, wb2.getNumberOfSheets());
assertNotNull(wb2.getSheetAt(0));
assertNotNull(wb2.getSheetAt(1));
assertNotNull(wb2.getSheetAt(2));
// TODO - fix these!
assertEquals("dd/mm/yyyy", wb2.getSheetAt(0).getRow(1).getCell(0).getRichStringCellValue().getString());
assertEquals("yyyy/mm/dd", wb2.getSheetAt(0).getRow(2).getCell(0).getRichStringCellValue().getString());
assertEquals("yyyy-mm-dd", wb2.getSheetAt(0).getRow(3).getCell(0).getRichStringCellValue().getString());
assertEquals("yy/mm/dd", wb2.getSheetAt(0).getRow(4).getCell(0).getRichStringCellValue().getString());
assertNotNull(wb2.getSharedStringSource());
assertNotNull(wb2.getStylesSource());
}