]> source.dussan.org Git - poi.git/commitdiff
Update hssf.extractor.ExcelExtractor to optionally output blank cells too
authorNick Burch <nick@apache.org>
Sun, 21 Sep 2008 19:17:41 +0000 (19:17 +0000)
committerNick Burch <nick@apache.org>
Sun, 21 Sep 2008 19:17:41 +0000 (19:17 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@697589 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/changes.xml
src/documentation/content/xdocs/status.xml
src/java/org/apache/poi/hssf/extractor/ExcelExtractor.java
src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java

index c9b7f86fb62a7957572950c67e85370e05af0fd8..1d03b99cec3a66f884f27eb64c37aae1c0eb3075 100644 (file)
@@ -37,6 +37,7 @@
 
                <!-- Don't forget to update status.xml too! -->
         <release version="3.2-alpha1" date="2008-??-??">
+           <action dev="POI-DEVELOPERS" type="add">Update hssf.extractor.ExcelExtractor to optionally output blank cells too</action>
            <action dev="POI-DEVELOPERS" type="add">Include the sheet name in the output of examples.XLS2CSVmra</action>
            <action dev="POI-DEVELOPERS" type="fix">45784 - Support long chart titles in SeriesTextRecords</action>
            <action dev="POI-DEVELOPERS" type="fix">45777 - Throw an exception if HSSF Footer or Header is attemped to be set too long, rather than having it break during writing out</action>
index ae6a90ae09a1ad47142813afa1a31898adbbd908..976c02833a03905143c451fbb378795bbe3dd4be 100644 (file)
@@ -34,6 +34,7 @@
        <!-- Don't forget to update changes.xml too! -->
     <changes>
         <release version="3.2-alpha1" date="2008-??-??">
+           <action dev="POI-DEVELOPERS" type="add">Update hssf.extractor.ExcelExtractor to optionally output blank cells too</action>
            <action dev="POI-DEVELOPERS" type="add">Include the sheet name in the output of examples.XLS2CSVmra</action>
            <action dev="POI-DEVELOPERS" type="fix">45784 - Support long chart titles in SeriesTextRecords</action>
            <action dev="POI-DEVELOPERS" type="fix">45777 - Throw an exception if HSSF Footer or Header is attemped to be set too long, rather than having it break during writing out</action>
index d5dc30d00c0c58030e19d0351231eb0b8c373eed..26fb1f8c3b04534bb554b0151be3f750fc30d1a9 100644 (file)
@@ -44,6 +44,7 @@ public class ExcelExtractor extends POIOLE2TextExtractor {
        private boolean includeSheetNames = true;
        private boolean formulasNotResults = false;
        private boolean includeCellComments = false;
+       private boolean includeBlankCells = false;
        
        public ExcelExtractor(HSSFWorkbook wb) {
                super(wb);
@@ -73,13 +74,26 @@ public class ExcelExtractor extends POIOLE2TextExtractor {
     public void setIncludeCellComments(boolean includeCellComments) {
         this.includeCellComments = includeCellComments;
     }
+       /**
+        * Should blank cells be output? Default is to only
+        *  output cells that are present in the file and are
+        *  non-blank.
+        */
+       public void setIncludeBlankCells(boolean includeBlankCells) {
+               this.includeBlankCells = includeBlankCells;
+       }
        
        /**
         * Retreives the text contents of the file
         */
        public String getText() {
                StringBuffer text = new StringBuffer();
+
+               // We don't care about the differnce between
+               //  null (missing) and blank cells
+               wb.setMissingCellPolicy(HSSFRow.RETURN_BLANK_AS_NULL);
                
+               // Process each sheet in turn
                for(int i=0;i<wb.getNumberOfSheets();i++) {
                        HSSFSheet sheet = wb.getSheetAt(i);
                        if(sheet == null) { continue; }
@@ -108,63 +122,68 @@ public class ExcelExtractor extends POIOLE2TextExtractor {
                                // Check each cell in turn
                                int firstCell = row.getFirstCellNum();
                                int lastCell = row.getLastCellNum();
+                               if(includeBlankCells) {
+                                       firstCell = 0;
+                               }
+                               
                                for(int k=firstCell;k<lastCell;k++) {
                                        HSSFCell cell = row.getCell(k);
-                                       if(cell == null) { continue; }
                                        boolean outputContents = true;
-                                       
-                                       switch(cell.getCellType()) {
-                                               case HSSFCell.CELL_TYPE_BLANK:
-                                                       outputContents = false;
-                                                       break;
-                                               case HSSFCell.CELL_TYPE_STRING:
-                                                       text.append(cell.getRichStringCellValue().getString());
-                                                       break;
-                                               case HSSFCell.CELL_TYPE_NUMERIC:
-                                                       // Note - we don't apply any formatting!
-                                                       text.append(cell.getNumericCellValue());
-                                                       break;
-                                               case HSSFCell.CELL_TYPE_BOOLEAN:
-                                                       text.append(cell.getBooleanCellValue());
-                                                       break;
-                                               case HSSFCell.CELL_TYPE_ERROR:
-                                                       text.append(ErrorEval.getText(cell.getErrorCellValue()));
-                                                       break;
-                                               case HSSFCell.CELL_TYPE_FORMULA:
-                                                       if(formulasNotResults) {
-                                                               text.append(cell.getCellFormula());
-                                                       } else {
-                                                               switch(cell.getCachedFormulaResultType()) {
-                                                                       case HSSFCell.CELL_TYPE_STRING:
-                                                                               HSSFRichTextString str = cell.getRichStringCellValue();
-                                                                               if(str != null && str.length() > 0) {
-                                                                                       text.append(str.toString());
-                                                                               }
-                                                                               break;
-                                                                       case HSSFCell.CELL_TYPE_NUMERIC:
-                                                                               text.append(cell.getNumericCellValue());
-                                                                               break;
-                                                                       case HSSFCell.CELL_TYPE_BOOLEAN:
-                                                                               text.append(cell.getBooleanCellValue());
-                                                                               break;
-                                                                       case HSSFCell.CELL_TYPE_ERROR:
-                                                                               text.append(ErrorEval.getText(cell.getErrorCellValue()));
-                                                                               break;
-                                                                               
+
+                                       if(cell == null) {
+                                               // Only output if requested
+                                               outputContents = includeBlankCells;
+                                       } else {
+                                               switch(cell.getCellType()) {
+                                                       case HSSFCell.CELL_TYPE_STRING:
+                                                               text.append(cell.getRichStringCellValue().getString());
+                                                               break;
+                                                       case HSSFCell.CELL_TYPE_NUMERIC:
+                                                               // Note - we don't apply any formatting!
+                                                               text.append(cell.getNumericCellValue());
+                                                               break;
+                                                       case HSSFCell.CELL_TYPE_BOOLEAN:
+                                                               text.append(cell.getBooleanCellValue());
+                                                               break;
+                                                       case HSSFCell.CELL_TYPE_ERROR:
+                                                               text.append(ErrorEval.getText(cell.getErrorCellValue()));
+                                                               break;
+                                                       case HSSFCell.CELL_TYPE_FORMULA:
+                                                               if(formulasNotResults) {
+                                                                       text.append(cell.getCellFormula());
+                                                               } else {
+                                                                       switch(cell.getCachedFormulaResultType()) {
+                                                                               case HSSFCell.CELL_TYPE_STRING:
+                                                                                       HSSFRichTextString str = cell.getRichStringCellValue();
+                                                                                       if(str != null && str.length() > 0) {
+                                                                                               text.append(str.toString());
+                                                                                       }
+                                                                                       break;
+                                                                               case HSSFCell.CELL_TYPE_NUMERIC:
+                                                                                       text.append(cell.getNumericCellValue());
+                                                                                       break;
+                                                                               case HSSFCell.CELL_TYPE_BOOLEAN:
+                                                                                       text.append(cell.getBooleanCellValue());
+                                                                                       break;
+                                                                               case HSSFCell.CELL_TYPE_ERROR:
+                                                                                       text.append(ErrorEval.getText(cell.getErrorCellValue()));
+                                                                                       break;
+                                                                                       
+                                                                       }
                                                                }
-                                                       }
-                                                       break;
-                                               default:
-                                                       throw new RuntimeException("Unexpected cell type (" + cell.getCellType() + ")");
-                                       }
-                                       
-                                       // Output the comment, if requested and exists
-                                   HSSFComment comment = cell.getCellComment();
-                                       if(includeCellComments && comment != null) {
-                                           // Replace any newlines with spaces, otherwise it
-                                           //  breaks the output
-                                           String commentText = comment.getString().getString().replace('\n', ' ');
-                                           text.append(" Comment by "+comment.getAuthor()+": "+commentText);
+                                                               break;
+                                                       default:
+                                                               throw new RuntimeException("Unexpected cell type (" + cell.getCellType() + ")");
+                                               }
+                                               
+                                               // Output the comment, if requested and exists
+                                           HSSFComment comment = cell.getCellComment();
+                                               if(includeCellComments && comment != null) {
+                                                   // Replace any newlines with spaces, otherwise it
+                                                   //  breaks the output
+                                                   String commentText = comment.getString().getString().replace('\n', ' ');
+                                                   text.append(" Comment by "+comment.getAuthor()+": "+commentText);
+                                               }
                                        }
                                        
                                        // Output a tab if we're not on the last cell
index cb028edfa56c9b705e6c3b400b8c63b3e19a0416..d71565052871ea207deaaa922619bad05581ab49 100644 (file)
@@ -187,6 +187,27 @@ public final class TestExcelExtractor extends TestCase {
                );
        }
        
+       public void testWithBlank() throws Exception {
+               ExcelExtractor extractor = createExtractor("MissingBits.xls");
+               String def = extractor.getText();
+               extractor.setIncludeBlankCells(true);
+               String padded = extractor.getText();
+               
+               assertTrue(def.startsWith(
+                               "Sheet1\n" +
+                               "&[TAB]\t\n" +
+                               "Hello\t\n" +
+                               "11.0\t23.0\t\n"
+               ));
+               
+               assertTrue(padded.startsWith(
+                               "Sheet1\n" +
+                               "&[TAB]\t\n" +
+                               "Hello\t\t\t\t\t\t\t\t\t\t\t\n" +
+                               "11.0\t\t\t23.0\t\t\t\t\t\t\t\t\n"
+               ));
+       }
+       
        
        /**
         * Embded in a non-excel file