001    /**
002     * Copyright (c) 2000-2010 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                    try {
037                            StringBundler sb = new StringBundler();
038    
039                            HSSFWorkbook workbook = new HSSFWorkbook(is);
040    
041                            int numOfSheets = workbook.getNumberOfSheets();
042    
043                            for (int i = 0; i < numOfSheets; i++) {
044                                    HSSFSheet sheet = workbook.getSheetAt(i);
045    
046                                    Iterator<Row> rowIterator = sheet.rowIterator();
047    
048                                    while (rowIterator.hasNext()) {
049                                            Row row = rowIterator.next();
050    
051                                            Iterator<Cell> cellIterator = row.cellIterator();
052    
053                                            while (cellIterator.hasNext()) {
054                                                    Cell cell = cellIterator.next();
055    
056                                                    String cellStringValue = null;
057    
058                                                    if (cell.getCellType() == 4) {
059                                                            boolean booleanValue = cell.getBooleanCellValue();
060    
061                                                            cellStringValue = String.valueOf(booleanValue);
062                                                    }
063                                                    else if (cell.getCellType() == 0) {
064                                                            double doubleValue = cell.getNumericCellValue();
065    
066                                                            cellStringValue = String.valueOf(doubleValue);
067                                                    }
068                                                    else if (cell.getCellType() == 1) {
069                                                            cellStringValue =
070                                                                    cell.getRichStringCellValue().getString();
071                                                    }
072    
073                                                    if (cellStringValue != null) {
074                                                            sb.append(cellStringValue);
075                                                            sb.append("\t");
076                                                    }
077                                            }
078    
079                                            sb.append("\n");
080                                    }
081                            }
082    
083                            _text = sb.toString();
084                    }
085                    catch (Exception e) {
086                            _log.error(e.getMessage());
087                    }
088            }
089    
090            public String getText() {
091                    return  _text;
092            }
093    
094            private static Log _log = LogFactoryUtil.getLog(XLSTextStripper.class);
095    
096            private String _text;
097    
098    }