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:gda.hrpd.data.ExcelReader.java

License:Open Source License

/**
 * open or reopen the specified spreadsheet file for work sheet 0.
 * //from w  w  w. ja  va2s.  co  m
 * @param filename
 */
public void openSpreadsheet(String filename) {
    if (filename == null) {
        filename = this.filename;
    }
    if (!filename.equalsIgnoreCase(this.filename)) {
        close();
        try {
            fin = new FileInputStream(filename);
        } catch (FileNotFoundException e) {
            logger.warn("Cannot find sample information file {}.", filename);
            JythonServerFacade.getInstance().print("please specify the URL for the sample information file.");
        }
    }
    if (fin != null) {

        try {
            fs = new POIFSFileSystem(fin);
            wb = new HSSFWorkbook(fs);
            sheet = wb.getSheetAt(0);
        } catch (IOException e) {
            logger.error("Error opening spreadsheet", e);
        }
    }
}

From source file:gda.hrpd.data.ExcelWorkbook.java

License:Open Source License

/**
 * Constructor - opens a file input stream object, creates a workbook for accessing to the content of the Excel
 * file.//from ww w. j a v a2s  . com
 * 
 * @param filename
 * @throws InstantiationException
 */
public ExcelWorkbook(String filename) throws InstantiationException {
    if (filename != null) {
        this.filename = filename;
    } else {
        throw new InstantiationException("Filename is null");
    }
    if (fileInput != null) {
        // close existing file input stream to ensure only one stream available
        try {
            fileInput.close();
        } catch (IOException e) {
            throw new InstantiationException("Can not close existing file input stream");
        }
    }

    // check file permission
    File file = new File(this.filename);

    readable = file.canRead();
    if (readable) {
        logger.info("GDA can read from the file: {}", this.filename);
    } else {
        logger.warn(
                "GDA cannot read from the file: {}. Please make sure file permission is correct for GDA to read.",
                this.filename);
    }

    writeable = file.canWrite();
    if (writeable) {
        logger.info("GDA can write to the file: {}", this.filename);
    } else {
        logger.warn(
                "GDA cannot write to the file: {}. Please make sure file permission is correct for GDA to write.",
                this.filename);
    }

    // open a new file input stream
    try {
        fileInput = new FileInputStream(this.filename);
    } catch (FileNotFoundException e) {
        logger.error("Cannot find sample information file {}.", filename);
        JythonServerFacade.getInstance().print("please specify the URL for file.");
        throw new InstantiationException(e.getMessage());
    }
    // open the workbook
    if (fileInput != null) {
        try {
            fs = new POIFSFileSystem(fileInput);
            wb = new HSSFWorkbook(fs);
        } catch (IOException e) {
            logger.error("failed to open Excel file {}.", this.filename);
            throw new InstantiationException("can not open Excel file: " + this.filename);
        }
    }
}

From source file:gda.hrpd.data.HSSF.java

License:Apache License

/**
 * Constructor HSSF - creates an HSSFStream from an InputStream. The HSSFStream reads in the records allowing
 * modification.//from  w w  w  . j a  va 2s . co  m
 *
 * @param filename
 * @exception IOException
 */

public HSSF(String filename) throws IOException {
    this.filename = filename;
    POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filename));

    hssfworkbook = new HSSFWorkbook(fs);

    // records = RecordFactory.createRecords(stream);
}

From source file:gda.hrpd.data.HSSF.java

License:Apache License

/**
 * Constructor HSSF - takes in file - attempts to read it then reconstruct it
 *
 * @param infile// w  w w  .  ja  v a  2  s  .  c om
 * @param outfile
 * @param write
 * @exception IOException
 */

public HSSF(String infile, @SuppressWarnings("unused") String outfile,
        @SuppressWarnings("unused") boolean write) throws IOException {
    this.filename = infile;
    POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filename));

    hssfworkbook = new HSSFWorkbook(fs);

    // HSSFWorkbook book = hssfstream.getWorkbook();
}

From source file:gda.hrpd.data.SampleExperimentSummary.java

License:Open Source License

/**
 * @throws InstantiationException/*from   ww  w  .  j  a va  2  s  .  com*/
 */
public SampleExperimentSummary() throws InstantiationException {
    // check if the data directory has been defined
    dataDir = PathConstructor.createFromDefaultProperty();

    if (this.dataDir == null) {
        // this java property is compulsory - stop the scan
        String error = "java property gda.data.scan.datawriter.datadir not defined - cannot create a new data file";
        logger.error(error);
        throw new InstantiationException(error);
    }
    String sampleInfoFile = LocalProperties.get("gda.hrpd.data.sample.info", "Sample.xls");
    // check if the sample information file is available
    filename = dataDir + File.separator + sampleInfoFile;

    try {
        fin = new FileInputStream(filename);
    } catch (FileNotFoundException e) {
        logger.warn("Cannot find sample information file {}.", filename);
    }

    if (fin != null) {

        try {
            fs = new POIFSFileSystem(fin);
            wb = new HSSFWorkbook(fs);
            sheet = wb.getSheetAt(1);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:gov.nih.nci.cabig.caaers.dataimport.AgentSpecificTermsImporter.java

License:BSD License

public Map<String, Object> importFile() throws Exception {

    POIFSFileSystem poifs;/*from ww w  .j a v  a2s. c o  m*/
    HSSFWorkbook wb;
    HSSFSheet sh = null;

    boolean isExcel = file.getName().endsWith(".xls");
    boolean isCSV = file.getName().endsWith(".csv");

    Map<String, Object> results = new HashMap<String, Object>();
    int rowCount = 0;
    int columnsCount = 0;
    Map<String, Agent> agents = new HashMap<String, Agent>();
    Map<String, Agent> missingAgents = new HashMap<String, Agent>();
    Set<String> missingTerms = new HashSet<String>();
    Map<String, String> asaelCache = new HashMap<String, String>();
    int asael;

    // wipe out the table
    agentSpecificTermDao.deleteAll();
    studyDao.deleteAllExpectedTerms();
    // if (true) return null;

    // get needed headers
    if (isExcel) {
        poifs = new POIFSFileSystem(new FileInputStream(file));
        wb = new HSSFWorkbook(poifs);
        sh = wb.getSheetAt(0);
        rowCount = sh.getLastRowNum();
        columnsCount = sh.getRow(0).getLastCellNum();

        for (byte i = 0; i < columnsCount; i++) {
            HSSFCell cell = sh.getRow(0).getCell(i);
            if (headers.containsKey(cell.getStringCellValue())) {
                headers.remove(cell.getStringCellValue());
                headers.put(cell.getStringCellValue(), Short.valueOf(i));
            }
        }
    }

    InputStream ir = null;
    Reader r = null;
    BufferedReader br = null;

    if (isCSV) {
        // readLines
        rowCount = 0;
        ir = new FileInputStream(file);
        r = new InputStreamReader(ir);
        br = new BufferedReader(r);
        String s = br.readLine();
        while (s != null) {
            if (rowCount == 0) {
                String[] _s = s.split("[\\|]{1}");
                for (byte j = 0; j < _s.length; j++) {
                    // System.out.println(_s[j]);
                    if (headers.containsKey(_s[j])) {
                        headers.remove(_s[j]);
                        headers.put(_s[j], Short.valueOf(j));
                    }
                }
            }
            rowCount++;
            s = br.readLine();
        }
        br.close();
        r.close();
        ir.close();

        ir = new FileInputStream(file);
        r = new InputStreamReader(ir);
        br = new BufferedReader(r);
    }

    /*
            System.out.println(rowCount);
            for (Map.Entry e : headers.entrySet()) {
    System.out.println(e.getKey() + "=>" + e.getValue());
            }
    */

    agents.clear();
    missingTerms.clear();
    missingAgents.clear();
    asael = 0;
    int duplicateAgentTerms = 0;

    //
    String nsc = "";
    String ctcae_category = "";
    String ctcae_version = "0.0";
    String ae_term = "";
    String other_toxicity = "";

    // Loading ASAE list
    // if (true) {  return null; }

    int i = 1;
    while (i <= rowCount) {

        nsc = "";

        if (isExcel) {
            HSSFRow row = sh.getRow(i);
            if (row != null) {
                nsc = getCellData("", i, row.getCell((short) headers.get("NSC")));
                ctcae_category = getCellData("", i, row.getCell((short) headers.get("CTCAE_CATEGORY")));
                ctcae_version = getCellData("", i, row.getCell((short) headers.get("CTCAE_VERSION")));
                ae_term = getCellData("", i, row.getCell((short) headers.get("AE_TERM")));
                other_toxicity = getCellData("", i, row.getCell((short) headers.get("OTHER_TOXICITY")));
            }
        } else {
            String s;
            s = br.readLine();
            if (s != null) {
                String[] _s = s.split("[\\|]{1}");
                if (i > 1 && _s.length > 1) {
                    nsc = _s[headers.get("NSC")];
                    ctcae_category = _s[headers.get("CTCAE_CATEGORY")];
                    try {
                        ctcae_version = _s[headers.get("CTCAE_VERSION")].trim();
                    } catch (NumberFormatException e) {
                        //                                System.out.println(s);
                        return null;
                    }
                    ae_term = _s[headers.get("AE_TERM")];
                    if (_s.length - 1 >= headers.get("OTHER_TOXICITY"))
                        other_toxicity = _s[headers.get("OTHER_TOXICITY")];
                    else
                        other_toxicity = "";
                }
            }
        }

        if (nsc.trim().equals("")) {
            i++;
            continue;
        } else {
            // System.out.println(String.format("%s). NSC:%s,   V:%s,   C:%s,   T:%s", i, nsc, ctcae_version, ctcae_category, ae_term));
        }

        Agent a = agents.get(nsc);
        if (a == null) {
            a = agentDao.getByNscNumber(nsc);
            // System.out.println(asael + ". OK. Found agent [" + a.getName() + "] for NSC: [" + nsc + "]");
            agents.put(nsc, a);
        }

        if (a != null) {
            AgentSpecificCtcTerm t = new AgentSpecificCtcTerm();
            t.setAgent(a);
            t.setOtherToxicity(other_toxicity);

            List<CtcTerm> list = terminologyRepository.getCtcTerm(ctcae_category, ctcae_version, ae_term);
            if (list.size() == 0) {
                // System.out.println("<ERROR>: Term not found: " + ae_term + ", Category: " + ctcae_category + ", CTCAE Version: " + ctcae_version);
                missingTerms.add("Term not found: " + ae_term + ", Category: " + ctcae_category
                        + ", CTCAE Version: " + ctcae_version);
            } else {
                t.setCtcTerm(list.get(0));
                if (persistASAE(t))
                    asael++;
                else
                    duplicateAgentTerms++;
            }

            agentSpecificTermDao.evict(t);

        } else {
            if (!missingAgents.containsKey(nsc)) {
                // System.out.println("<ERROR>: The agent was not found by its NSC: " + nsc);
                missingAgents.put(nsc, null);
            }
        }

        i++;
    }

    if (isCSV) {
        br.close();
        r.close();
        ir.close();
    }

    results.put(KEY_MISSING_TERMS, missingTerms);
    results.put(KEY_PROCESSED_AGENTS, agents.size() - missingAgents.size());
    results.put(KEY_PROCESSED_AGENTTERMS, asael);
    results.put(KEY_MISSING_AGENTS, missingAgents);
    results.put(KEY_DUPLICATE_AGENT_TERMS, duplicateAgentTerms);

    return results;
}

From source file:gov.nih.nci.cabig.caaers.tools.ExcelProcessor.java

License:BSD License

private void bootstrap(File inputFile) throws Exception {

    poifs = new POIFSFileSystem(new FileInputStream(inputFile));

    // create a workbook out of the input stream
    wb = new HSSFWorkbook(poifs);
    studyInfoSheet = getSheet(STUDY_SHEET_NAME);
    agentInfoSheet = getSheet(AGENT_SHEET_NAME);
    diseaseInfoSheet = getSheet(DISEASE_SHEET_NAME);
    tacInfoSheet = getSheet(TAC_SHEET_NAME);
    orgInfoSheet = getSheet(ORG_SHEET_NAME);
    investigatorInfoSheet = getSheet(INVESTIGATOR_SHEET_NAME);
    therapyInfoSheet = getSheet(THERAPY_SHEET_NAME);
    rowCount = studyInfoSheet.getLastRowNum();
}

From source file:gov.nih.nci.cananolab.util.ExcelParser.java

License:BSD License

/**
 * Vertically parse the Excel file into a 2-D matrix represented as a map of map.
 * Key is Column header, value is a map, whose key is Row header and value is
 * the cell./*  w  ww.j  a va2 s  .c o m*/
 * 
 * @return
 * @throws IOException
 */
public SortedMap<String, SortedMap<String, Double>> verticalParse(String fileName) throws IOException {
    InputStream inputStream = null;
    SortedMap<String, SortedMap<String, Double>> dataMatrix = new TreeMap<String, SortedMap<String, Double>>();
    try {
        inputStream = new BufferedInputStream(new FileInputStream(fileName));
        POIFSFileSystem fs = new POIFSFileSystem(inputStream);
        Workbook wb = new HSSFWorkbook(fs);
        Sheet sheet1 = wb.getSheetAt(0);
        //printSheet(sheet1);
        Row firstRow = sheet1.getRow(0);
        int rowIndex = 0;
        for (Row row : sheet1) {
            int colIndex = 0;
            String rowHeader = row.getCell(0).getStringCellValue();
            for (Cell cell : row) {
                if (rowIndex > 0 && colIndex > 0) { //skipping first row/column
                    String columnHeader = firstRow.getCell(colIndex).getStringCellValue();
                    SortedMap<String, Double> columnData = null;
                    if (dataMatrix.get(columnHeader) != null) {
                        columnData = dataMatrix.get(columnHeader);
                    } else {
                        columnData = new TreeMap<String, Double>();
                    }
                    if (cell != null) {
                        columnData.put(rowHeader, cell.getNumericCellValue());
                        dataMatrix.put(columnHeader, columnData);
                    }
                }
                colIndex++;
            }
            rowIndex++;
        }
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Exception e) {
            }
        }
    }
    return dataMatrix;
}

From source file:gov.nih.nci.cananolab.util.ExcelParser.java

License:BSD License

/**
 * Horizontally parse the Excel file into a 2-D matrix represented as a map of map.
 * Key is Row header, value is a map, whose key is Column header and value is
 * the cell.//from  w  w w  .ja v a2s  .c  om
 * 
 * @return
 * @throws IOException
 */
public SortedMap<String, SortedMap<String, Double>> horizontalParse(String fileName) throws IOException {
    InputStream inputStream = null;
    SortedMap<String, SortedMap<String, Double>> dataMatrix = new TreeMap<String, SortedMap<String, Double>>();
    try {
        inputStream = new BufferedInputStream(new FileInputStream(fileName));
        POIFSFileSystem fs = new POIFSFileSystem(inputStream);
        Workbook wb = new HSSFWorkbook(fs);
        Sheet sheet1 = wb.getSheetAt(0);
        //printSheet(sheet1);
        Row firstRow = sheet1.getRow(0);
        int rowIndex = 0;
        for (Row row : sheet1) {
            int colIndex = 0;
            String rowHeader = row.getCell(0).getStringCellValue();
            for (Cell cell : row) {
                if (rowIndex > 0 && colIndex > 0) { //skipping first row/column
                    String columnHeader = firstRow.getCell(colIndex).getStringCellValue();
                    SortedMap<String, Double> rowData = null;
                    if (dataMatrix.get(rowHeader) != null) {
                        rowData = dataMatrix.get(rowHeader);
                    } else {
                        rowData = new TreeMap<String, Double>();
                    }
                    if (cell != null) {
                        rowData.put(columnHeader, cell.getNumericCellValue());
                        dataMatrix.put(rowHeader, rowData);
                    }
                }
                colIndex++;
            }
            rowIndex++;
        }
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Exception e) {
            }
        }
    }
    return dataMatrix;
}

From source file:gov.nih.nci.cananolab.util.ExcelParser.java

License:BSD License

/**
 * Parse secondary StanShaw Excel spreadsheet and store data in a 3-layer map.
 * 1st layer: sample map, key is sample name (261-13-4), value is the 2nd layer map.
 * 2nd layer: assay map, key is assay name (Aorta 1), value is the 3rd layer map.
 * 3rd layer: datum map, there are always 3 entries in this map, for example,  
 *            key is datum name Median (M), value is 9.02194E-08.
 *            key is datum name Mean (M), value is 7.96025E-08.
 *            key is datum name SEM (M), value is 6.12968E-09.
 *  //from ww w.  java 2  s .  c  o m
 * @param fileName
 * @return a 3-layer map
 * @throws IOException
 */
public SortedMap<String, SortedMap<String, SortedMap<String, Double>>> twoWayParse(String fileName)
        throws IOException {
    InputStream inputStream = null;
    SortedMap<String, SortedMap<String, SortedMap<String, Double>>> dataMatrix = new TreeMap<String, SortedMap<String, SortedMap<String, Double>>>();
    try {
        inputStream = new BufferedInputStream(new FileInputStream(fileName));
        POIFSFileSystem fs = new POIFSFileSystem(inputStream);
        Workbook wb = new HSSFWorkbook(fs);
        Sheet sheet1 = wb.getSheetAt(0);
        //printSheet(sheet1);
        // Sheet must contain >= 2 rows (header + data).
        if (sheet1.getLastRowNum() < 1) {
            return dataMatrix;
        }
        // Sheet must contain >= 5 columns (assay, sample + 3 datums).   
        Row firstRow = sheet1.getRow(0);
        if (firstRow.getLastCellNum() < 4) {
            return dataMatrix;
        }
        // Iterate sheet from 2nd row and populate the data matrix.
        for (int rowIndex = 1; rowIndex <= sheet1.getLastRowNum(); rowIndex++) {
            Row row = sheet1.getRow(rowIndex);

            //1.get sampleName key for 1st layer map, assayName key for 2 layer map.
            String sampleName = row.getCell(1).getStringCellValue();
            String assayName = row.getCell(0).getStringCellValue();

            //2.find sampleMap in dataMatrix, if null create & store new sampleMap.
            SortedMap<String, SortedMap<String, Double>> sampleMap = dataMatrix.get(sampleName);
            if (sampleMap == null) {
                sampleMap = new TreeMap<String, SortedMap<String, Double>>();
                dataMatrix.put(sampleName, sampleMap);
            }

            //3.find assayMap in sampleMap, if null create & store new assayMap.
            SortedMap<String, Double> assayMap = sampleMap.get(assayName);
            if (assayMap == null) {
                assayMap = new TreeMap<String, Double>();
                sampleMap.put(assayName, assayMap);
            }

            //4.iterate row from col-2 to last column, store datum value.
            for (int colIndex = 2; colIndex <= row.getLastCellNum(); colIndex++) {
                Cell cell = row.getCell(colIndex);
                if (cell != null && cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                    String datumName = firstRow.getCell(colIndex).getStringCellValue();
                    assayMap.put(datumName, cell.getNumericCellValue());
                }
            }
        }
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Exception e) {
            }
        }
        //this.print2ndMatrix(dataMatrix);
    }
    return dataMatrix;
}