Use new version of OpenXML4J that allows saving straight to an OutputStream without going through a file.

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@615206 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Ugo Cei 2008-01-25 12:34:33 +00:00
parent 86a0e470b9
commit 985bbe1696
3 changed files with 20 additions and 28 deletions

View File

@ -145,8 +145,8 @@ under the License.
<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" -->

View File

@ -17,9 +17,6 @@
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;
@ -364,20 +361,11 @@ public class XSSFWorkbook implements Workbook {
}
/**
* 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
@ -397,7 +385,7 @@ public class XSSFWorkbook implements Workbook {
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);
@ -409,23 +397,11 @@ public class XSSFWorkbook implements Workbook {
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);

View File

@ -17,6 +17,10 @@
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;
@ -114,4 +118,16 @@ public class TestXSSFWorkbook extends TestCase {
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();
}
}