]> source.dussan.org Git - poi.git/commitdiff
60879 -- figure out if we can support old beta xlsb or throw exception. For now...
authorTim Allison <tallison@apache.org>
Mon, 20 Mar 2017 18:50:25 +0000 (18:50 +0000)
committerTim Allison <tallison@apache.org>
Mon, 20 Mar 2017 18:50:25 +0000 (18:50 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1787832 13f79535-47bb-0310-9956-ffa450edef68

src/integrationtest/org/apache/poi/stress/XSSFBFileHandler.java
src/ooxml/java/org/apache/poi/xssf/binary/XSSFBRecordType.java
src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFBReader.java
src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFBEventBasedExcelExtractor.java

index 52691de9a04f073daf8c57d788273c62ee6dfec9..02c5193d40b9f89e7943bdb3859147798267cd40 100644 (file)
@@ -32,11 +32,8 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 public class XSSFBFileHandler extends AbstractFileHandler {
 
     static {
-        //this is a "Beta" xlsb version and is not openable with Excel 2016
-        //TODO: see if we can support this easily enough
-        AbstractFileHandler.EXPECTED_EXTRACTOR_FAILURES.add(
-                "spreadsheet/Simple.xlsb"
-        );
+        //add expected failures here:
+//        AbstractFileHandler.EXPECTED_EXTRACTOR_FAILURES.add("spreadsheet/Simple.xlsb");
     }
 
     @Override
index 436dee986fe6eab7d48da0821f09f5b77691071f..35465043067f00f5ff020738bd0264d07caa64b0 100644 (file)
@@ -73,6 +73,10 @@ public enum XSSFBRecordType {
     BrtEndSst(160),   //stored strings end sst
 
     BrtBundleSh(156), //defines worksheet in wb part
+
+    //TODO -- implement these as needed
+    //BrtFileVersion(128), //file version
+    //BrtWbProp(153), //Workbook prop contains 1904/1900-date based bit
     Unimplemented(-1);
 
     private static final Map<Integer, XSSFBRecordType> TYPE_MAP =
index cd8a208455abdb7b94d9ef3bcb56969c296ec755..825422936e1555d93803358670845f29308ca357 100644 (file)
@@ -32,6 +32,8 @@ import org.apache.poi.openxml4j.opc.PackageRelationship;
 import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
 import org.apache.poi.openxml4j.opc.PackagingURIHelper;
 import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.POILogFactory;
+import org.apache.poi.util.POILogger;
 import org.apache.poi.xssf.binary.XSSFBCommentsTable;
 import org.apache.poi.xssf.binary.XSSFBParseException;
 import org.apache.poi.xssf.binary.XSSFBParser;
@@ -48,6 +50,9 @@ import org.apache.poi.xssf.usermodel.XSSFRelation;
  * @since 3.16-beta3
  */
 public class XSSFBReader extends XSSFReader {
+
+    private final static POILogger log = POILogFactory.getLogger(XSSFBReader.class);
+
     /**
      * Creates a new XSSFReader, for the given package
      *
@@ -147,6 +152,23 @@ public class XSSFBReader extends XSSFReader {
         }
 
         private void addWorksheet(byte[] data) {
+            //try to parse the BrtBundleSh
+            //if there's an exception, catch it and
+            //try to figure out if this is one of the old beta-created xlsb files
+            //or if this is a general exception
+            try {
+                tryToAddWorksheet(data);
+            } catch (XSSFBParseException e) {
+                if (tryOldFormat(data)) {
+                    log.log(POILogger.WARN, "This file was written with a beta version of Excel. "+
+                            "POI will try to parse the file as a regular xlsb.");
+                } else {
+                    throw e;
+                }
+            }
+        }
+
+        private void tryToAddWorksheet(byte[] data) throws XSSFBParseException {
             int offset = 0;
             //this is the sheet state #2.5.142
             long hsShtat = LittleEndian.getUInt(data, offset); offset += LittleEndian.INT_SIZE;
@@ -158,13 +180,35 @@ public class XSSFBReader extends XSSFReader {
             }
             StringBuilder sb = new StringBuilder();
             offset += XSSFBUtils.readXLWideString(data, offset, sb);
+            String relId = sb.toString(); sb.setLength(0);
+            offset += XSSFBUtils.readXLWideString(data, offset, sb);
+            String name = sb.toString();
+            if (relId != null && relId.trim().length() > 0) {
+                sheets.add(new XSSFSheetRef(relId, name));
+            }
+        }
+
+        private boolean tryOldFormat(byte[] data) throws XSSFBParseException {
+            //undocumented what is contained in these 8 bytes.
+            //for the non-beta xlsb files, this would be 4, not 8.
+            int offset = 8;
+            long iTabID = LittleEndian.getUInt(data, offset); offset += LittleEndian.INT_SIZE;
+            if (iTabID < 1 || iTabID > 0x0000FFFFL) {
+                throw new XSSFBParseException("table id out of range: "+iTabID);
+            }
+            StringBuilder sb = new StringBuilder();
+            offset += XSSFBUtils.readXLWideString(data, offset, sb);
             String relId = sb.toString();
             sb.setLength(0);
-            XSSFBUtils.readXLWideString(data, offset, sb);
+            offset += XSSFBUtils.readXLWideString(data, offset, sb);
             String name = sb.toString();
             if (relId != null && relId.trim().length() > 0) {
                 sheets.add(new XSSFSheetRef(relId, name));
             }
+            if (offset == data.length) {
+                return true;
+            }
+            return false;
         }
 
         List<XSSFSheetRef> getSheets() {
index a49a1082172b9e1f4dfb7fbfca37659dd975b10a..56d17ee27d6be8c3ca6664052bbf8107ffed7540 100644 (file)
@@ -99,4 +99,13 @@ public class TestXSSFBEventBasedExcelExtractor {
         }
     }
 
+    @Test
+    public void testBeta() throws Exception {
+        XSSFEventBasedExcelExtractor extractor = getExtractor("Simple.xlsb");
+        extractor.setIncludeCellComments(true);
+        String text = extractor.getText();
+        assertContains(text,
+                "This is an example spreadsheet created with Microsoft Excel 2007 Beta 2.");
+    }
+
 }