選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

EventBasedExcelExtractor.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.extractor;
  16. import java.io.IOException;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import org.apache.poi.POIDocument;
  20. import org.apache.poi.POIOLE2TextExtractor;
  21. import org.apache.poi.hpsf.DocumentSummaryInformation;
  22. import org.apache.poi.hpsf.SummaryInformation;
  23. import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener;
  24. import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;
  25. import org.apache.poi.hssf.eventusermodel.HSSFListener;
  26. import org.apache.poi.hssf.eventusermodel.HSSFRequest;
  27. import org.apache.poi.hssf.model.HSSFFormulaParser;
  28. import org.apache.poi.hssf.record.BOFRecord;
  29. import org.apache.poi.hssf.record.BoundSheetRecord;
  30. import org.apache.poi.hssf.record.FormulaRecord;
  31. import org.apache.poi.hssf.record.LabelRecord;
  32. import org.apache.poi.hssf.record.LabelSSTRecord;
  33. import org.apache.poi.hssf.record.NoteRecord;
  34. import org.apache.poi.hssf.record.NumberRecord;
  35. import org.apache.poi.hssf.record.Record;
  36. import org.apache.poi.hssf.record.SSTRecord;
  37. import org.apache.poi.hssf.record.StringRecord;
  38. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  39. import org.apache.poi.poifs.filesystem.DirectoryNode;
  40. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  41. /**
  42. * A text extractor for Excel files, that is based
  43. * on the HSSF EventUserModel API.
  44. * It will typically use less memory than
  45. * {@link ExcelExtractor}, but may not provide
  46. * the same richness of formatting.
  47. * Returns the textual content of the file, suitable for
  48. * indexing by something like Lucene, but not really
  49. * intended for display to the user.
  50. * <p>
  51. * To turn an excel file into a CSV or similar, then see
  52. * the XLS2CSVmra example
  53. * </p>
  54. *
  55. * @see <a href="http://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/hssf/eventusermodel/examples/XLS2CSVmra.java">XLS2CSVmra</a>
  56. */
  57. public class EventBasedExcelExtractor extends POIOLE2TextExtractor implements org.apache.poi.ss.extractor.ExcelExtractor {
  58. private DirectoryNode _dir;
  59. boolean _includeSheetNames = true;
  60. boolean _formulasNotResults = false;
  61. public EventBasedExcelExtractor( DirectoryNode dir )
  62. {
  63. super( (POIDocument)null );
  64. _dir = dir;
  65. }
  66. public EventBasedExcelExtractor(POIFSFileSystem fs) {
  67. this(fs.getRoot());
  68. }
  69. /**
  70. * Would return the document information metadata for the document,
  71. * if we supported it
  72. */
  73. public DocumentSummaryInformation getDocSummaryInformation() {
  74. throw new IllegalStateException("Metadata extraction not supported in streaming mode, please use ExcelExtractor");
  75. }
  76. /**
  77. * Would return the summary information metadata for the document,
  78. * if we supported it
  79. */
  80. public SummaryInformation getSummaryInformation() {
  81. throw new IllegalStateException("Metadata extraction not supported in streaming mode, please use ExcelExtractor");
  82. }
  83. /**
  84. * Would control the inclusion of cell comments from the document,
  85. * if we supported it
  86. */
  87. public void setIncludeCellComments(boolean includeComments) {
  88. throw new IllegalStateException("Comment extraction not supported in streaming mode, please use ExcelExtractor");
  89. }
  90. /**
  91. * Would control the inclusion of headers and footers from the document,
  92. * if we supported it
  93. */
  94. public void setIncludeHeadersFooters(boolean includeHeadersFooters) {
  95. throw new IllegalStateException("Header/Footer extraction not supported in streaming mode, please use ExcelExtractor");
  96. }
  97. /**
  98. * Should sheet names be included? Default is true
  99. */
  100. public void setIncludeSheetNames(boolean includeSheetNames) {
  101. _includeSheetNames = includeSheetNames;
  102. }
  103. /**
  104. * Should we return the formula itself, and not
  105. * the result it produces? Default is false
  106. */
  107. public void setFormulasNotResults(boolean formulasNotResults) {
  108. _formulasNotResults = formulasNotResults;
  109. }
  110. /**
  111. * Retreives the text contents of the file
  112. */
  113. public String getText() {
  114. String text = null;
  115. try {
  116. TextListener tl = triggerExtraction();
  117. text = tl._text.toString();
  118. if(! text.endsWith("\n")) {
  119. text = text + "\n";
  120. }
  121. } catch(IOException e) {
  122. throw new RuntimeException(e);
  123. }
  124. return text;
  125. }
  126. private TextListener triggerExtraction() throws IOException {
  127. TextListener tl = new TextListener();
  128. FormatTrackingHSSFListener ft = new FormatTrackingHSSFListener(tl);
  129. tl._ft = ft;
  130. // Register and process
  131. HSSFEventFactory factory = new HSSFEventFactory();
  132. HSSFRequest request = new HSSFRequest();
  133. request.addListenerForAllRecords(ft);
  134. factory.processWorkbookEvents(request, _dir);
  135. return tl;
  136. }
  137. private class TextListener implements HSSFListener {
  138. FormatTrackingHSSFListener _ft;
  139. private SSTRecord sstRecord;
  140. private final List<String> sheetNames;
  141. final StringBuffer _text = new StringBuffer();
  142. private int sheetNum = -1;
  143. private int rowNum;
  144. private boolean outputNextStringValue = false;
  145. private int nextRow = -1;
  146. public TextListener() {
  147. sheetNames = new ArrayList<String>();
  148. }
  149. public void processRecord(Record record) {
  150. String thisText = null;
  151. int thisRow = -1;
  152. switch(record.getSid()) {
  153. case BoundSheetRecord.sid:
  154. BoundSheetRecord sr = (BoundSheetRecord)record;
  155. sheetNames.add(sr.getSheetname());
  156. break;
  157. case BOFRecord.sid:
  158. BOFRecord bof = (BOFRecord)record;
  159. if(bof.getType() == BOFRecord.TYPE_WORKSHEET) {
  160. sheetNum++;
  161. rowNum = -1;
  162. if(_includeSheetNames) {
  163. if(_text.length() > 0) _text.append("\n");
  164. _text.append(sheetNames.get(sheetNum));
  165. }
  166. }
  167. break;
  168. case SSTRecord.sid:
  169. sstRecord = (SSTRecord)record;
  170. break;
  171. case FormulaRecord.sid:
  172. FormulaRecord frec = (FormulaRecord) record;
  173. thisRow = frec.getRow();
  174. if(_formulasNotResults) {
  175. thisText = HSSFFormulaParser.toFormulaString((HSSFWorkbook)null, frec.getParsedExpression());
  176. } else {
  177. if(frec.hasCachedResultString()) {
  178. // Formula result is a string
  179. // This is stored in the next record
  180. outputNextStringValue = true;
  181. nextRow = frec.getRow();
  182. } else {
  183. thisText = _ft.formatNumberDateCell(frec);
  184. }
  185. }
  186. break;
  187. case StringRecord.sid:
  188. if(outputNextStringValue) {
  189. // String for formula
  190. StringRecord srec = (StringRecord)record;
  191. thisText = srec.getString();
  192. thisRow = nextRow;
  193. outputNextStringValue = false;
  194. }
  195. break;
  196. case LabelRecord.sid:
  197. LabelRecord lrec = (LabelRecord) record;
  198. thisRow = lrec.getRow();
  199. thisText = lrec.getValue();
  200. break;
  201. case LabelSSTRecord.sid:
  202. LabelSSTRecord lsrec = (LabelSSTRecord) record;
  203. thisRow = lsrec.getRow();
  204. if(sstRecord == null) {
  205. throw new IllegalStateException("No SST record found");
  206. }
  207. thisText = sstRecord.getString(lsrec.getSSTIndex()).toString();
  208. break;
  209. case NoteRecord.sid:
  210. NoteRecord nrec = (NoteRecord) record;
  211. thisRow = nrec.getRow();
  212. // TODO: Find object to match nrec.getShapeId()
  213. break;
  214. case NumberRecord.sid:
  215. NumberRecord numrec = (NumberRecord) record;
  216. thisRow = numrec.getRow();
  217. thisText = _ft.formatNumberDateCell(numrec);
  218. break;
  219. default:
  220. break;
  221. }
  222. if(thisText != null) {
  223. if(thisRow != rowNum) {
  224. rowNum = thisRow;
  225. if(_text.length() > 0)
  226. _text.append("\n");
  227. } else {
  228. _text.append("\t");
  229. }
  230. _text.append(thisText);
  231. }
  232. }
  233. }
  234. }