1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util.poi;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  
28  import java.io.InputStream;
29  
30  import java.util.Iterator;
31  
32  import org.apache.poi.hssf.usermodel.HSSFCell;
33  import org.apache.poi.hssf.usermodel.HSSFRow;
34  import org.apache.poi.hssf.usermodel.HSSFSheet;
35  import org.apache.poi.hssf.usermodel.HSSFWorkbook;
36  
37  /**
38   * <a href="XLSTextStripper.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Mirco Tamburini
41   */
42  public class XLSTextStripper {
43  
44      public XLSTextStripper(InputStream is) {
45          try {
46              StringBuilder sb = new StringBuilder();
47  
48              HSSFWorkbook workbook = new HSSFWorkbook(is);
49  
50              int numOfSheets = workbook.getNumberOfSheets();
51  
52              for (int i = 0; i < numOfSheets; i++) {
53                  HSSFSheet sheet = workbook.getSheetAt(i);
54  
55                  Iterator<HSSFRow> rowIterator = sheet.rowIterator();
56  
57                  while (rowIterator.hasNext()) {
58                      HSSFRow row = rowIterator.next();
59  
60                      Iterator<HSSFCell> cellIterator = row.cellIterator();
61  
62                      while (cellIterator.hasNext()) {
63                          HSSFCell cell = cellIterator.next();
64  
65                          String cellStringValue = null;
66  
67                          if (cell.getCellType() == 4) {
68                              boolean booleanValue = cell.getBooleanCellValue();
69  
70                              cellStringValue = String.valueOf(booleanValue);
71                          }
72                          else if (cell.getCellType() == 0) {
73                              double doubleValue = cell.getNumericCellValue();
74  
75                              cellStringValue = String.valueOf(doubleValue);
76                          }
77                          else if (cell.getCellType() == 1) {
78                              cellStringValue =
79                                  cell.getRichStringCellValue().getString();
80                          }
81  
82                          if (cellStringValue != null) {
83                              sb.append(cellStringValue);
84                              sb.append("\t");
85                          }
86                      }
87  
88                      sb.append("\n");
89                  }
90              }
91  
92              _text = sb.toString();
93          }
94          catch (Exception e) {
95              _log.error(e.getMessage());
96          }
97      }
98  
99      public String getText() {
100         return  _text;
101     }
102 
103     private static Log _log = LogFactoryUtil.getLog(XLSTextStripper.class);
104 
105     private String _text;
106 
107 }