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:no.hild1.bank.KonverterMottagerregister.java

License:Apache License

private void openWorkbook(File file) throws FileNotFoundException, IOException, InvalidFormatException {
    FileInputStream fis = null;/*w w  w.j a  va 2s.  co m*/
    try {
        System.out.println("pner arbeidsbok [" + file.getName() + "]");
        fis = new FileInputStream(file);
        this.workbook = WorkbookFactory.create(fis);
        this.evaluator = this.workbook.getCreationHelper().createFormulaEvaluator();
        this.formatter = new DataFormatter(true);
    } finally {
        if (fis != null) {
            fis.close();
        }
    }
}

From source file:no.hild1.excelsplit.ES.java

private void openWorkbook(File file) throws FileNotFoundException, IOException, InvalidFormatException {
    FileInputStream fis = null;/*from   w w w . j  ava  2s  . c  o  m*/
    try {
        System.out.println("pner arbeidsbok [" + file.getName() + "]");
        fis = new FileInputStream(file);
        this.inputWorkbook = WorkbookFactory.create(fis);
        this.evaluator = this.inputWorkbook.getCreationHelper().createFormulaEvaluator();
        this.formatter = new DataFormatter(true);
    } finally {
        if (fis != null) {
            fis.close();
        }
    }
}

From source file:nu.mine.kino.jenkins.plugins.projectmanagement.utils.PMUtils.java

License:Open Source License

public static Date getBaseDateFromExcelWithPoi(File file) {
    InputStream in = null;/*w w  w.j  av a2s .  co  m*/
    try {
        in = new FileInputStream(file);
        Workbook workbook = WorkbookFactory.create(in);
        Sheet sheet = workbook.getSheetAt(0);
        Name name = workbook.getName("??");
        CellReference cellRef = new CellReference(name.getRefersToFormula());
        Row row = sheet.getRow(cellRef.getRow());
        Cell baseDateCell = row.getCell(cellRef.getCol());
        // System.out.println("cellt:"
        // + PoiUtil.isCellDateFormatted(baseDateCell));
        Date baseDate = baseDateCell.getDateCellValue();
        return baseDate;

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (InvalidFormatException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null)
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

    }
    return null;
}

From source file:nu.mine.kino.projects.ExcelProjectCreator.java

License:Open Source License

/**
 * Excel tH?[}bgFile(InputStream)Project??
 * //from w  w  w.j  ava 2 s. co  m
 * @see nu.mine.kino.projects.ProjectCreator#createProject(java.io.InputStream)
 */
@Override
public Project createProjectFromStream() throws ProjectException {

    Map<String, ExcelPOIScheduleBean> poiMap = new HashMap<String, ExcelPOIScheduleBean>();
    Date baseDate = null;
    Holiday[] holidays = null;
    FileInputStream in = null;
    try {
        in = new FileInputStream(file);
        Workbook workbook = WorkbookFactory.create(in);
        Sheet sheet = workbook.getSheetAt(0);

        baseDate = ProjectUtils.createBaseDate(workbook, sheet);
        poiMap = ProjectUtils.createExcelPOIScheduleBeanMap(workbook, baseDate);
        holidays = ProjectUtils.createHolidays(workbook);
    } catch (InvalidFormatException e) {
        throw new ProjectException(e);
    } catch (FileNotFoundException e) {
        throw new ProjectException(e);
    } catch (IOException e) {
        throw new ProjectException(e);
    } finally {
        if (in != null)
            try {
                in.close();
            } catch (IOException e) {
                // TODO ?? catch u?bN
                e.printStackTrace();
            }
    }

    try {
        List<TaskInformation> taskInfoList = new ArrayList<TaskInformation>();
        ExcelScheduleBeanSheet sheet = new ExcelScheduleBeanSheet();
        // getInputStream(), ExcelScheduleBeanSheet.class);
        // ExcelScheduleBeanSheet sheet = new XLSBeans().load(
        // getInputStream(), ExcelScheduleBeanSheet.class);
        sheet.setBaseDate(baseDate);
        plus(sheet);
        java.util.List<ExcelScheduleBean> instanceList = sheet.getExcelScheduleBean();

        for (ExcelScheduleBean instance : instanceList) {

            // poiBeanJavaBeansRs?[?B
            ExcelPOIScheduleBean poiBean = poiMap.get(instance.getTaskId());
            if (poiBean != null && !StringUtils.isEmpty(poiBean.getId())) {
                Task task = ExcelScheduleBean2Task.convert(instance); // ???AMapinstance?s?B

                ExcelPOIScheduleBean2Task.convert(poiBean, task);
                PVTotalBean pvTotalBean = ExcelPOIScheduleBean2PVTotalBean.convert(poiBean);
                ACTotalBean acTotalBean = ExcelPOIScheduleBean2ACTotalBean.convert(poiBean);
                EVTotalBean evTotalBean = ExcelPOIScheduleBean2EVTotalBean.convert(poiBean);
                TaskInformation taskInfo = ExcelPOIScheduleBean2TaskInformation.convert(poiBean);
                taskInfo.setTask(task);
                taskInfo.setPV(pvTotalBean);
                taskInfo.setAC(acTotalBean);
                taskInfo.setEV(evTotalBean);
                taskInfoList.add(taskInfo);
            }
        }

        Project project = new Project();
        project.setTaskInformations(taskInfoList.toArray(new TaskInformation[taskInfoList.size()]));

        ExcelScheduleBeanSheet2Project.convert(sheet, project);

        project.setHolidays(holidays);
        return project;
        // } catch (XLSBeansException e) {
        // throw new ProjectException(e);
        // }
    } finally {

    }
}

From source file:nu.mine.kino.projects.ExcelProjectCreator.java

License:Open Source License

private void plus(ExcelScheduleBeanSheet excelBeanSheet) throws ProjectException {
    FileInputStream in = null;/*w  w w  .j  ava 2s  .  c o m*/
    try {
        in = new FileInputStream(file);
        Workbook workbook = WorkbookFactory.create(in);
        excelBeanSheet.init(workbook);
    } catch (InvalidFormatException e) {
        throw new ProjectException(e);
    } catch (FileNotFoundException e) {
        throw new ProjectException(e);
    } catch (IOException e) {
        throw new ProjectException(e);
    } finally {
        if (in != null)
            try {
                in.close();
            } catch (IOException e) {
                // TODO ?? catch u?bN
                e.printStackTrace();
            }
    }

}

From source file:nu.mine.kino.projects.utils.BaseDataUtilsTest.java

License:Open Source License

@Test
public void test() throws ParseException, ProjectException {
    java.io.InputStream in = null;
    try {//from   w  w w  .  ja v  a 2 s. co  m
        Workbook workbook = WorkbookFactory
                .create(new FileInputStream(new File("project_management_tools.xls")));
        Sheet poiSheet = workbook.getSheetAt(0);
        Date baseDate = ProjectUtils.createBaseDate(workbook, poiSheet);
        Map<String, ExcelPOIScheduleBean> poiMap = ProjectUtils.createExcelPOIScheduleBeanMap(workbook,
                baseDate);

        List<TaskInformation> taskInfoList = new ArrayList<TaskInformation>();

        in = new java.io.FileInputStream("project_management_tools.xls");
        ExcelScheduleBeanSheet sheet = new ExcelScheduleBeanSheet();
        // ExcelScheduleBeanSheet sheet = new XLSBeans().load(in,
        // ExcelScheduleBeanSheet.class);
        sheet.init(workbook);

        java.util.List<ExcelScheduleBean> instanceList = sheet.getExcelScheduleBean();

        for (ExcelScheduleBean instance : instanceList) {
            if (!StringUtils.isEmpty(instance.getTaskId())) {
                Task task = ExcelScheduleBean2Task.convert(instance);
                print(task);
            }

        }
        System.out.println(instanceList.size());
        System.out.println(sheet.getBaseDate());

    } catch (FileNotFoundException e) {
        // TODO ?? catch u?bN
        e.printStackTrace();
        // } catch (XLSBeansException e) {
        // // TODO ?? catch u?bN
        // e.printStackTrace();
    } catch (InvalidFormatException e) {
        // TODO ?? catch u?bN
        e.printStackTrace();
    } catch (IOException e) {
        // TODO ?? catch u?bN
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                // TODO ?? catch u?bN
                e.printStackTrace();
            }
        }
    }

}

From source file:nu.mine.kino.projects.utils.POI2Test.java

License:Open Source License

@Before
public void before() {
    try {//from w  w w  .j  a  v a2s  . c o m
        in = new FileInputStream(new java.io.File("testdata2.xls"));
        workbook = WorkbookFactory.create(in);
    } catch (FileNotFoundException e) {
        Assert.fail(e.getMessage());
    } catch (InvalidFormatException e) {
        Assert.fail(e.getMessage());
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    }
}

From source file:nu.mine.kino.projects.utils.POITest.java

License:Open Source License

public static void main(String[] args) {
    Workbook workbook = null;//  w  w w. ja v a2 s .  c om
    FileInputStream in = null;
    try {
        in = new FileInputStream(new java.io.File("project_management_tools.xls"));
        workbook = WorkbookFactory.create(in);
        Sheet sheet = workbook.getSheetAt(0);
        // int[] rowBreaks = sheet.getRowBreaks();

        Row row = sheet.getRow(25);
        Cell cell = row.getCell(24);
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_NUMERIC:
            System.out.println(cell.getNumericCellValue());
            break;
        case Cell.CELL_TYPE_STRING:
            System.out.println(cell.getStringCellValue());
            break;
        default:
            System.out.println("cellType=" + cell.getCellType());
            break;
        }
        System.out.println("value: " + cell);
        System.out.println(sheet.getRow(46).getCell(4));

        final int rowMin = sheet.getFirstRowNum();
        System.out.println(rowMin);
        final int rowMax = sheet.getLastRowNum();
        System.out.println(rowMax);

    } catch (InvalidFormatException e) {
        // TODO ?? catch u?bN
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        // TODO ?? catch u?bN
        e.printStackTrace();
    } catch (IOException e) {
        // TODO ?? catch u?bN
        e.printStackTrace();
    } finally {
        if (in != null)
            try {
                in.close();
            } catch (IOException e) {
                // TODO ?? catch u?bN
                e.printStackTrace();
            }
    }

}

From source file:nu.mine.kino.projects.utils.POITest.java

License:Open Source License

@Before
public void before() {
    try {/* w w w .j  a  v a 2  s  .co m*/
        in = new FileInputStream(new java.io.File("project_management_tools.xls"));
        workbook = WorkbookFactory.create(in);
    } catch (FileNotFoundException e) {
        Assert.fail(e.getMessage());
    } catch (InvalidFormatException e) {
        Assert.fail(e.getMessage());
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    }
}

From source file:org.abhishek.simplicitas.util.common.ExcelUtils.java

License:Apache License

/**
 * Open an Excel workbook ready for conversion.
 * <p>/*from   w  ww  . j a va2s .co m*/
 * Converts all exceptions to Runtime alternatives thereby removing the need
 * for explicit checks.
 * <p>
 * Handles {@link FileNotFoundException} thrown if the file cannot be
 * located.
 * <p>
 * Handles {@link IOException} thrown if a problem occurs in the file
 * system.
 * <p>
 * Handles {@link InvalidFormatException} thrown if invalid xml is found
 * whilst parsing an input SpreadsheetML file.
 *
 * @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.
 */
private void openWorkbook(File file) {
    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(true);
    } catch (InvalidFormatException ex) {
        handleCheckedException(ex);
    } catch (IOException ex) {
        handleIOException(ex);
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException ex) {
                handleIOException(ex);
            }
        }
    }
}