]> source.dussan.org Git - poi.git/commitdiff
Use new version of OpenXML4J that allows saving straight to an OutputStream without...
authorUgo Cei <ugo@apache.org>
Fri, 25 Jan 2008 12:34:33 +0000 (12:34 +0000)
committerUgo Cei <ugo@apache.org>
Fri, 25 Jan 2008 12:34:33 +0000 (12:34 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@615206 13f79535-47bb-0310-9956-ffa450edef68

build.xml
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java

index 2b38793a545f035eb16e16fe0631ea97689c2810..4e33434f4f955c0a02bf374a32ff021f83741fbe 100644 (file)
--- a/build.xml
+++ b/build.xml
@@ -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" -->
index 508006cf6253276ccafaf3138044a60097acb5b6..1d92b6380054020fdf5c38d9e9bd8ae3f38db7ce 100644 (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);
index d1ca3883e1b48a2f3b4956794813b611ea93313b..11e7ad5237bbd2b41d03e2334b9faaf98cbe8a91 100644 (file)
 
 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();
+    }
 }