From 985bbe1696278e71dd38e37ba29d91207061abc1 Mon Sep 17 00:00:00 2001 From: Ugo Cei Date: Fri, 25 Jan 2008 12:34:33 +0000 Subject: [PATCH] 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 --- build.xml | 4 +-- .../poi/xssf/usermodel/XSSFWorkbook.java | 28 ++----------------- .../poi/xssf/usermodel/TestXSSFWorkbook.java | 16 +++++++++++ 3 files changed, 20 insertions(+), 28 deletions(-) diff --git a/build.xml b/build.xml index 2b38793a5..4e33434f4 100644 --- a/build.xml +++ b/build.xml @@ -145,8 +145,8 @@ under the License. - - + + diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index 508006cf6..1d92b6380 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -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); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java index d1ca3883e..11e7ad523 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java @@ -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(); + } }