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:org.mifos.dmt.excel.columnValidator.ColumnStructureTest.java

License:Open Source License

@SuppressWarnings("static-access")
@Test//from   w  w  w .j  av  a 2s  . c o  m
public void processSheetStructureTrue() throws InvalidFormatException, IOException, DMTException {
    sheetPass = false;
    baseTemplate = DMTConfig.DMT_CONFIG_DIR + "\\DMTMigrationTemplateBase.xlsx";
    InputStream inp = this.getClass().getResourceAsStream("MigrationTemplatePassTest.xlsx");
    workbook = WorkbookFactory.create(inp);
    columnStructure = new ColumnStructure(workbook, baseTemplate);
    columnStructure.processSheetStructure();
    sheetPass = columnStructure.sheetPass;
    Assert.assertTrue(sheetPass);

}

From source file:org.mifos.dmt.excel.columnValidator.ColumnStructureTest.java

License:Open Source License

@SuppressWarnings("static-access")
@Test/*w  w  w . jav a 2  s  . c o m*/
public void processSheetStructureFalse() throws InvalidFormatException, IOException {
    sheetPass = true;
    baseTemplate = DMTConfig.DMT_CONFIG_DIR + "\\DMTMigrationTemplateBase.xlsx";
    InputStream inp = this.getClass().getResourceAsStream("MigrationTemplateFailTest.xlsx");
    workbook = WorkbookFactory.create(inp);
    columnStructure = new ColumnStructure(workbook, baseTemplate);
    try {
        columnStructure.processSheetStructure();
    } catch (DMTException e) {
        sheetPass = columnStructure.sheetPass;
    } finally {
        Assert.assertFalse(sheetPass);

    }

}

From source file:org.mifos.dmt.excel.CreateExcel.java

License:Open Source License

public Workbook generate(String baseTemplate, DMTMigrationParams params)
        throws InvalidFormatException, IOException, DMTException {
    InputStream inp = new FileInputStream(baseTemplate);

    Workbook wb = WorkbookFactory.create(inp);
    ArrayList<String> sheetSpec = readSheetSpec(params);
    for (int i = wb.getNumberOfSheets(); i > 0; i--) {
        String sheetName = wb.getSheetName(i - 1);
        if (sheetSpec.contains(sheetName)) {
            wb.removeSheetAt(i - 1);//ww  w  .j a  va2s  .c om
        } else {
            logger.info("Adding sheet : " + sheetName);
        }

    }
    Sheet configSheet = wb.getSheetAt(0);
    editConfigSheet(configSheet);

    return wb;
}

From source file:org.mifos.dmt.excel.workbookValidator.SheetStructureTest.java

License:Open Source License

@Test
public void processWorkbookTrue() throws InvalidFormatException, IOException, Exception {

    InputStream inp = this.getClass().getResourceAsStream("MigrationTemplatePassTest.xlsx");
    workbook = WorkbookFactory.create(inp);
    sheetStructure = new SheetStructure(this.workbook);
    status = false;/*from  www .  j  av  a2 s .c  o m*/
    status = sheetStructure.processWorkbook();
    Assert.assertTrue(status);
}

From source file:org.mifos.dmt.excel.workbookValidator.SheetStructureTest.java

License:Open Source License

@Test
public void processWorkbookFalse() throws InvalidFormatException, IOException, Exception {

    InputStream inp = this.getClass().getResourceAsStream("MigrationTemplateFailTest.xlsx");
    workbook = WorkbookFactory.create(inp);
    sheetStructure = new SheetStructure(this.workbook);
    status = true;//  w  ww  .  java2s .c om
    status = sheetStructure.processWorkbook();
    Assert.assertFalse(status);
}

From source file:org.mifos.dmt.ui.DMTExcelUpload.java

License:Open Source License

@SuppressWarnings("rawtypes")
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    DMTLock migrationLock = DMTLock.getInstance();
    if (!migrationLock.isLocked()) {
        migrationLock.getLock();//from   ww  w.j a  va 2s  .c om

        clearLogs();
        response.setContentType("text/plain");

        DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
        fileItemFactory.setSizeThreshold(5 * 1024 * 1024);
        fileItemFactory.setRepository(tmpDir);
        ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);

        try {
            List items = uploadHandler.parseRequest(request);
            Iterator itr = items.iterator();
            while (itr.hasNext()) {
                FileItem item = (FileItem) itr.next();

                File file = new File(destinationDir, "MigrationTemplate.xlsx");
                /*System.out.println("i got here " + destinationDir.toString());*/
                Workbook workbook = WorkbookFactory.create(item.getInputStream());

                SheetStructure sheetStructure = new SheetStructure(workbook);
                if (!sheetStructure.processWorkbook()) {
                    logger.error(
                            "Excel upload failed!!Please check if necessary sheets are present in the excel");
                    throw new DMTException(
                            "Excel upload failed!!Please check if necessary sheets are present in the excel");
                }

                String baseTemplate = DMTConfig.DMT_CONFIG_DIR + "\\DMTMigrationTemplateBase.xlsx";
                ColumnStructure columnstructure = new ColumnStructure(workbook, baseTemplate);
                workbook = columnstructure.processSheetStructure();

                PurgeEmptyRows excessrows = new PurgeEmptyRows(workbook);
                workbook = excessrows.processEmptyRows();

                FileOutputStream fileoutputstream = new FileOutputStream(file);
                workbook.write(fileoutputstream);
                logger.info("Uploading of Excel has been successful!!");
                migrationLock.releaseLock();
            }
        } catch (FileUploadException ex) {
            logger.error("Error encountered while parsing the request", ex);
            logger.error("Uploading of Excel has not been successful!!");
            migrationLock.releaseLock();
            ex.printStackTrace();
        } catch (Exception ex) {
            logger.error("Error encountered while uploading file", ex);
            logger.error("Uploading of Excel has not been successful!!");
            migrationLock.releaseLock();
            ex.printStackTrace();
        }

    } else {
        request.setAttribute("action", "info");
        RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/x");
        requestDispatcher.forward(request, response);
    }
}

From source file:org.mousephenotype.cda.exporter.ExporterExcel.java

License:Apache License

/**
 * Exports a collection of data to an {@code excel} file.
 *
 * @param response {@link HttpServletResponse} instance required by the downstream implementation
 * @param filename The name of the download file that will be created
 * @param headings The headings. If null, no heading is written
 * @param data The data to be written/*w  w w.  jav  a 2s. c o m*/
 * @throws IOException if {@code exportType} is not valid
 */
public static void export(HttpServletResponse response, String filename, List<String> headings,
        List<List<String>> data) throws IOException {

    boolean isWorkbookEmpty = true;

    response.setHeader("Pragma", "no-cache");
    response.setHeader("Expires", "0");
    response.setHeader("Content-disposition", "attachment; filename=" + filename + ".xlsx");

    int nextRow = 0;
    Workbook workbook = WorkbookFactory.create(true);
    Sheet sheet = workbook.createSheet("Sheet 1");
    Row row;

    if (headings != null) {
        row = sheet.createRow(nextRow++);
        writeRow(row, headings);
        isWorkbookEmpty = false;
    }
    if (data != null) {
        for (int i = 0; i < data.size(); i++) {
            row = sheet.createRow(nextRow++);
            writeRow(row, data.get(i));
        }
        isWorkbookEmpty = false;
    }

    if (!isWorkbookEmpty) {
        workbook.write(response.getOutputStream());
    }
}

From source file:org.mousephenotype.cda.threei.util.AnaExcelReader.java

License:Apache License

public AnaExcelReader(String inFilename) {
    try {/*  w  w  w. ja  v a  2s.c  o m*/
        this.inputStream = new FileInputStream(new File(inFilename));
        this.workbook = WorkbookFactory.create(inputStream);
        this.datatypeSheet = workbook.getSheetAt(0);
        this.rowIterator = datatypeSheet.iterator();

        setColumnHeadings();
        reset();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.mousephenotype.cda.utilities.DataReaderXls.java

License:Apache License

/**
 * Opens the stream defined by the url used in the constructor.
 * @throws IOException/*from   w  ww  .j  a v a2 s .  c  o  m*/
 */
@Override
protected void open() throws IOException {
    try (InputStream inputStream = url.openStream()) {
        workbook = WorkbookFactory.create(inputStream);
    } catch (Exception e) {
        System.out.println(
                "Error opening workbook. Will treat as empty workbook. Exception: " + e.getLocalizedMessage());
        return;
    }
    Sheet sheet = workbook.getSheetAt(0);
    rowIterator = sheet.rowIterator();
}

From source file:org.mousephenotype.www.testing.model.DataReaderXls.java

License:Apache License

/**
 * Opens the stream defined by the url used in the constructor.
 * @throws IOException /*  w  ww.ja va 2  s. c o m*/
 */
@Override
public void open() throws IOException {
    try {
        try (InputStream inputStream = url.openStream()) {
            workbook = WorkbookFactory.create(inputStream);
        }
        Sheet sheet = workbook.getSheetAt(0);
        rowIterator = sheet.rowIterator();
    } catch (InvalidFormatException e) {
        System.out.println("InvalidFormatException: " + e.getLocalizedMessage());
        throw new IOException(e);
    }
}