001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.poi;
022:
023: import com.liferay.portal.kernel.util.StringMaker;
024:
025: import java.io.InputStream;
026:
027: import java.util.Iterator;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.apache.poi.hssf.usermodel.HSSFCell;
032: import org.apache.poi.hssf.usermodel.HSSFRow;
033: import org.apache.poi.hssf.usermodel.HSSFSheet;
034: import org.apache.poi.hssf.usermodel.HSSFWorkbook;
035:
036: /**
037: * <a href="XLSTextStripper.java.html"><b><i>View Source</i></b></a>
038: *
039: * @author Mirco Tamburini
040: *
041: */
042: public class XLSTextStripper {
043:
044: public XLSTextStripper(InputStream in) {
045: try {
046: StringMaker sm = new StringMaker();
047:
048: HSSFWorkbook workbook = new HSSFWorkbook(in);
049:
050: int numOfSheets = workbook.getNumberOfSheets();
051:
052: for (int i = 0; i < numOfSheets; i++) {
053: HSSFSheet sheet = workbook.getSheetAt(i);
054:
055: Iterator rowIterator = sheet.rowIterator();
056:
057: while (rowIterator.hasNext()) {
058: HSSFRow row = (HSSFRow) rowIterator.next();
059:
060: Iterator cellIterator = row.cellIterator();
061:
062: while (cellIterator.hasNext()) {
063: HSSFCell cell = (HSSFCell) cellIterator.next();
064:
065: String cellStringValue = null;
066:
067: if (cell.getCellType() == 4) {
068: boolean booleanValue = cell
069: .getBooleanCellValue();
070: cellStringValue = Boolean
071: .toString(booleanValue);
072: } else if (cell.getCellType() == 0) {
073: double doubleValue = cell
074: .getNumericCellValue();
075: cellStringValue = Double
076: .toString(doubleValue);
077: } else if (cell.getCellType() == 1) {
078: cellStringValue = cell
079: .getRichStringCellValue()
080: .getString();
081: }
082:
083: if (cellStringValue != null) {
084: sm.append(cellStringValue);
085: sm.append("\t");
086: }
087: }
088:
089: sm.append("\n");
090: }
091: }
092:
093: _text = sm.toString();
094: } catch (Exception e) {
095: _log.error(e.getMessage());
096: }
097: }
098:
099: public String getText() {
100: return _text;
101: }
102:
103: private static Log _log = LogFactory.getLog(XLSTextStripper.class);
104:
105: private String _text;
106:
107: }
|