| XLSTextStripper.java |
1 /**
2 * Copyright (c) 2000-2009 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.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 */
43 public class XLSTextStripper {
44
45 public XLSTextStripper(InputStream is) {
46 try {
47 StringBuilder sb = new StringBuilder();
48
49 HSSFWorkbook workbook = new HSSFWorkbook(is);
50
51 int numOfSheets = workbook.getNumberOfSheets();
52
53 for (int i = 0; i < numOfSheets; i++) {
54 HSSFSheet sheet = workbook.getSheetAt(i);
55
56 Iterator<HSSFRow> rowIterator = sheet.rowIterator();
57
58 while (rowIterator.hasNext()) {
59 HSSFRow row = rowIterator.next();
60
61 Iterator<HSSFCell> cellIterator = row.cellIterator();
62
63 while (cellIterator.hasNext()) {
64 HSSFCell cell = cellIterator.next();
65
66 String cellStringValue = null;
67
68 if (cell.getCellType() == 4) {
69 boolean booleanValue = cell.getBooleanCellValue();
70
71 cellStringValue = String.valueOf(booleanValue);
72 }
73 else if (cell.getCellType() == 0) {
74 double doubleValue = cell.getNumericCellValue();
75
76 cellStringValue = String.valueOf(doubleValue);
77 }
78 else if (cell.getCellType() == 1) {
79 cellStringValue =
80 cell.getRichStringCellValue().getString();
81 }
82
83 if (cellStringValue != null) {
84 sb.append(cellStringValue);
85 sb.append("\t");
86 }
87 }
88
89 sb.append("\n");
90 }
91 }
92
93 _text = sb.toString();
94 }
95 catch (Exception e) {
96 _log.error(e.getMessage());
97 }
98 }
99
100 public String getText() {
101 return _text;
102 }
103
104 private static Log _log = LogFactoryUtil.getLog(XLSTextStripper.class);
105
106 private String _text;
107
108 }