]> source.dussan.org Git - poi.git/commitdiff
Get it to the point that existing .xslx files can be loaded+saved, and opened by...
authorNick Burch <nick@apache.org>
Fri, 21 Mar 2008 19:54:35 +0000 (19:54 +0000)
committerNick Burch <nick@apache.org>
Fri, 21 Mar 2008 19:54:35 +0000 (19:54 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@639779 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/model/SharedStringsTable.java
src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java
src/ooxml/java/org/apache/poi/xssf/model/XSSFModel.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCreationHelper.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataFormat.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java

index c6d8d2a862f63d318fe3aeabd4415b7bafaf731d..769c7c312b910a67cfcef065112986d2c3588bec 100644 (file)
@@ -22,6 +22,8 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.LinkedList;
 
+import javax.xml.namespace.QName;
+
 import org.apache.poi.ss.usermodel.SharedStringSource;
 import org.apache.xmlbeans.XmlException;
 import org.apache.xmlbeans.XmlOptions;
@@ -95,6 +97,11 @@ public class SharedStringsTable implements SharedStringSource, XSSFModel {
     public void writeTo(OutputStream out) throws IOException {
         XmlOptions options = new XmlOptions();
         options.setSaveOuter();
+        options.setUseDefaultNamespace();
+        
+        // Requests use of whitespace for easier reading
+        options.setSavePrettyPrint();
+
         SstDocument doc = SstDocument.Factory.newInstance(options);
         CTSst sst = doc.addNewSst();
         sst.setCount(strings.size());
@@ -102,6 +109,6 @@ public class SharedStringsTable implements SharedStringSource, XSSFModel {
         for (String s : strings) {
             sst.addNewSi().setT(s);
         }
-        doc.save(out);
+        doc.save(out, options);
     }
 }
index 3e8c0f54dfab6269a8ef30c8dbbf561f0af7ec25..0cb06197ef1630b731d4bfa6ced667dd771a87dd 100644 (file)
@@ -40,7 +40,9 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTNumFmt;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTNumFmts;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTStylesheet;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument;;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument;
+import javax.xml.namespace.QName;
+
 
 
 /**
@@ -92,21 +94,27 @@ public class StylesTable implements StylesSource, XSSFModel {
                doc = StyleSheetDocument.Factory.parse(is);
                
                // Grab all the different bits we care about
+               if(doc.getStyleSheet().getNumFmts() != null)
                for (CTNumFmt nfmt : doc.getStyleSheet().getNumFmts().getNumFmtArray()) {
                        numberFormats.put(nfmt.getNumFmtId(), nfmt.getFormatCode());
                }
+               if(doc.getStyleSheet().getFonts() != null)
                for (CTFont font : doc.getStyleSheet().getFonts().getFontArray()) {
                        fonts.add(font);
                }
+               if(doc.getStyleSheet().getFills() != null)
                for (CTFill fill : doc.getStyleSheet().getFills().getFillArray()) {
                        fills.add(fill);
                }
+               if(doc.getStyleSheet().getBorders() != null)
                for (CTBorder border : doc.getStyleSheet().getBorders().getBorderArray()) {
                        borders.add(border);
                }
+               if(doc.getStyleSheet().getCellStyleXfs() != null)
                for (CTXf xf : doc.getStyleSheet().getCellXfs().getXfArray()) {
                        xfs.add(xf);
                }
+               if(doc.getStyleSheet().getCellStyleXfs() != null)
                for (CTXf xf : doc.getStyleSheet().getCellStyleXfs().getXfArray()) {
                        styleXfs.add(xf);
                }
@@ -221,6 +229,15 @@ public class StylesTable implements StylesSource, XSSFModel {
     public void writeTo(OutputStream out) throws IOException {
         XmlOptions options = new XmlOptions();
         options.setSaveOuter();
+        options.setUseDefaultNamespace();
+        
+        // Requests use of whitespace for easier reading
+        options.setSavePrettyPrint();
+
+        // XXX This should not be needed, but apparently the setSaveOuter call above does not work in XMLBeans 2.2
+        options.setSaveSyntheticDocumentElement(
+                       new QName(CTStylesheet.type.getName().getNamespaceURI(), doc.getStyleSheet().getDomNode().getNodeName()));
+
         
         // Work on the current one
         // Need to do this, as we don't handle
@@ -254,6 +271,6 @@ public class StylesTable implements StylesSource, XSSFModel {
        // TODO
        
         // Save
-        doc.save(out);
+        doc.save(out, options);
     }
 }
index 7fe1083e76fe6f8e504c46e1a78070727b3b9243..eedf39117929285d448b266798c06111c4ffc7e6 100644 (file)
@@ -21,6 +21,8 @@ import java.io.InputStream;
 import java.io.OutputStream;
 
 public interface XSSFModel {
+       /** Read from the given InputStream */
        public void readFrom(InputStream is) throws IOException;
+       /** Write to the supplied OutputStream, with default options */
        public void writeTo(OutputStream out) throws IOException;
 }
index 7941a86610596914f8bd22b3acef211519efc12b..94eb5c1ec441c149086e4751276950143ffbb4b7 100644 (file)
@@ -27,7 +27,7 @@ public class XSSFCreationHelper implements CreationHelper {
                workbook = wb;
                
                // Create the things we only ever need one of
-               dataFormat = new XSSFDataFormat();
+               dataFormat = new XSSFDataFormat(workbook.getStylesSource());
        }
        
     /**
index 837b7ca5ef3d8dd85dc3ad4d1a55c10637ba0a2d..6b5191d8453a5576d61307ecd046357edfc32502 100644 (file)
 package org.apache.poi.xssf.usermodel;
 
 import org.apache.poi.ss.usermodel.DataFormat;
+import org.apache.poi.ss.usermodel.StylesSource;
 
 /**
- * TODO - figure out how this should really work for XSSF
+ * Handles data formats for XSSF.
+ * TODO Figure out if there are build in formats too 
  */
 public class XSSFDataFormat implements DataFormat {
+       private StylesSource stylesSource;
+       public XSSFDataFormat(StylesSource stylesSource) {
+               this.stylesSource = stylesSource;
+       }
+       
        public short getFormat(String format) {
-               return -1;
+               return (short)stylesSource.putNumberFormat(format);
        }
 
        public String getFormat(short index) {
-               return null;
+               return stylesSource.getNumberFormatAt((long)index);
        }
 }
index d4ef042f02b1b46902279582c7e85d5978b06bb9..71410a0861d763c231cd53e4d1e0222d052abd4e 100644 (file)
@@ -165,6 +165,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
             PackageRelationship rel =
                corePart.addRelationship(ppName, TargetMode.INTERNAL, REL);
             PackagePart part = corePart.getPackage().createPart(ppName, TYPE);
+            
             OutputStream out = part.getOutputStream();
             model.writeTo(out);
             out.close();
@@ -188,6 +189,10 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
         CTBookView bv = bvs.addNewWorkbookView();
         bv.setActiveTab(0);
         this.workbook.addNewSheets();
+        
+        // We always require styles and shared strings
+        sharedStringSource = new SharedStringsTable();
+        stylesSource = new StylesTable();
     }
     
     public XSSFWorkbook(String path) throws IOException {
@@ -203,11 +208,16 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
                    // Load shared strings
                    this.sharedStringSource = (SharedStringSource)
                        SHARED_STRINGS.load(getCorePart());
+            } catch(Exception e) {
+               throw new IOException("Unable to load shared strings - " + e.toString());
+            }
+            try {
                    // Load styles source
                    this.stylesSource = (StylesSource)
                        STYLES.load(getCorePart());
             } catch(Exception e) {
-               throw new IOException(e.getMessage());
+               e.printStackTrace();
+               throw new IOException("Unable to load styles - " + e.toString());
             }
             
             // Load individual sheets
@@ -617,6 +627,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
                  
                 // Update our internal reference for the package part
                 workbook.getSheets().getSheetArray(i).setId(rel.getId());
+                workbook.getSheets().getSheetArray(i).setSheetId(sheetNumber);
             }
              
             // Write shared strings and styles
index ca7dbb37361594b1f3c1c97d897998779c422765..8f52d618d0fc421835aea0ebbf0dbd03c9e0ec6e 100644 (file)
@@ -23,6 +23,7 @@ import java.io.OutputStream;
 
 import junit.framework.TestCase;
 
+import org.apache.poi.ss.usermodel.RichTextString;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.StylesSource;
 import org.apache.poi.xssf.model.StylesTable;
@@ -158,8 +159,10 @@ public class TestXSSFWorkbook extends TestCase {
         Sheet sheet2 = workbook.createSheet("sheet2");
         Sheet sheet3 = workbook.createSheet("sheet3");
         
-        sheet1.createRow(0);
-        sheet1.createRow(1);
+        RichTextString rts = workbook.getCreationHelper().createRichTextString("hello world");
+        
+        sheet1.createRow(0).createCell((short)0).setCellValue(1.2);
+        sheet1.createRow(1).createCell((short)0).setCellValue(rts);
         sheet2.createRow(0);
         
         assertEquals(0, workbook.getSheetAt(0).getFirstRowNum());
@@ -184,9 +187,9 @@ public class TestXSSFWorkbook extends TestCase {
         
         PackagePart wbPart = 
                pkg.getPart(PackagingURIHelper.createPartName("/xl/workbook.xml"));
-        // Links to the three sheets
+        // Links to the three sheets, shared strings and styles
         assertTrue(wbPart.hasRelationships());
-        assertEquals(3, wbPart.getRelationships().size());
+        assertEquals(5, wbPart.getRelationships().size());
         
         // Load back the XSSFWorkbook
         workbook = new XSSFWorkbook(pkg);
@@ -195,12 +198,19 @@ public class TestXSSFWorkbook extends TestCase {
         assertNotNull(workbook.getSheetAt(1));
         assertNotNull(workbook.getSheetAt(2));
         
+        assertNotNull(workbook.getSharedStringSource());
+        assertNotNull(workbook.getStylesSource());
+        
         assertEquals(0, workbook.getSheetAt(0).getFirstRowNum());
         assertEquals(1, workbook.getSheetAt(0).getLastRowNum());
         assertEquals(0, workbook.getSheetAt(1).getFirstRowNum());
         assertEquals(0, workbook.getSheetAt(1).getLastRowNum());
         assertEquals(-1, workbook.getSheetAt(2).getFirstRowNum());
         assertEquals(-1, workbook.getSheetAt(2).getLastRowNum());
+        
+        sheet1 = workbook.getSheetAt(0);
+        assertEquals(1.2, sheet1.getRow(0).getCell(0).getNumericCellValue(), 0.0001);
+        assertEquals("hello world", sheet1.getRow(1).getCell(0).getRichStringCellValue().getString());
     }
     
     public void testExisting() throws Exception {