<property name="ooxml.jar4.dir" location="${ooxml.lib}/jsr173_1.0_api.jar"/>
<property name="ooxml.jar4.url" value="${repository}/xmlbeans/jars/jsr173_1.0_api.jar"/>
<!-- No official release of openxml4j yet -->
- <property name="ooxml.jar5.dir" location="${ooxml.lib}/openxml4j-bin-alpha-080124.jar"/>
- <property name="ooxml.jar5.url" value="http://people.apache.org/~nick/openxml4j-bin-prealpha-071224.jar"/>
+ <property name="ooxml.jar5.dir" location="${ooxml.lib}/openxml4j-bin-alpha-080125.jar"/>
+ <property name="ooxml.jar5.url" value="http://people.apache.org/~ugo/openxml4j-bin-alpha-080125.jar"/>
<!-- See http://www.ecma-international.org/publications/standards/Ecma-376.htm -->
<!-- "Copy these file(s), free of charge" -->
package org.apache.poi.xssf.usermodel;
-import java.io.BufferedInputStream;
-import java.io.File;
-import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.LinkedList;
}
- /**
- * XXX: Horribly naive implementation based on OpenXML4J's Package class,
- * which sucks because it does not allow instantiation using an
- * OutputStream instead of a File. So we write the Package to a temporary
- * file, which we then proceed to read and stream out.
- */
public void write(OutputStream stream) throws IOException {
- // Create a temporary file
- File file = File.createTempFile("poi-", ".xlsx");
- file.delete();
try {
// Create a package referring the temp file.
- Package pkg = Package.create(file);
+ Package pkg = Package.create(stream);
// Main part
PackagePartName corePartName = PackagingURIHelper.createPartName("/xl/workbook.xml");
// Create main part relationship
workbook.save(out, xmlOptions);
out.close();
- for (int i = 1 ; i <= this.getNumberOfSheets() ; ++i) {
+ for (int i = 0 ; i < this.getNumberOfSheets() ; ++i) {
XSSFSheet sheet = (XSSFSheet) this.getSheetAt(i);
PackagePartName partName = PackagingURIHelper.createPartName("/xl/worksheets/sheet" + i + ".xml");
corePart.addRelationship(partName, TargetMode.INTERNAL, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet", "rSheet" + 1);
out = part.getOutputStream();
sheet.getWorksheet().save(out, xmlOptions);
- // XXX DEBUG
- System.err.println(sheet.getWorksheet().xmlText(xmlOptions));
out.close();
}
pkg.close();
- byte[] buf = new byte[8192];
- int nread = 0;
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
- try {
- while ((nread = bis.read(buf)) > 0) {
- stream.write(buf, 0, nread);
- }
- } finally {
- bis.close();
- }
} catch (InvalidFormatException e) {
// TODO: replace with more meaningful exception
throw new RuntimeException(e);
package org.apache.poi.xssf.usermodel;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+
import junit.framework.TestCase;
import org.apache.poi.ss.usermodel.Sheet;
workbook.removeSheetAt(0);
assertEquals(0, workbook.getNumberOfSheets());
}
+
+ public void testSave() throws Exception {
+ XSSFWorkbook workbook = new XSSFWorkbook();
+ Sheet sheet1 = workbook.createSheet("sheet1");
+ Sheet sheet2 = workbook.createSheet("sheet2");
+ Sheet sheet3 = workbook.createSheet("sheet3");
+ File file = File.createTempFile("poi-", ".xlsx");
+ System.out.println("Saving to " + file.getAbsolutePath());
+ OutputStream out = new FileOutputStream(file);
+ workbook.write(out);
+ out.close();
+ }
}