1   /**
2    * Copyright (c) 2000-2007 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 com.liferay.portal.kernel.util.StringMaker;
26  
27  import java.io.InputStream;
28  
29  import java.util.Iterator;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.apache.poi.hssf.usermodel.HSSFCell;
34  import org.apache.poi.hssf.usermodel.HSSFRow;
35  import org.apache.poi.hssf.usermodel.HSSFSheet;
36  import org.apache.poi.hssf.usermodel.HSSFWorkbook;
37  
38  /**
39   * <a href="XLSTextStripper.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Mirco Tamburini
42   *
43   */
44  public class XLSTextStripper {
45  
46      public XLSTextStripper(InputStream in) {
47          try {
48              StringMaker sm = new StringMaker();
49  
50              HSSFWorkbook workbook = new HSSFWorkbook(in);
51  
52              int numOfSheets = workbook.getNumberOfSheets();
53  
54              for(int i = 0; i < numOfSheets; i++) {
55                  HSSFSheet sheet = workbook.getSheetAt(i);
56  
57                  Iterator rowIterator = sheet.rowIterator();
58  
59                  while (rowIterator.hasNext()) {
60                      HSSFRow row = (HSSFRow)rowIterator.next();
61  
62                      Iterator cellIterator = row.cellIterator();
63  
64                      while (cellIterator.hasNext()) {
65                          HSSFCell cell = (HSSFCell)cellIterator.next();
66  
67                          String cellStringValue = null;
68  
69                          if (cell.getCellType() == 4) {
70                              boolean booleanValue = cell.getBooleanCellValue();
71                              cellStringValue = Boolean.toString(booleanValue);
72                          }
73                          else if (cell.getCellType() == 0) {
74                              double doubleValue = cell.getNumericCellValue();
75                              cellStringValue = Double.toString(doubleValue);
76                          }
77                          else if (cell.getCellType() == 1) {
78                              cellStringValue =
79                                  cell.getRichStringCellValue().getString();
80                          }
81  
82                          if (cellStringValue != null) {
83                              sm.append(cellStringValue);
84                              sm.append("\t");
85                          }
86                      }
87  
88                      sm.append("\n");
89                  }
90              }
91  
92              _text = sm.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 }