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:guineu.database.intro.impl.WriteFile.java

License:Open Source License

void WriteExcelExpressionSetDataset(Dataset dataset, String path, SimpleParameterSet parameters) {
    FileOutputStream fileOut = null;
    try {//from  w  w  w  .  ja  v  a  2 s.  co m
        // Prepares sheet
        HSSFSheet sheet;
        try {
            FileInputStream fileIn = new FileInputStream(path);
            POIFSFileSystem fs = new POIFSFileSystem(fileIn);
            wb = new HSSFWorkbook(fs);
            int NumberOfSheets = wb.getNumberOfSheets();
            sheet = wb.createSheet(String.valueOf(NumberOfSheets));
        } catch (Exception exception) {
            wb = new HSSFWorkbook();
            sheet = wb.createSheet("Normalized");
        }
        HSSFRow row = sheet.getRow(0);
        if (row == null) {
            row = sheet.createRow(0);
        }

        ExpressionDataColumnName elementsObjects[] = parameters
                .getParameter(SaveExpressionParameters.exportExpression).getValue();

        // Writes head
        int fieldsNumber = elementsObjects.length;
        int cont = 0;
        for (ExpressionDataColumnName p : elementsObjects) {
            this.setCell(row, cont++, p.getColumnName(), null);
        }
        int c = fieldsNumber;
        for (String experimentName : dataset.getAllColumnNames()) {
            this.setCell(row, c++, experimentName, null);
        }

        // Writes content
        for (int i = 0; i < dataset.getNumberRows(); i++) {
            SimplePeakListRowExpression lipid = (SimplePeakListRowExpression) dataset.getRow(i);
            row = sheet.getRow(i + 1);
            if (row == null) {
                row = sheet.createRow(i + 1);
            }

            cont = 0;
            for (ExpressionDataColumnName p : elementsObjects) {
                try {
                    if (cont == 0) {
                        System.out.println(lipid.getVar(p.getGetFunctionName()));
                    }
                    this.setCell(row, cont++, lipid.getVar(p.getGetFunctionName()), null);
                } catch (Exception ee) {
                }

            }
            c = fieldsNumber;
            for (String experimentName : dataset.getAllColumnNames()) {
                this.setCell(row, c++, lipid.getPeak(experimentName), null);
            }
        }
        //Writes the output to a file
        fileOut = new FileOutputStream(path);
        wb.write(fileOut);
        fileOut.close();
    } catch (Exception exception) {
    }
}

From source file:guineu.modules.configuration.parameters.ParameterDialog.java

License:Open Source License

private HSSFWorkbook openExcelFile(String file_name) throws IOException {
    FileInputStream fileIn = null;
    try {//from w w  w.j av  a2 s.c  o  m
        HSSFWorkbook wb;
        POIFSFileSystem fs;
        fileIn = new FileInputStream(file_name);
        fs = new POIFSFileSystem(fileIn);
        wb = new HSSFWorkbook(fs);
        return wb;
    } finally {
        if (fileIn != null) {
            fileIn.close();
        }
    }
}

From source file:hr.restart.sisfun.frmReportxList.java

License:Apache License

void OKPress() {
    if (jf.showOpenDialog(this.getWindow()) != jf.APPROVE_OPTION)
        return;/*from   www  .  ja  v a2  s.c  o  m*/
    File sel = jf.getSelectedFile();
    if (!sel.exists()) {
        JOptionPane.showMessageDialog(this.getWindow(), "Datoteka ne postoji!", "Greka",
                JOptionPane.ERROR_MESSAGE);
        return;
    }
    if (!sel.canRead()) {
        JOptionPane.showMessageDialog(this.getWindow(), "Datoteka se ne moe otvoriti!", "Greka",
                JOptionPane.ERROR_MESSAGE);
        return;
    }

    if (reps.getString("TIP").equals("G")) {
        if (!dlg.show(this.getWindow(), pan, reps.getString("NAZREP")))
            return;
    }

    try {
        FileInputStream fis = new FileInputStream(sel);
        try {
            POIFSFileSystem fs = new POIFSFileSystem(fis);
            HSSFWorkbook wb = new HSSFWorkbook(fs);
            if (fillData(sel, wb)) {
                JOptionPane.showMessageDialog(this.getWindow(), "Izvjetaj zavren.", "Excel izvjetaj",
                        JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (IOException e) {
            JOptionPane.showMessageDialog(this.getWindow(), "Greka kod otvaranja datoteke!", "Greka",
                    JOptionPane.ERROR_MESSAGE);
            return;
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this.getWindow(), e.getMessage(), "Greka",
                    JOptionPane.ERROR_MESSAGE);
            return;
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                //
            }
        }
    } catch (FileNotFoundException e) {
        JOptionPane.showMessageDialog(this.getWindow(), "Datoteka ne postoji!", "Greka",
                JOptionPane.ERROR_MESSAGE);
        return;
    }
}

From source file:Importers.ExcelImporter.java

License:Apache License

@Override
public DefaultMutableTreeNode readFile(File file) {
    System.out.println("==ExcelImporter=readFile: " + file.getAbsolutePath());
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("vulns");
    try {/*w w  w .j a  v a  2 s .c  o  m*/

        POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
        HSSFWorkbook wb = new HSSFWorkbook(fs);
        HSSFSheet sheet = wb.getSheetAt(0);
        HSSFRow row;
        HSSFCell cell;

        int rows; // No of rows
        rows = sheet.getPhysicalNumberOfRows();

        int cols = 0; // No of columns
        int tmp = 0;

        // This trick ensures that we get the data properly even if it doesn't start from first few rows
        for (int i = 0; i < 10 || i < rows; i++) {
            row = sheet.getRow(i);
            if (row != null) {
                tmp = sheet.getRow(i).getPhysicalNumberOfCells();
                if (tmp > cols) {
                    cols = tmp;
                }
            }
        }

        for (int r = 1; r < rows; r++) {
            row = sheet.getRow(r);
            if (row != null) {

                // Create a new vuln
                Vulnerability vuln = new Vulnerability();
                vuln.setTitle("NEW");
                vuln.setIs_custom_risk(true);
                vuln.setRisk_category("None");

                for (int c = 0; c < cols; c++) {
                    cell = row.getCell(c);
                    if (cell != null) {
                        // Your code here
                        String value = cell.getStringCellValue();
                        switch (c) {
                        case 1:// title
                            vuln.setTitle(value);
                            break;
                        case 2: // Risk
                            CellStyle style = cell.getCellStyle();
                            short colorIdx = style.getFillForegroundColor();
                            HSSFPalette palette = ((HSSFWorkbook) wb).getCustomPalette();
                            HSSFColor color = palette.getColor(colorIdx);
                            String cc = color.getHexString();
                            System.out.println(cc);
                            if (cc.equalsIgnoreCase("8080:8080:0")) {
                                vuln.setRisk_category("Critical");
                            } else if (cc.equalsIgnoreCase("FFFF:0:0")) {
                                vuln.setRisk_category("High");
                            } else if (cc.equalsIgnoreCase("FFFF:6666:0")) {
                                vuln.setRisk_category("Medium");
                            } else if (cc.equalsIgnoreCase("F2F2:ECEC:0")) {
                                vuln.setRisk_category("Low");
                            } else if (cc.equalsIgnoreCase("0:0:FFFF")) {
                                vuln.setRisk_category("Info");
                            }

                            break;
                        case 3:// cvss string
                            System.out.println(value);
                            if (value.equalsIgnoreCase("No CVSS Vector")) {
                                vuln.setIs_custom_risk(true);
                            } else {
                                vuln.setIs_custom_risk(false);
                                vuln.setCvss_vector_string("CVSS2#" + value);
                            }
                            break;
                        case 4://Description
                            vuln.setDescription(value);
                            break;
                        case 5://Recommendation
                            vuln.setRecommendation(value);
                            break;
                        case 6://Affected Hosts
                            try {
                                String[] lines = value.split("\n");

                                for (String line : lines) {
                                    String[] bits = line.split(" ");
                                    Host host = new Host();
                                    host.setIp_address(bits[0]);
                                    String portprotocol = bits[2];
                                    host.setPortnumber(portprotocol.split("/")[0]);
                                    host.setProtocol(portprotocol.split("/")[1]);
                                    vuln.addAffectedHost(host);
                                }
                            } catch (Exception ex) {
                                ;
                            }
                            break;
                        }

                    }
                }
                System.out.println(vuln);

                root.add(new DefaultMutableTreeNode(vuln));
            }
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return root;
}

From source file:include.excel_import.Outter.java

License:Open Source License

public void reparedOut() throws Exception {
    if (fileName == null)
        throw new Exception("Exception: File should be asigned!");
    try {/*ww  w .j  av a 2s  .  co  m*/
        FileInputStream fileinputstream = new FileInputStream(fileName);
        POIFSFileSystem poifsfilesystem = new POIFSFileSystem(fileinputstream);
        wb = new HSSFWorkbook(poifsfilesystem);
    } catch (Exception exception) {
        exception.printStackTrace();
    }
}

From source file:include.excel_import.XlsInfo.java

License:Open Source License

public XlsInfo(String fileName) {
    String filename = getPath() + "/conf/excel_import/" + fileName;
    try {//from w w  w . j  av a 2 s . c  o m
        InputStream input = new FileInputStream(filename);
        POIFSFileSystem fs = new POIFSFileSystem(input);
        wb = new HSSFWorkbook(fs);
        df = wb.createDataFormat();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:include.excel_import.XlsValidator.java

License:Open Source License

/**
 * excel??/*  w w w .  ja v a 2 s  .c  o m*/
 * ?
 * @param fileName ??
 */
public void setFile(String fileName) throws Exception {
    this.fileName = getPath() + "/conf/excel_import/" + fileName;
    if (fileName == null) {
        throw new Exception("Exception: ?Excel??");
    }
    try {
        InputStream input = new FileInputStream(this.fileName);
        POIFSFileSystem fs = new POIFSFileSystem(input);
        wb = new HSSFWorkbook(fs);
        xlsInfo = new XlsInfo(wb);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:info.jtrac.domain.ExcelFile.java

License:Apache License

public ExcelFile(InputStream is) {
    POIFSFileSystem fs = null;//from w  w w .j ava2 s . com
    HSSFWorkbook wb = null;
    try {
        fs = new POIFSFileSystem(is);
        wb = new HSSFWorkbook(fs);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    HSSFSheet sheet = wb.getSheetAt(0);
    HSSFRow r = null;
    HSSFCell c = null;
    int row = 0;
    int col = 0;
    columns = new ArrayList<Column>();
    //========================== HEADER ====================================
    // column headings are important, this routine assumes that the first
    // row is a header row and that reaching an empty cell means end of data
    r = sheet.getRow(row);
    while (true) {
        c = r.getCell((short) col);
        if (c == null) {
            break;
        }
        String value = c.getStringCellValue();
        if (value == null || value.trim().length() == 0) {
            break;
        }
        Column column = new Column(value.trim());
        columns.add(column);
        col++;
    }
    //============================ DATA ====================================
    rows = new ArrayList<List<Cell>>();
    while (true) {
        row++;
        r = sheet.getRow(row);
        if (r == null) {
            break;
        }
        List<Cell> rowData = new ArrayList<>(columns.size());
        boolean isEmptyRow = true;
        for (col = 0; col < columns.size(); col++) {
            c = r.getCell((short) col);
            Object value = null;
            switch (c.getCellType()) {
            case (HSSFCell.CELL_TYPE_STRING):
                value = c.getStringCellValue();
                break;
            case (HSSFCell.CELL_TYPE_NUMERIC):
                // value = c.getDateCellValue();
                value = c.getNumericCellValue();
                break;
            case (HSSFCell.CELL_TYPE_BLANK):
                break;
            default: // do nothing
            }
            if (value != null && value.toString().length() > 0) {
                isEmptyRow = false;
                rowData.add(new Cell(value));
            } else {
                rowData.add(new Cell(null));
            }
        }
        if (isEmptyRow) {
            break;
        }
        rows.add(rowData);
    }
}

From source file:info.vancauwenberge.filedriver.filereader.xls.XlsFileReader.java

License:Mozilla Public License

public void openFile(File f) throws ReadException {
    trace.trace("Reading " + f.getAbsolutePath());
    //If the file is existing, open and read it
    try {//  w w  w.j a va 2  s .  c o m
        FileInputStream fin = new FileInputStream(f);
        POIFSFileSystem poifs = new POIFSFileSystem(fin);
        wb = new HSSFWorkbook(poifs);
    } catch (IOException e) {
        throw new ReadException("Error while trying to read file " + f.getAbsolutePath(), e);
    }
    currentSheet = wb.getSheet(sheetName);
    //If we do not have a sheet with the given name, throw exception.
    if (currentSheet == null)
        throw new ReadException("No sheet with name " + sheetName + " found in file " + f.getAbsolutePath(),
                null);
    nextRowNumber = currentSheet.getFirstRowNum();

    //If we have a aheader row, read it to get the actual schema
    if (hasHeader) {
        if (useHeaderNames) {
            HSSFRow row = currentSheet.getRow(nextRowNumber);
            //Last cell num is zero based => +1
            String[] fields = new String[row.getLastCellNum()];
            trace.trace("Number of fields:" + fields.length);
            Iterator<Cell> iter = row.cellIterator();
            while (iter.hasNext()) {
                HSSFCell element = (HSSFCell) iter.next();
                String value = element.getStringCellValue();
                fields[element.getCellNum()] = value;
            }
            //We might have some nulls in the array. Default them.
            for (int i = 0; i < fields.length; i++) {
                String string = fields[i];
                if (string == null)
                    fields[i] = "_Unknown_" + i + "_";
            }
            schema = fields;
            nextRowNumber++;
        }
    }
}

From source file:intelligentWebAlgorithms.util.parsing.msword.MSWordDocumentParser.java

License:Apache License

public HWPFDocument poiReadDocument(String fileName) {

    POIFSFileSystem fs = null;/*w  w w  .j  ava2 s  .c o  m*/
    HWPFDocument hwpfDoc = null;
    try {
        fs = new POIFSFileSystem(new FileInputStream(fileName));
        hwpfDoc = new HWPFDocument(fs);

        /** Read the content **/

        String text = hwpfDoc.getDocumentText();

        wordDoc.setDocumentTitle(getTitle(text));
        wordDoc.setText(text);
        wordDoc.setContent(text);

        //          P.hline();
        //          P.println(getTitle(text));
        //          printProperties(hwpfDoc.getDocProperties());

        // readParagraphs(hwpfDoc);

    } catch (Exception e) {
        e.printStackTrace();
    }
    return hwpfDoc;
}