diff options
author | Nick Burch <nick@apache.org> | 2008-04-13 15:09:42 +0000 |
---|---|---|
committer | Nick Burch <nick@apache.org> | 2008-04-13 15:09:42 +0000 |
commit | adb1c55e9606ea9e898712458949326b8e55c9cd (patch) | |
tree | 8a55e00e3d72c5316b30284a0a1acd4993037970 | |
parent | a54ece7864439a74d084a1ec2478aeccf691dce6 (diff) | |
download | poi-adb1c55e9606ea9e898712458949326b8e55c9cd.tar.gz poi-adb1c55e9606ea9e898712458949326b8e55c9cd.zip |
Finish off eventusermodel based Excel Extractor, and update the xls to csv converter (moved to correct place) based on discoveries for the text extractor
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@647576 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | src/examples/src/org/apache/poi/hssf/eventusermodel/examples/XLS2CSVmra.java (renamed from src/scratchpad/examples/src/org/apache/poi/hssf/eventusermodel/examples/XLS2CSVmra.java) | 40 | ||||
-rw-r--r-- | src/java/org/apache/poi/hssf/extractor/EventBasedExcelExtractor.java | 18 | ||||
-rw-r--r-- | src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java | 4 |
3 files changed, 48 insertions, 14 deletions
diff --git a/src/scratchpad/examples/src/org/apache/poi/hssf/eventusermodel/examples/XLS2CSVmra.java b/src/examples/src/org/apache/poi/hssf/eventusermodel/examples/XLS2CSVmra.java index 1c53f1ecc2..9bebd3a837 100644 --- a/src/scratchpad/examples/src/org/apache/poi/hssf/eventusermodel/examples/XLS2CSVmra.java +++ b/src/examples/src/org/apache/poi/hssf/eventusermodel/examples/XLS2CSVmra.java @@ -23,11 +23,7 @@ import java.io.PrintStream; import java.text.DateFormat; import java.text.DecimalFormat; import java.text.SimpleDateFormat; -import java.util.ArrayList; import java.util.Date; -import java.util.Hashtable; -import java.util.List; -import java.util.Map; import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener; import org.apache.poi.hssf.eventusermodel.HSSFEventFactory; @@ -36,11 +32,10 @@ import org.apache.poi.hssf.eventusermodel.HSSFRequest; import org.apache.poi.hssf.eventusermodel.MissingRecordAwareHSSFListener; import org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord; import org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord; +import org.apache.poi.hssf.model.FormulaParser; import org.apache.poi.hssf.record.BlankRecord; import org.apache.poi.hssf.record.BoolErrRecord; import org.apache.poi.hssf.record.CellValueRecordInterface; -import org.apache.poi.hssf.record.ExtendedFormatRecord; -import org.apache.poi.hssf.record.FormatRecord; import org.apache.poi.hssf.record.FormulaRecord; import org.apache.poi.hssf.record.LabelRecord; import org.apache.poi.hssf.record.LabelSSTRecord; @@ -49,7 +44,7 @@ import org.apache.poi.hssf.record.NumberRecord; import org.apache.poi.hssf.record.RKRecord; import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.SSTRecord; -import org.apache.poi.hssf.usermodel.HSSFDataFormat; +import org.apache.poi.hssf.record.StringRecord; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.poifs.filesystem.POIFSFileSystem; @@ -72,6 +67,11 @@ public class XLS2CSVmra implements HSSFListener { // Records we pick up as we process private SSTRecord sstRecord; private FormatTrackingHSSFListener formatListener; + + // For handling formulas with string results + private int nextRow; + private int nextColumn; + private boolean outputNextStringRecord; /** * Creates a new XLS -> CSV converter @@ -142,6 +142,7 @@ public class XLS2CSVmra implements HSSFListener { thisColumn = berec.getColumn(); thisStr = ""; break; + case FormulaRecord.sid: FormulaRecord frec = (FormulaRecord) record; @@ -149,12 +150,31 @@ public class XLS2CSVmra implements HSSFListener { thisColumn = frec.getColumn(); if(outputFormulaValues) { - thisStr = formatNumberDateCell(frec, frec.getValue()); + if(Double.isNaN( frec.getValue() )) { + // Formula result is a string + // This is stored in the next record + outputNextStringRecord = true; + nextRow = frec.getRow(); + nextColumn = frec.getColumn(); + } else { + thisStr = formatNumberDateCell(frec, frec.getValue()); + } } else { - // TODO: Output the formula string - thisStr = '"' + frec.toString() + '"'; + thisStr = '"' + + FormulaParser.toFormulaString(null, frec.getParsedExpression()) + '"'; } break; + case StringRecord.sid: + if(outputNextStringRecord) { + // String for formula + StringRecord srec = (StringRecord)record; + thisStr = srec.getString(); + thisRow = nextRow; + thisColumn = nextColumn; + outputNextStringRecord = false; + } + break; + case LabelRecord.sid: LabelRecord lrec = (LabelRecord) record; diff --git a/src/java/org/apache/poi/hssf/extractor/EventBasedExcelExtractor.java b/src/java/org/apache/poi/hssf/extractor/EventBasedExcelExtractor.java index 718c025168..8f3eebb2d3 100644 --- a/src/java/org/apache/poi/hssf/extractor/EventBasedExcelExtractor.java +++ b/src/java/org/apache/poi/hssf/extractor/EventBasedExcelExtractor.java @@ -42,6 +42,7 @@ import org.apache.poi.hssf.record.NoteRecord; import org.apache.poi.hssf.record.NumberRecord; import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.SSTRecord; +import org.apache.poi.hssf.record.StringRecord; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.poifs.filesystem.POIFSFileSystem; @@ -142,6 +143,9 @@ public class EventBasedExcelExtractor extends POIOLE2TextExtractor { private int sheetNum = -1; private int rowNum; + private boolean outputNextStringValue = false; + private int nextRow = -1; + public void processRecord(Record record) { String thisText = null; int thisRow = -1; @@ -175,12 +179,24 @@ public class EventBasedExcelExtractor extends POIOLE2TextExtractor { thisText = FormulaParser.toFormulaString(null, frec.getParsedExpression()); } else { if(Double.isNaN( frec.getValue() )) { - thisText = "(todo - string formulas)"; + // Formula result is a string + // This is stored in the next record + outputNextStringValue = true; + nextRow = frec.getRow(); } else { thisText = formatNumberDateCell(frec, frec.getValue()); } } break; + case StringRecord.sid: + if(outputNextStringValue) { + // String for formula + StringRecord srec = (StringRecord)record; + thisText = srec.getString(); + thisRow = nextRow; + outputNextStringValue = false; + } + break; case LabelRecord.sid: LabelRecord lrec = (LabelRecord) record; thisRow = lrec.getRow(); diff --git a/src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java b/src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java index b93bc65a24..63d67ee771 100644 --- a/src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java +++ b/src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java @@ -136,9 +136,7 @@ public final class TestExcelExtractor extends TestCase { extractor.setIncludeSheetNames(true); String text = extractor.getText(); - // TODO - assertEquals("Sheet1\nreplaceme\nreplaceme\n(todo - string formulas)\nSheet2\nSheet3\n", text); -// assertEquals("Sheet1\nreplaceme\nreplaceme\nreplacemereplaceme\nSheet2\nSheet3\n", text); + assertEquals("Sheet1\nreplaceme\nreplaceme\nreplacemereplaceme\nSheet2\nSheet3\n", text); extractor.setIncludeSheetNames(false); extractor.setFormulasNotResults(true); |