unfinished release guide. It would be nice to have a html version.
[poi.git] / src / scratchpad / ooxml-src / org / apache / poi / hssf / extractor / HXFExcelExtractor.java
blob34ae0680052ecf29cc4a226a9534ad037987aa21
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
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 ==================================================================== */
17 package org.apache.poi.hssf.extractor;
19 import java.io.File;
20 import java.io.IOException;
22 import org.apache.poi.POIXMLTextExtractor;
23 import org.apache.poi.hssf.HSSFXML;
24 import org.apache.poi.hssf.usermodel.HSSFXMLCell;
25 import org.apache.poi.hssf.usermodel.HSSFXMLWorkbook;
26 import org.apache.poi.hxf.HXFDocument;
27 import org.apache.xmlbeans.XmlException;
28 import org.openxml4j.exceptions.OpenXML4JException;
29 import org.openxml4j.opc.Package;
30 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
31 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow;
32 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
33 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
35 /**
36 * Helper class to extract text from an OOXML Excel file
38 public class HXFExcelExtractor extends POIXMLTextExtractor {
39 private HSSFXMLWorkbook workbook;
40 private boolean includeSheetNames = true;
41 private boolean formulasNotResults = false;
43 public HXFExcelExtractor(Package container) throws XmlException, OpenXML4JException, IOException {
44 this(new HSSFXMLWorkbook(
45 new HSSFXML(container)
46 ));
48 public HXFExcelExtractor(HSSFXMLWorkbook workbook) {
49 super(workbook);
50 this.workbook = workbook;
53 public static void main(String[] args) throws Exception {
54 if(args.length < 1) {
55 System.err.println("Use:");
56 System.err.println(" HXFExcelExtractor <filename.xlsx>");
57 System.exit(1);
59 POIXMLTextExtractor extractor =
60 new HXFExcelExtractor(HXFDocument.openPackage(
61 new File(args[0])
62 ));
63 System.out.println(extractor.getText());
66 /**
67 * Should sheet names be included? Default is true
69 public void setIncludeSheetNames(boolean includeSheetNames) {
70 this.includeSheetNames = includeSheetNames;
72 /**
73 * Should we return the formula itself, and not
74 * the result it produces? Default is false
76 public void setFormulasNotResults(boolean formulasNotResults) {
77 this.formulasNotResults = formulasNotResults;
80 /**
81 * Retreives the text contents of the file
83 public String getText() {
84 StringBuffer text = new StringBuffer();
86 CTSheet[] sheetRefs =
87 workbook._getHSSFXML().getSheetReferences().getSheetArray();
88 for(int i=0; i<sheetRefs.length; i++) {
89 try {
90 CTWorksheet sheet =
91 workbook._getHSSFXML().getSheet(sheetRefs[i]);
92 CTRow[] rows =
93 sheet.getSheetData().getRowArray();
95 if(i > 0) {
96 text.append("\n");
98 if(includeSheetNames) {
99 text.append(sheetRefs[i].getName() + "\n");
102 for(int j=0; j<rows.length; j++) {
103 CTCell[] cells = rows[j].getCArray();
104 for(int k=0; k<cells.length; k++) {
105 CTCell cell = cells[k];
106 if(k > 0) {
107 text.append("\t");
110 boolean done = false;
112 // Is it a formula one?
113 if(cell.getF() != null) {
114 if(formulasNotResults) {
115 text.append(cell.getF().getStringValue());
116 done = true;
119 if(!done) {
120 HSSFXMLCell uCell = new HSSFXMLCell(cell, workbook);
121 text.append(uCell.getStringValue());
124 text.append("\n");
126 } catch(Exception e) {
127 throw new RuntimeException(e);
131 return text.toString();