Example usage for org.apache.poi.poifs.filesystem POIFSFileSystem POIFSFileSystem

List of usage examples for org.apache.poi.poifs.filesystem POIFSFileSystem POIFSFileSystem

Introduction

In this page you can find the example usage for org.apache.poi.poifs.filesystem POIFSFileSystem POIFSFileSystem.

Prototype


public POIFSFileSystem(InputStream stream) throws IOException 

Source Link

Document

Create a POIFSFileSystem from an InputStream.

Usage

From source file:cn.trymore.core.util.excel.PoiExcelParser.java

License:Open Source License

public void open(int sheetIdx) throws Exception {
    InputStream input = new FileInputStream(this.xlsFile);
    POIFSFileSystem poi = new POIFSFileSystem(input);
    this.book = new HSSFWorkbook(poi);
    this.sheet = getSheet(sheetIdx);
    this.cfms = this.sheet.getSheetConditionalFormatting();
}

From source file:com.afrisoftech.lib.ExportData.java

public static Vector read(String fileName) {
    Vector cellVectorHolder = new Vector();
    try {//www.  java2s. com

        FileInputStream myInput = new FileInputStream(fileName);
        POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
        HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
        HSSFSheet mySheet = myWorkBook.getSheetAt(0);
        Iterator rowIter = mySheet.rowIterator();
        while (rowIter.hasNext()) {
            HSSFRow myRow = (HSSFRow) rowIter.next();
            Iterator cellIter = myRow.cellIterator();
            Vector cellStoreVector = new Vector();
            while (cellIter.hasNext()) {
                HSSFCell myCell = (HSSFCell) cellIter.next();
                //Object obj=myCell.getStringCellValue();
                System.out.print(myCell.getCellType() + " -");
                if (myCell.getCellType() == 0) {
                    cellStoreVector.addElement(myCell.getNumericCellValue());

                } else if (myCell.getCellType() == 1) {
                    cellStoreVector.addElement(myCell.getStringCellValue());

                }
                //cellStoreVector.addElement(myCell.getStringCellValue());
            }
            System.out.println();
            cellVectorHolder.addElement(cellStoreVector);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return cellVectorHolder;
}

From source file:com.auxilii.msgparser.MsgParser.java

License:Open Source License

/**
 * Parses a .msg file provided by an input stream.
 *
 * @param msgFileStream The .msg file as a InputStream.
 * @return A {@link Message} object representing the .msg file.
 * @throws IOException Thrown if the file could not be loaded or parsed.
 *//*from w w w .  j  a  va 2  s . c  o  m*/
public Message parseMsg(InputStream msgFileStream) throws IOException {
    // the .msg file, like a file system, contains directories
    // and documents within this directories
    // we now gain access to the root node
    // and recursively go through the complete 'filesystem'.
    POIFSFileSystem fs = new POIFSFileSystem(msgFileStream);
    DirectoryEntry dir = fs.getRoot();
    Message msg = new Message();
    parseMsg(dir, msg);
    return msg;
}

From source file:com.bayareasoftware.chartengine.ds.util.ExcelDump.java

License:Apache License

private static void runNew(String fileName) throws Exception {
    InputStream inp = new FileInputStream(fileName);
    HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(inp));
    ExcelExtractor xt = new ExcelExtractor(wb);

    xt.setFormulasNotResults(false);//from  www  .  java  2 s . c o m
    xt.setIncludeBlankCells(true);
    xt.setIncludeSheetNames(false);
    String text = xt.getText();
    String[] lines = StringUtil.splitCompletely(text, '\n');
    for (int i = 0; i < lines.length; i++) {
        System.out.println((i + 1) + ") " + lines[i]);
    }
    System.out.println("XLS: \n" + text);
}

From source file:com.bayareasoftware.chartengine.ds.util.ExcelDump.java

License:Apache License

private static void runOld(String fileName) throws Exception {
    InputStream is = new FileInputStream(fileName);
    POIFSFileSystem fs = new POIFSFileSystem(is);
    HSSFWorkbook wb = new HSSFWorkbook(fs);
    HSSFSheet sheet = wb.getSheetAt(0);/* w  w w .  j av  a2s.c  om*/
    int firstRow = sheet.getFirstRowNum();
    int lastRow = sheet.getLastRowNum();
    p("first/last row: " + firstRow + "/" + lastRow);
    HSSFRow[] rows = new HSSFRow[lastRow + 1];
    int maxFirstCell = 0, maxLastCell = 0;
    for (int i = firstRow; i <= lastRow; i++) {
        HSSFRow r = sheet.getRow(i);
        if (r != null) {
            rows[i] = r;
            maxFirstCell = Math.max(maxFirstCell, r.getFirstCellNum());
            maxLastCell = Math.max(maxLastCell, r.getLastCellNum());

        }
    }
    p("maxFirstCell=" + maxFirstCell + ", maxLastCell=" + maxLastCell);

    StringBuilder table = new StringBuilder();
    table.append("<html><head><style>\n");
    table.append("body,td { font-family: monospaced; font-size: 12 }\n");
    table.append("</style></head>");
    table.append("<p>maxFirstCell=" + maxFirstCell + " maxLastCell=" + maxLastCell + "</p>");
    table.append("<table border=\"1\">");
    for (int i = firstRow; i <= lastRow; i++) {
        HSSFRow r = sheet.getRow(i);
        if (r == null) {
            System.err.println("NULL row at " + i);
        }
        table.append(row2string(r, maxLastCell));
    }
    table.append("</table></body></html>");
    File f = new File("sheet.html");
    Writer w = new FileWriter(f);
    w.write(table.toString());
    w.close();
    p("saved to " + f.getAbsolutePath());
}

From source file:com.bayareasoftware.chartengine.ds.util.XLS2Data.java

License:Apache License

/**
 * Creates a new XLS -> CSV converter
 * //from  ww w. j av  a 2  s  . c  o m
 * @param filename
 *      The file to process
 * @param minColumns
 *      The minimum number of columns to output, or -1 for no minimum
 * @throws IOException
 * @throws FileNotFoundException
 */
public XLS2Data(InputStream is, String sheetName, int maxRows) throws IOException {
    this(new POIFSFileSystem(is), sheetName, maxRows);
}

From source file:com.beyondb.io.ExcelControl.java

private org.apache.poi.ss.usermodel.Sheet getSheet() throws IOException, InvalidFormatException {
    org.apache.poi.ss.usermodel.Sheet sheet = null;

    try {/*from   www  .j  a  va  2s.  com*/

        m_InputStream = new FileInputStream(m_File);

        if (!m_InputStream.markSupported()) {
            m_InputStream = new PushbackInputStream(m_InputStream, 8);
        }
        if (POIFSFileSystem.hasPOIFSHeader(m_InputStream)) {
            POIFSFileSystem poifsfs = new POIFSFileSystem(m_InputStream);
            m_Workerbook = WorkbookFactory.create(poifsfs);

        } else if (POIXMLDocument.hasOOXMLHeader(m_InputStream)) {
            m_Workerbook = WorkbookFactory.create(OPCPackage.open(m_File));
        } else {
            throw new IllegalArgumentException("excel????");
        }
        sheet = m_Workerbook.getSheetAt(0);
    } catch (FileNotFoundException ex) {

        Logger.getLogger(ExcelControl.class.getName()).log(Level.SEVERE, "EXCEL?", ex);
    } catch (IOException | InvalidFormatException ex) {
        Logger.getLogger(ExcelControl.class.getName()).log(Level.SEVERE, "?EXCEL", ex);
        throw ex;
    }
    return sheet;
}

From source file:com.beyondb.io.ExcelReader.java

/**
 * ?Excel/*from  ww w. j av  a2s  .co  m*/
 * @param InputStream
 * @return String 
 */
public String[] readExcelTitle(InputStream is) {
    try {
        fs = new POIFSFileSystem(is);
        wb = new HSSFWorkbook(fs);
    } catch (IOException e) {
        e.printStackTrace();
    }
    sheet = wb.getSheetAt(0);
    row = sheet.getRow(0);
    // 
    int colNum = row.getPhysicalNumberOfCells();
    System.out.println("colNum:" + colNum);
    String[] title = new String[colNum];
    for (int i = 0; i < colNum; i++) {
        //title[i] = getStringCellValue(row.getCell((short) i));
        title[i] = getCellFormatValue(row.getCell(i));
    }
    return title;
}

From source file:com.beyondb.io.ExcelReader.java

/**
 * ?Excel?/*from w  w  w .  jav a 2 s.  c  om*/
 * @param InputStream
 * @return Map ???Map
 */
public Map<Integer, String> readExcelContent(InputStream is) {
    Map<Integer, String> content = new HashMap<Integer, String>();
    String str = "";
    try {
        fs = new POIFSFileSystem(is);
        wb = new HSSFWorkbook(fs);
    } catch (IOException e) {
        e.printStackTrace();
    }
    sheet = wb.getSheetAt(0);
    // 
    int rowNum = sheet.getLastRowNum();
    row = sheet.getRow(0);
    int colNum = row.getPhysicalNumberOfCells();
    // ,
    for (int i = 1; i <= rowNum; i++) {
        row = sheet.getRow(i);
        int j = 0;
        while (j < colNum) {
            // ???"-"??Stringreplace()?
            // ????javabean?javabean
            // str += getStringCellValue(row.getCell((short) j)).trim() +
            // "-";
            str += getCellFormatValue(row.getCell(j)).trim() + "    ";
            j++;
        }
        content.put(i, str);
        str = "";
    }
    return content;
}

From source file:com.bluecubs.xinco.index.filetypes.XincoIndexMicrosoftExcel.java

License:Apache License

public String getFileContentString(File f) {
    int i, j, j2, k;
    short k2;/*from w  ww  .j av  a2  s. co  m*/
    HSSFWorkbook wb = null;
    HSSFSheet sheet = null;
    HSSFRow row = null;
    HSSFCell cell = null;
    InputStream is = null;
    String cell_string = "";
    try {
        is = new FileInputStream(f);
        POIFSFileSystem fs = new POIFSFileSystem(is);
        wb = new HSSFWorkbook(fs);
        for (i = 0; i < wb.getNumberOfSheets(); i++) {
            sheet = wb.getSheetAt(i);
            j2 = 0;
            for (j = 0; j < sheet.getPhysicalNumberOfRows(); j++) {
                while ((row = sheet.getRow(j2)) == null) {
                    j2++;
                }
                j2++;
                k2 = 0;
                for (k = 0; k < row.getPhysicalNumberOfCells(); k++) {
                    while ((cell = row.getCell(k2)) == null) {
                        k2++;
                    }
                    k2++;
                    switch (cell.getCellType()) {
                    case HSSFCell.CELL_TYPE_FORMULA:
                        break;
                    case HSSFCell.CELL_TYPE_NUMERIC:
                        cell_string = cell_string + cell.getNumericCellValue() + "\t";
                        break;
                    case HSSFCell.CELL_TYPE_STRING:
                        cell_string = cell_string + cell.getStringCellValue() + "\t";
                        break;
                    default:
                    }

                }
                cell_string = cell_string + "\n";
            }
            cell_string = cell_string + "\n\n\n";
        }
        is.close();
    } catch (Exception fe) {
        cell_string = null;
        if (is != null) {
            try {
                is.close();
            } catch (Exception ise) {
            }
        }
    }
    return cell_string;
}