1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.util.poi;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.StringBundler;
20  
21  import java.io.InputStream;
22  
23  import java.util.Iterator;
24  
25  import org.apache.poi.hssf.usermodel.HSSFSheet;
26  import org.apache.poi.hssf.usermodel.HSSFWorkbook;
27  import org.apache.poi.ss.usermodel.Cell;
28  import org.apache.poi.ss.usermodel.Row;
29  
30  /**
31   * <a href="XLSTextStripper.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Mirco Tamburini
34   */
35  public class XLSTextStripper {
36  
37      public XLSTextStripper(InputStream is) {
38          try {
39              StringBundler sb = new StringBundler();
40  
41              HSSFWorkbook workbook = new HSSFWorkbook(is);
42  
43              int numOfSheets = workbook.getNumberOfSheets();
44  
45              for (int i = 0; i < numOfSheets; i++) {
46                  HSSFSheet sheet = workbook.getSheetAt(i);
47  
48                  Iterator<Row> rowIterator = sheet.rowIterator();
49  
50                  while (rowIterator.hasNext()) {
51                      Row row = rowIterator.next();
52  
53                      Iterator<Cell> cellIterator = row.cellIterator();
54  
55                      while (cellIterator.hasNext()) {
56                          Cell cell = cellIterator.next();
57  
58                          String cellStringValue = null;
59  
60                          if (cell.getCellType() == 4) {
61                              boolean booleanValue = cell.getBooleanCellValue();
62  
63                              cellStringValue = String.valueOf(booleanValue);
64                          }
65                          else if (cell.getCellType() == 0) {
66                              double doubleValue = cell.getNumericCellValue();
67  
68                              cellStringValue = String.valueOf(doubleValue);
69                          }
70                          else if (cell.getCellType() == 1) {
71                              cellStringValue =
72                                  cell.getRichStringCellValue().getString();
73                          }
74  
75                          if (cellStringValue != null) {
76                              sb.append(cellStringValue);
77                              sb.append("\t");
78                          }
79                      }
80  
81                      sb.append("\n");
82                  }
83              }
84  
85              _text = sb.toString();
86          }
87          catch (Exception e) {
88              _log.error(e.getMessage());
89          }
90      }
91  
92      public String getText() {
93          return  _text;
94      }
95  
96      private static Log _log = LogFactoryUtil.getLog(XLSTextStripper.class);
97  
98      private String _text;
99  
100 }