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