Example usage for org.apache.poi.ss.usermodel WorkbookFactory create

List of usage examples for org.apache.poi.ss.usermodel WorkbookFactory create

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel WorkbookFactory create.

Prototype

public static Workbook create(File file) throws IOException, EncryptedDocumentException 

Source Link

Document

Creates the appropriate HSSFWorkbook / XSSFWorkbook from the given File, which must exist and be readable.

Usage

From source file:net.pcal.sqlsheet.XlsDriver.java

License:Apache License

private Workbook getOrCreateWorkbook(URL workbookUrl) throws IOException, InvalidFormatException {
    if (workbookUrl.getProtocol().equalsIgnoreCase("file")) {
        File file = new File(workbookUrl.getPath());
        if (!file.exists() || (file.length() == 0)) {
            Workbook workbook;//from  ww  w.  j a  va 2s  .c o m
            if (file.getPath().toLowerCase().endsWith("x")) {
                workbook = new XSSFWorkbook();
            } else {
                workbook = new HSSFWorkbook();
            }
            flushWorkbook(workbook, file);
        }
    }
    return WorkbookFactory.create(workbookUrl.openStream());
}

From source file:net.sf.dvstar.swirl.desktopdbf.data.XLSPanelLoader.java

License:Open Source License

/**
 * Open an Excel workbook ready for conversion.
 *
 * @param file An instance of the File class that encapsulates a handle
 *        to a valid Excel workbook. Note that the workbook can be in
 *        either binary (.xls) or SpreadsheetML (.xlsx) format.
 * @throws java.io.FileNotFoundException Thrown if the file cannot be located.
 * @throws java.io.IOException Thrown if a problem occurs in the file system.
 * @throws org.apache.poi.openxml4j.exceptions.InvalidFormatException Thrown
 *         if invalid xml is found whilst parsing an input SpreadsheetML
 *         file.//from   ww  w  .java 2  s.  c o m
 */
private void openWorkbook(File file) throws FileNotFoundException, IOException, InvalidFormatException {
    FileInputStream fis = null;
    try {
        System.out.println("Opening workbook [" + file.getName() + "]");

        fis = new FileInputStream(file);

        // Open the workbook and then create the FormulaEvaluator and
        // DataFormatter instances that will be needed to, respectively,
        // force evaluation of forumlae found in cells and create a
        // formatted String encapsulating the cells contents.
        this.workbook = WorkbookFactory.create(fis);
        this.evaluator = this.workbook.getCreationHelper().createFormulaEvaluator();
        this.formatter = new DataFormatter();
    } finally {
        if (fis != null) {
            fis.close();
        }
    }
}

From source file:net.sf.excelutils.WorkbookUtils.java

License:Apache License

/**
 * Open Excel File/*from   w  ww. jav  a2s .c om*/
 *
 * @param ctx ServletContext
 * @param config Excel Template Name
 * @throws ExcelException
 * @return Workbook
 */
public static Workbook openWorkbook(ServletContext ctx, String config) throws ExcelException {

    InputStream in = null;
    Workbook wb = null;
    try {
        in = ctx.getResourceAsStream(config);
        wb = WorkbookFactory.create(in);
    } catch (Exception e) {
        throw new ExcelException("File" + config + "not found," + e.getMessage());
    } finally {
        try {
            in.close();
        } catch (Exception e) {
        }
    }
    return wb;
}

From source file:net.sf.excelutils.WorkbookUtils.java

License:Apache License

/**
 * Open an excel file by real fileName/* w  w w  . j a  va2s .co m*/
 *
 * @param fileName
 * @return Workbook
 * @throws ExcelException
 */
public static Workbook openWorkbook(String fileName) throws ExcelException {
    InputStream in = null;
    Workbook wb = null;
    try {
        in = new FileInputStream(fileName);
        wb = WorkbookFactory.create(in);
    } catch (Exception e) {
        throw new ExcelException("File" + fileName + "not found" + e.getMessage());
    } finally {
        try {
            in.close();
        } catch (Exception e) {
        }
    }
    return wb;
}

From source file:net.sf.excelutils.WorkbookUtils.java

License:Apache License

/**
 * Open an excel from InputStream//from ww w  .j  a  v  a2 s  .  c  o  m
 *
 * @param in
 * @return Workbook
 * @throws ExcelException
 */
public static Workbook openWorkbook(InputStream in) throws ExcelException {
    Workbook wb = null;
    try {
        wb = WorkbookFactory.create(in);
    } catch (Exception e) {
        throw new ExcelException(e.getMessage());
    }
    return wb;
}

From source file:net.sf.taverna.t2.activities.spreadsheet.ExcelSpreadsheetReader.java

License:Open Source License

public void read(InputStream inputStream, Range rowRange, Range columnRange, boolean ignoreBlankRows,
        SpreadsheetRowProcessor rowProcessor) throws SpreadsheetReadException {
    Workbook workbook;/*from w w  w.jav a 2  s.  co  m*/
    try {
        workbook = WorkbookFactory.create(inputStream);
    } catch (InvalidFormatException e) {
        throw new SpreadsheetReadException("The file does not have a compatible spreadsheet format", e);
    } catch (IOException e) {
        throw new SpreadsheetReadException("The spreadsheet stream could not be read", e);
    } catch (IllegalArgumentException e) {
        throw new SpreadsheetReadException("The spreadsheet stream could not be read", e);
    }

    DataFormatter dataFormatter = new DataFormatter();

    workbook.setMissingCellPolicy(Row.CREATE_NULL_AS_BLANK);
    Sheet sheet = workbook.getSheetAt(0);

    if (rowRange.getEnd() < 0) {
        rowRange.setEnd(sheet.getLastRowNum());
        logger.debug("No end of row range specified, setting to " + rowRange.getEnd());
    }

    SortedMap<Integer, String> currentDataRow = new TreeMap<Integer, String>();

    for (int rowIndex = rowRange.getStart(); rowIndex <= rowRange.getEnd(); rowIndex++) {
        boolean blankRow = true;
        if (rowRange.contains(rowIndex)) {
            Row row = sheet.getRow(rowIndex);
            for (int columnIndex = columnRange.getStart(); columnIndex <= columnRange.getEnd(); columnIndex++) {
                if (columnRange.contains(columnIndex)) {
                    String value = null;
                    if (row != null) {
                        Cell cell = row.getCell(columnIndex);
                        if (cell != null) {
                            value = getCellValue(cell, dataFormatter);
                        }
                    }
                    if (value != null) {
                        blankRow = false;
                    }
                    currentDataRow.put(columnIndex, value);
                    if (columnIndex == columnRange.getEnd()) {
                        if (!ignoreBlankRows || !blankRow) {
                            rowProcessor.processRow(rowIndex, currentDataRow);
                        }
                        currentDataRow = new TreeMap<Integer, String>();
                    }
                }
            }
        }
    }

}

From source file:net.sibcolombia.sibsp.service.portal.implementation.ResourceManagerImplementation.java

License:Creative Commons License

/**
 * Process template file to generate an EML XML file
 * // w  ww .  j  av  a 2 s  . c o  m
 * @param sourceFile
 * @param actionLogger
 * @throws IOException
 * @throws InvalidFormatException
 */
@Override
public Resource processMetadataSpreadsheetPart(File sourceFile, String fileName, ActionLogger actionLogger)
        throws InvalidFormatException, IOException, NullPointerException {
    Resource resource = new Resource();
    Eml eml = new Eml();
    Workbook template = WorkbookFactory.create(sourceFile);

    readBasicMetaData(eml, template, resource);
    readGeographicCoverage(eml, template);
    readTaxonomicCoverage(eml, template);
    readTemporalCoverage(eml, template);
    readKeywords(eml, template);
    readAssociatedParties(eml, template);
    readProjectData(eml, template);
    readSamplingMethods(eml, template);
    readCitations(eml, template);
    readCollectionData(eml, template);
    readExternallinks(eml, template);
    readAdditionalMetadata(eml, template);

    // Set resource details
    resource.setFileName(fileName);
    resource.setEml(eml);

    return resource;
}

From source file:net.sourceforge.squirrel_sql.plugins.dataimport.importer.excel.ExcelFileImporter.java

License:Open Source License

public boolean open() throws IOException {
    try {/*from   www.  j  ava 2 s . c o m*/
        workbook = WorkbookFactory.create(importFile);
    } catch (InvalidFormatException fe) {
        throw new IOException(fe.toString());
    }
    reset();
    return true;
}

From source file:net.sourceforge.squirrel_sql.plugins.dataimport.importer.excel.ExcelFileImporter.java

License:Open Source License

public String[][] getPreview(int noOfLines) throws IOException {
    String[][] data = null;/*from w w w.j  a v  a  2  s.c om*/
    Workbook wb = null;
    Sheet sht = null;
    try {
        wb = WorkbookFactory.create(importFile);
        sht = getSheet(wb);
    } catch (InvalidFormatException fe) {
        throw new IOException(fe.toString());
    }

    int y = 0;
    int x = 0;
    int maxLines = (noOfLines < sht.getPhysicalNumberOfRows()) ? noOfLines : sht.getPhysicalNumberOfRows();
    Row row = sht.getRow(0);
    data = new String[maxLines][row.getPhysicalNumberOfCells()];

    for (y = 0; y < maxLines; y++) {
        for (x = 0; x < row.getPhysicalNumberOfCells(); x++) {
            data[y][x] = row.getCell(x).toString();
        }
    }
    return data;
}

From source file:net.sourceforge.squirrel_sql.plugins.dataimport.importer.excel.ExcelSettingsPanel.java

License:Open Source License

/**
 * The standard constructor//w w  w.j  a va  2s .c o  m
 * 
 * @param settings The settings holder
 * @param f The import file.
 */
public ExcelSettingsPanel(ExcelSettingsBean settings, File f) {
    this.settings = settings;
    try {
        this.wb = WorkbookFactory.create(f);
    } catch (Exception e) {
        this.wb = null;
    }
    init();
    loadSettings();
}