001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util.poi;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.StringBundler;
020    
021    import java.io.InputStream;
022    
023    import java.util.Iterator;
024    
025    import org.apache.poi.hssf.usermodel.HSSFSheet;
026    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
027    import org.apache.poi.ss.usermodel.Cell;
028    import org.apache.poi.ss.usermodel.Row;
029    
030    /**
031     * @author Mirco Tamburini
032     */
033    public class XLSTextStripper {
034    
035            public XLSTextStripper(InputStream is) {
036                    String text = null;
037    
038                    try {
039                            StringBundler sb = new StringBundler();
040    
041                            HSSFWorkbook workbook = new HSSFWorkbook(is);
042    
043                            int numOfSheets = workbook.getNumberOfSheets();
044    
045                            for (int i = 0; i < numOfSheets; i++) {
046                                    HSSFSheet sheet = workbook.getSheetAt(i);
047    
048                                    Iterator<Row> rowIterator = sheet.rowIterator();
049    
050                                    while (rowIterator.hasNext()) {
051                                            Row row = rowIterator.next();
052    
053                                            Iterator<Cell> cellIterator = row.cellIterator();
054    
055                                            while (cellIterator.hasNext()) {
056                                                    Cell cell = cellIterator.next();
057    
058                                                    String cellStringValue = null;
059    
060                                                    if (cell.getCellType() == 4) {
061                                                            boolean booleanValue = cell.getBooleanCellValue();
062    
063                                                            cellStringValue = String.valueOf(booleanValue);
064                                                    }
065                                                    else if (cell.getCellType() == 0) {
066                                                            double doubleValue = cell.getNumericCellValue();
067    
068                                                            cellStringValue = String.valueOf(doubleValue);
069                                                    }
070                                                    else if (cell.getCellType() == 1) {
071                                                            cellStringValue =
072                                                                    cell.getRichStringCellValue().getString();
073                                                    }
074    
075                                                    if (cellStringValue != null) {
076                                                            sb.append(cellStringValue);
077                                                            sb.append("\t");
078                                                    }
079                                            }
080    
081                                            sb.append("\n");
082                                    }
083                            }
084    
085                            text = sb.toString();
086                    }
087                    catch (Exception e) {
088                            _log.error(e.getMessage());
089                    }
090    
091                    _text = text;
092            }
093    
094            public String getText() {
095                    return _text;
096            }
097    
098            private static final Log _log = LogFactoryUtil.getLog(
099                    XLSTextStripper.class);
100    
101            private final String _text;
102    
103    }