1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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 java.io.InputStream;
26  
27  import java.util.Iterator;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.poi.hssf.usermodel.HSSFCell;
32  import org.apache.poi.hssf.usermodel.HSSFRow;
33  import org.apache.poi.hssf.usermodel.HSSFSheet;
34  import org.apache.poi.hssf.usermodel.HSSFWorkbook;
35  
36  /**
37   * <a href="XLSTextStripper.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Mirco Tamburini
40   *
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 = LogFactory.getLog(XLSTextStripper.class);
104 
105     private String _text;
106 
107 }