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:gov.nih.nci.evs.app.neopl.XLSXMetadataUtils.java

License:Open Source License

public static String getSummaryData(String filename, String key) {
    String value = null;/*from   w  ww .j  a va2  s .co m*/
    FileInputStream stream = null;
    try {
        stream = new FileInputStream(new File(filename));
        POIFSFileSystem poifs = null;
        try {
            poifs = new POIFSFileSystem(stream);
        } catch (Exception e) {
            stream.close();
            return getPOISummaryData(filename, key);
        }
        DirectoryEntry dir = poifs.getRoot();
        DocumentEntry siEntry = (DocumentEntry) dir.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
        if (siEntry != null) {
            DocumentInputStream dis = new DocumentInputStream(siEntry);
            PropertySet ps = new PropertySet(dis);
            SummaryInformation si = new SummaryInformation(ps);

            if (key.compareTo(SUMMARY_DATA_AUTHOR) == 0) {
                value = si.getAuthor();
            } else if (key.compareTo(SUMMARY_DATA_KEYWORDS) == 0) {
                value = si.getKeywords();
            } else if (key.compareTo(SUMMARY_DATA_TITLE) == 0) {
                value = si.getTitle();
            } else if (key.compareTo(SUMMARY_DATA_SUBJECT) == 0) {
                value = si.getSubject();
            }
        }
    } catch (Exception ex) {
        ex.getStackTrace();
    } finally {
        try {
            stream.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    return value;
}

From source file:gov.nih.nci.evs.app.neopl.XLSXMetadataUtils.java

License:Open Source License

public static void setSummaryData(String filename, String[] keys, String[] values) {
    String size = getFileSize(filename);
    FileInputStream stream = null;
    try {//w ww  .j a v a  2s  . com
        stream = new FileInputStream(new File(filename));
        POIFSFileSystem poifs = null;
        boolean passed = false;
        try {
            poifs = new POIFSFileSystem(stream);
            passed = true;
        } catch (Exception e) {
            passed = false;
        }
        if (!passed) {
            setPOISummaryData(filename, keys, values);
            stream.close();
            return;
        }

        HSSFWorkbook workbook = null;
        SummaryInformation summaryInfo = null;
        FileInputStream fis = null;
        try {
            System.out.println(filename);
            fis = new FileInputStream(filename);

            workbook = new HSSFWorkbook(fis);
            summaryInfo = workbook.getSummaryInformation();
            if (summaryInfo == null) {
                workbook.createInformationProperties();
                summaryInfo = workbook.getSummaryInformation();

                for (int i = 0; i < keys.length; i++) {
                    String key = keys[i];
                    String value = values[i];

                    System.out.println(key + " -> " + value);

                    if (key.compareTo(SUMMARY_DATA_AUTHOR) == 0) {
                        summaryInfo.setAuthor(value);
                    } else if (key.compareTo(SUMMARY_DATA_KEYWORDS) == 0) {
                        summaryInfo.setKeywords(value);
                    } else if (key.compareTo(SUMMARY_DATA_TITLE) == 0) {
                        summaryInfo.setTitle(value);
                    } else if (key.compareTo(SUMMARY_DATA_SUBJECT) == 0) {
                        summaryInfo.setSubject(value);
                    }
                }

            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (Exception ex) {

            }
        }
        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream(new File(filename));
            workbook.write(fos);
            fos.flush();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            fos.close();
        }

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

From source file:gov.nih.nci.evs.app.neopl.XLSXMetadataUtils.java

License:Open Source License

public static String getAuthor(File file) {
    String author = null;//from  w  w  w  . ja va 2 s  .  c o  m
    try {
        FileInputStream stream = new FileInputStream(file);
        POIFSFileSystem poifs = null;
        try {
            poifs = new POIFSFileSystem(stream);
        } catch (Exception e) {
            stream.close();
            return getCreator(file);
        }
        DirectoryEntry dir = null;
        try {
            dir = poifs.getRoot();
        } catch (Exception ex) {
            System.out.println("DirectoryEntry is NULL???");
            return null;
        }
        DocumentEntry siEntry = (DocumentEntry) dir.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
        if (siEntry != null) {
            DocumentInputStream dis = new DocumentInputStream(siEntry);
            PropertySet ps = new PropertySet(dis);
            SummaryInformation si = new SummaryInformation(ps);
            author = si.getAuthor();
        }
        stream.close();
    } catch (Exception ex) {
        ex.getStackTrace();
    }
    return author;
}

From source file:gov.nih.nci.evs.app.neopl.XLSXMetadataUtils.java

License:Open Source License

public static void setAuthor(String filename, String author) {
    try {/*from   w w w  .  j  a  v a2  s  .  com*/
        FileInputStream stream = new FileInputStream(new File(filename));
        POIFSFileSystem poifs = new POIFSFileSystem(stream);
        DirectoryEntry dir = poifs.getRoot();

        System.out.println("SummaryInformation.DEFAULT_STREAM_NAME: " + SummaryInformation.DEFAULT_STREAM_NAME);

        DocumentEntry siEntry = (DocumentEntry) dir.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
        DocumentInputStream dis = new DocumentInputStream(siEntry);
        PropertySet ps = new PropertySet(dis);
        SummaryInformation si = new SummaryInformation(ps);

        System.out.println("SummaryInformation setAuthor: " + author);

        si.setAuthor(author);

        OutputStream outStream = null;
        outStream = new FileOutputStream(new File(filename));
        byte[] buffer = new byte[1024];
        int length;

        while ((length = stream.read(buffer)) > 0) {
            outStream.write(buffer, 0, length);
        }
        outStream.close();
        stream.close();

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

From source file:gov.nih.nci.evs.app.neopl.XLSXMetadataUtils.java

License:Open Source License

public static void setAuthor(File file, String author) {
    try {// ww  w  .  j a v a  2  s  . c  o  m
        FileInputStream stream = new FileInputStream(file);
        POIFSFileSystem poifs = null;
        try {
            poifs = new POIFSFileSystem(stream);
        } catch (Exception e) {
            stream.close();
            setCreator(file, author);
            return;
        }
        DirectoryEntry dir = poifs.getRoot();
        DocumentEntry siEntry = (DocumentEntry) dir.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
        if (siEntry != null) {
            DocumentInputStream dis = new DocumentInputStream(siEntry);
            PropertySet ps = new PropertySet(dis);
            SummaryInformation si = new SummaryInformation(ps);
            si.setAuthor(author);
        }
        stream.close();
    } catch (Exception ex) {
        ex.getStackTrace();
    }
}

From source file:gr.abiss.calipso.domain.ExcelFile.java

License:Open Source License

public ExcelFile(InputStream is) {
    POIFSFileSystem fs = null;/*from   ww  w. j a  v  a 2 s  .co m*/
    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 ====================================
    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 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(null);
            }
        }
        if (isEmptyRow) {
            break;
        }
        rows.add(rowData);
    }
}

From source file:guineu.data.parser.impl.ParserXLS.java

License:Open Source License

public HSSFWorkbook openExcel(String file_name) throws IOException {
    FileInputStream fileIn = null;
    try {//from w  w w  .j a va  2 s .co  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:guineu.database.intro.impl.WriteFile.java

License:Open Source License

/**
 * Writes the LC-MS data set into an excel file.
 *
 * @param dataset LC-MS data set//  w  ww.  j  ava 2 s  .c  o  m
 * @param path Path where the new file will be created
 * @param parameters Parameters for saving the file (columns saved in the new file)
 */
public void WriteExcelFileLCMS(Dataset dataset, String path, SimpleParameterSet parameters) {
    FileOutputStream fileOut = null;
    try {
        // 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);
        }

        LCMSColumnName elementsObjects[] = parameters.getParameter(SaveLCMSParameters.exportLCMS).getValue();

        // Writes head
        int fieldsNumber = elementsObjects.length;
        int cont = 0;
        for (LCMSColumnName 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++) {
            SimplePeakListRowLCMS lipid = (SimplePeakListRowLCMS) dataset.getRow(i);
            row = sheet.getRow(i + 1);
            if (row == null) {
                row = sheet.createRow(i + 1);
            }

            cont = 0;
            for (LCMSColumnName p : elementsObjects) {
                try {
                    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.database.intro.impl.WriteFile.java

License:Open Source License

/**
 * Writes the basic data set into an excel file.
 *
 * @param dataset basic data set/*from   w ww.  j av  a2 s .c  om*/
 * @param path Path where the new file will be created
 */
public void WriteXLSFileBasicDataset(Dataset dataset, String path) {
    FileOutputStream fileOut = null;
    try {
        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("Page 1");
        }
        HSSFRow row = sheet.getRow(0);
        if (row == null) {
            row = sheet.createRow(0);
        }
        int cont = 0;
        for (String experimentName : dataset.getAllColumnNames()) {
            this.setCell(row, cont++, experimentName, null);

        }

        HSSFPalette palette = wb.getCustomPalette();
        Color[] colors = dataset.getRowColor();
        if (colors.length > 0) {
            for (int i = 0; i < dataset.getNumberRows(); i++) {
                palette.setColorAtIndex((short) 0x12, (byte) colors[i].getRed(), (byte) colors[i].getGreen(),
                        (byte) colors[i].getBlue());
                HSSFColor mycolor = palette.getColor((short) 0x12); //unmodified
                SimplePeakListRowOther lipid = (SimplePeakListRowOther) dataset.getRow(i);

                row = sheet.getRow(i + 1);
                if (row == null) {
                    row = sheet.createRow(i + 1);
                }
                int c = 0;
                for (String experimentName : dataset.getAllColumnNames()) {
                    if (lipid.getPeak(experimentName) == null) {
                        this.setCell(row, c++, "", mycolor);
                    } else {
                        this.setCell(row, c++, lipid.getPeak(experimentName), mycolor);
                    }
                }

            }
        } else {

            List<String> names = dataset.getAllColumnNames();
            for (int i = 0; i < dataset.getNumberRows(); i++) {
                SimplePeakListRowOther lipid = (SimplePeakListRowOther) dataset.getRow(i);
                row = sheet.getRow(i + 1);
                if (row == null) {
                    row = sheet.createRow(i + 1);
                }
                for (int j = 0; j < names.size(); j++) {
                    Color c = dataset.getCellColor(i, j + 1);
                    HSSFColor mycolor = null;
                    if (c != null) {
                        mycolor = palette.findColor((byte) c.getRed(), (byte) c.getGreen(), (byte) c.getBlue());
                    }
                    if (lipid.getPeak(names.get(j)) == null) {
                        this.setCell(row, j, "", null);
                    } else {
                        this.setCell(row, j, lipid.getPeak(names.get(j)), mycolor);
                    }
                }
            }
        }

        //Write the output to a file
        fileOut = new FileOutputStream(path);
        wb.write(fileOut);
        fileOut.close();
    } catch (Exception exception) {
    }
}

From source file:guineu.database.intro.impl.WriteFile.java

License:Open Source License

/**
 * Writes the GCxGC-MS data set into an excel file.
 *
 * @param dataset GCxGC-MS data set/*  w w  w  . j  a  v  a2 s.co  m*/
 * @param path Path where the new file will be created
 * @param parameters Parameters for saving the file (columns saved in the new file)
 */
public void WriteExcelFileGCGC(Dataset dataset, String path, SimpleParameterSet parameters) {
    FileOutputStream fileOut = null;
    try {
        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);
        }

        GCGCColumnName elementsObjects[] = parameters.getParameter(SaveGCGCParameters.exportGCGC).getValue();

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

        // Write content
        for (int i = 0; i < dataset.getNumberRows(); i++) {
            SimplePeakListRowGCGC metabolite = (SimplePeakListRowGCGC) dataset.getRow(i);
            row = sheet.getRow(i + 1);
            if (row == null) {
                row = sheet.createRow(i + 1);
            }
            cont = 0;

            for (GCGCColumnName p : elementsObjects) {
                try {
                    this.setCell(row, cont++, metabolite.getVar(p.getGetFunctionName()), null);
                } catch (Exception ee) {
                }

            }
            c = fieldsNumber;
            for (String experimentName : dataset.getAllColumnNames()) {
                try {
                    this.setCell(row, c++, metabolite.getPeak(experimentName), null);
                } catch (Exception e) {
                    this.setCell(row, c, "NA", null);
                }
            }
        }
        //Write the output to a file
        fileOut = new FileOutputStream(path);
        wb.write(fileOut);
        fileOut.close();
    } catch (Exception e) {
        System.out.println("Inoracle2.java --> WriteExcelFileGCGC() " + e);
    }
}