Example usage for org.apache.poi.ss.usermodel Workbook write

List of usage examples for org.apache.poi.ss.usermodel Workbook write

Introduction

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

Prototype

void write(OutputStream stream) throws IOException;

Source Link

Document

Write out this workbook to an Outputstream.

Usage

From source file:com.NRC.NMEA.main.NMEA.java

public static void createExcelFile() throws IOException {
    Workbook wb = new XSSFWorkbook();
    FileOutputStream fileOut = new FileOutputStream("C:\\Users\\Noah\\Desktop\\Excel stuff\\Data.xlsx");
    ;// w  w  w  .j  av  a2s  .  co m
    Sheet PSheet = wb.createSheet("Position Data");
    Sheet ESheet = wb.createSheet("Elevation Data");
    Sheet AbESheet = wb.createSheet("Absolute Elevation Data");
    Sheet DSheet = wb.createSheet("Chart Data");
    DSheet = writeStatInfoToExcel(DSheet);

    {
        AbESheet = writeAbsElevationsToExcel(pControl, AbESheet, 0);
        AbESheet = writeAbsElevationsToExcel(test1, AbESheet, 4);
        AbESheet = writeAbsElevationsToExcel(test2, AbESheet, 9);
        AbESheet = writeAbsElevationsToExcel(test3, AbESheet, 14);
        AbESheet = writeAbsElevationsToExcel(test4, AbESheet, 19);

        //           

    }
    {

        ESheet = writeElevationsToExcel(pControl, ESheet, 0);
        ESheet = writeElevationsToExcel(test1, ESheet, 4);
        ESheet = writeElevationsToExcel(test2, ESheet, 9);
        ESheet = writeElevationsToExcel(test3, ESheet, 14);
        ESheet = writeElevationsToExcel(test4, ESheet, 19);
        Set<Double> Eset2 = new HashSet();
        Eset2.add(pControl.dev.meanElevation);
        Eset2.add(test1.dev.meanElevation);
        Eset2.add(test2.dev.meanElevation);
        Eset2.add(test3.dev.meanElevation);
        Eset2.add(test4.dev.meanElevation);
        Set<Double> Eset3 = new HashSet();
        Eset3.add(pControl.dev.verticalStandardDeviation);
        Eset3.add(test1.dev.verticalStandardDeviation);
        Eset3.add(test2.dev.verticalStandardDeviation);
        Eset3.add(test3.dev.verticalStandardDeviation);
        Eset3.add(test4.dev.verticalStandardDeviation);

        Row erow21 = ESheet.createRow((short) 24);
        Row erow22 = ESheet.createRow((short) 25);
        Row erow23 = ESheet.createRow((short) 27);
        Row erow24 = ESheet.createRow((short) 28);

        Cell erow21c = erow21.createCell(0);
        erow21c.setCellValue("Means");
        Cell erow22c = erow22.createCell(0);
        erow22c.setCellValue("Height");

        Cell erow23c = erow23.createCell(0);
        erow23c.setCellValue("Standard Deviations");
        Cell erow24c = erow24.createCell(0);
        erow24c.setCellValue("Height");

        int i = 1;
        for (Double d : Eset2) {
            erow22c = erow22.createCell(i);
            erow22c.setCellValue(d);
            i++;
        }
        i = 1;
        for (Double d : Eset3) {
            erow24c = erow24.createCell(i);
            erow24c.setCellValue(d);
            i++;
        }

    }

    {
        PSheet = writePositionsToExcel(pControl, PSheet, 0);
        PSheet = writePositionsToExcel(test1, PSheet, 4);
        PSheet = writePositionsToExcel(test2, PSheet, 9);
        PSheet = writePositionsToExcel(test3, PSheet, 14);
        PSheet = writePositionsToExcel(test4, PSheet, 19);
        {
            Set<Coordinate> set = new HashSet();
            set.add(pControl.dev.center);
            set.add(test1.dev.center);
            set.add(test2.dev.center);
            set.add(test3.dev.center);
            set.add(test4.dev.center);
            Set<Double> set2 = new HashSet();
            set2.add(pControl.dev.latitudeStandardDeviation);
            set2.add(test1.dev.latitudeStandardDeviation);
            set2.add(test2.dev.latitudeStandardDeviation);
            set2.add(test3.dev.latitudeStandardDeviation);
            set2.add(test4.dev.latitudeStandardDeviation);
            Set<Double> set3 = new HashSet();
            set3.add(pControl.dev.longitudeStandardDeviation);
            set3.add(test1.dev.longitudeStandardDeviation);
            set3.add(test2.dev.longitudeStandardDeviation);
            set3.add(test3.dev.longitudeStandardDeviation);
            set3.add(test4.dev.longitudeStandardDeviation);

            Row centerrow1 = PSheet.createRow((short) 24);
            Row centerrow2 = PSheet.createRow((short) 25);
            Row centerrow3 = PSheet.createRow((short) 26);
            Cell centerrow1c = centerrow1.createCell(0);
            centerrow1c.setCellValue("Centers");
            Cell centerrow2c = centerrow2.createCell(0);
            centerrow2c.setCellValue("lat");
            Cell row3c = centerrow3.createCell(0);
            row3c.setCellValue("long");

            int i = 1;
            for (Coordinate c : set) {
                centerrow1c = centerrow1.createCell(i);
                centerrow2c = centerrow2.createCell(i);
                centerrow1c.setCellValue(c.getLatitude());
                centerrow2c.setCellValue(-c.getLongitude());
                i++;

            }
        }
    }
    wb.write(fileOut);
    fileOut.close();
}

From source file:com.oleke.facebookcrawler.ExcelAPI.java

License:Apache License

/**
 * This method creates an Excel Workbook and an Excel Sheet
 *
 * @param filename The output filename/*from www.j  a va  2 s . c o m*/
 * @param sheetname the name of the sheet
 * @return Returns a new workbook
 */
public Workbook createExcel(String filename, String sheetname) {
    try {
        FileOutputStream out = new FileOutputStream(filename + ".xls");
        Workbook wb = new HSSFWorkbook();
        wb.createSheet(sheetname);
        wb.write(out);
        out.close();
        return wb;
    } catch (IOException ex) {
        Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;

}

From source file:com.oleke.facebookcrawler.ExcelAPI.java

License:Apache License

/**
 * This method writes all changes made to a workbook
 *
 * @param filename The output filename//from w ww  . j  ava2  s . c  o  m
 * @param wbk The input workbook
 * @return Returns true if changes were written to file
 */
public boolean commitChanges(String filename, Workbook wbk) {
    try {
        FileOutputStream out = new FileOutputStream(filename + ".xls");
        wbk.write(out);
        out.close();
        return true;
    } catch (IOException ex) {
        Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
    }
    return false;
}

From source file:com.opendoorlogistics.core.tables.io.PoiIO.java

License:Open Source License

private static void saveWorkbook(File file, Workbook wb) {
    try {//from   w ww  .  ja  v  a 2s  .  co  m
        FileOutputStream fos = new FileOutputStream(file, false);
        wb.write(fos);
        fos.flush();
        fos.close();
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }
}

From source file:com.perceptive.epm.perkolcentral.bl.ExcelReportBL.java

public FileInputStream generateAllEmployeeReport(File file) throws ExceptionWrapper {
    try {//from  w  ww  . j  av a 2s .com
        FileInputStream inp = new FileInputStream(file);
        //InputStream inp = new FileInputStream("workbook.xlsx");

        Workbook wb = WorkbookFactory.create(inp);
        Sheet sheet = wb.getSheetAt(0);

        int iRowCounter = 1;
        for (EmployeeBO employeeBO : employeeBL.getAllEmployees().values()) {
            Row row = sheet.getRow(iRowCounter);
            if (row == null)
                row = sheet.createRow(iRowCounter);
            Cell cell = row.getCell(0);
            if (cell == null)
                cell = row.createCell(0);
            setCellBorder(wb, cell);
            cell.setCellValue(employeeBO.getEmployeeId());

            cell = row.getCell(1);
            if (cell == null)
                cell = row.createCell(1);
            setCellBorder(wb, cell);
            cell.setCellValue(employeeBO.getEmployeeUid());

            cell = row.getCell(2);
            if (cell == null)
                cell = row.createCell(2);
            setCellBorder(wb, cell);
            cell.setCellValue(employeeBO.getEmployeeName());

            cell = row.getCell(3);
            if (cell == null)
                cell = row.createCell(3);
            setCellBorder(wb, cell);
            cell.setCellValue(employeeBO.getEmail());

            cell = row.getCell(4);
            if (cell == null)
                cell = row.createCell(4);
            setCellBorder(wb, cell);
            cell.setCellValue(employeeBO.getManager());

            cell = row.getCell(5);
            if (cell == null)
                cell = row.createCell(5);
            setCellBorder(wb, cell);
            cell.setCellValue(employeeBO.getMobileNumber());

            cell = row.getCell(6);
            if (cell == null)
                cell = row.createCell(6);
            setCellBorder(wb, cell);
            cell.setCellValue(employeeBO.getExtensionNum());

            cell = row.getCell(7);
            if (cell == null)
                cell = row.createCell(7);
            setCellBorder(wb, cell);
            cell.setCellValue(employeeBO.getWorkspace());

            iRowCounter = iRowCounter + 1;
        }
        FileOutputStream fileOut = new FileOutputStream(file);
        wb.write(fileOut);
        fileOut.close();
        return new FileInputStream(file);
    } catch (Exception ex) {
        throw new ExceptionWrapper(ex);
    }

}

From source file:com.perceptive.epm.perkolcentral.bl.ExcelReportBL.java

public FileInputStream generateAllEmployeeTeamWiseReport(File file, Boolean isScrumTeam)
        throws ExceptionWrapper {
    try {//from w w w .j ava 2 s  .c o m
        FileInputStream inp = new FileInputStream(file);
        //InputStream inp = new FileInputStream("workbook.xlsx");

        Workbook wb = WorkbookFactory.create(inp);
        int iSheetCounter = 1;
        for (Integer groupID : employeeBL.getAllEmployeesKeyedByGroupId().keySet()) {
            GroupBO groupBO = groupsBL.getAllGroups().get(groupID);
            if (isScrumTeam && !groupBO.getRallyGroup())
                continue;
            if (!isScrumTeam && groupBO.getRallyGroup())
                continue;

            Sheet sheet = wb.cloneSheet(0);
            wb.setSheetName(wb.getSheetIndex(sheet), groupBO.getGroupName());
            //wb.setSheetName(iSheetCounter,groupBO.getGroupName());

            int iRowCounter = 1;
            for (EmployeeBO employeeBO : employeeBL.getAllEmployeesKeyedByGroupId().get(groupID)) {
                Row row = sheet.getRow(iRowCounter);
                if (row == null)
                    row = sheet.createRow(iRowCounter);
                Cell cell = row.getCell(0);
                if (cell == null)
                    cell = row.createCell(0);
                setCellBorder(wb, cell);
                cell.setCellValue(employeeBO.getEmployeeId());

                cell = row.getCell(1);
                if (cell == null)
                    cell = row.createCell(1);
                setCellBorder(wb, cell);
                cell.setCellValue(employeeBO.getEmployeeUid());

                cell = row.getCell(2);
                if (cell == null)
                    cell = row.createCell(2);
                setCellBorder(wb, cell);
                cell.setCellValue(employeeBO.getEmployeeName());

                cell = row.getCell(3);
                if (cell == null)
                    cell = row.createCell(3);
                setCellBorder(wb, cell);
                cell.setCellValue(employeeBO.getEmail());

                cell = row.getCell(4);
                if (cell == null)
                    cell = row.createCell(4);
                setCellBorder(wb, cell);
                cell.setCellValue(employeeBO.getManager());

                cell = row.getCell(5);
                if (cell == null)
                    cell = row.createCell(5);
                setCellBorder(wb, cell);
                cell.setCellValue(employeeBO.getMobileNumber());

                cell = row.getCell(6);
                if (cell == null)
                    cell = row.createCell(6);
                setCellBorder(wb, cell);
                cell.setCellValue(employeeBO.getExtensionNum());

                cell = row.getCell(7);
                if (cell == null)
                    cell = row.createCell(7);
                setCellBorder(wb, cell);
                cell.setCellValue(employeeBO.getWorkspace());

                if (isScrumTeam) {
                    cell = row.getCell(8);
                    if (cell == null)
                        cell = row.createCell(8);
                    setCellBorder(wb, cell);
                    cell.setCellValue(employeeBO.getSpecificRoleInScrumTeam());
                }

                iRowCounter = iRowCounter + 1;
            }
        }
        iSheetCounter = iSheetCounter + 1;
        wb.removeSheetAt(0);
        FileOutputStream fileOut = new FileOutputStream(file);
        wb.write(fileOut);
        fileOut.close();
        return new FileInputStream(file);
    } catch (Exception ex) {
        throw new ExceptionWrapper(ex);
    }

}

From source file:com.perceptive.epm.perkolcentral.bl.ExcelReportBL.java

public FileInputStream generateLicenseReport(File file) throws ExceptionWrapper {
    try {/* w  ww . ja  v  a  2s . co m*/
        FileInputStream inp = new FileInputStream(file);
        //InputStream inp = new FileInputStream("workbook.xlsx");

        Workbook wb = WorkbookFactory.create(inp);
        HashMap<String, ArrayList<String>> licenseInfoKeyedByLicenseName = licensesBL.getLicenseRelatedInfo();
        //Create The Summary Sheet
        Sheet sheetSummary = wb.getSheetAt(1);
        int iSummaryRowCounter = 1;
        for (String licenseType : licenseInfoKeyedByLicenseName.keySet()) {

            Row row = sheetSummary.getRow(iSummaryRowCounter);
            if (row == null)
                row = sheetSummary.createRow(iSummaryRowCounter);
            Cell cell = row.getCell(0);
            if (cell == null)
                cell = row.createCell(0);
            setCellBorder(wb, cell);
            cell.setCellValue(licenseType);

            row = sheetSummary.getRow(iSummaryRowCounter);
            if (row == null)
                row = sheetSummary.createRow(iSummaryRowCounter);
            cell = row.getCell(1);
            if (cell == null)
                cell = row.createCell(1);
            setCellBorder(wb, cell);
            cell.setCellValue(Double.parseDouble(licenseInfoKeyedByLicenseName.get(licenseType).get(0)));
            cell.setCellType(Cell.CELL_TYPE_NUMERIC);

            row = sheetSummary.getRow(iSummaryRowCounter);
            if (row == null)
                row = sheetSummary.createRow(iSummaryRowCounter);
            cell = row.getCell(2);
            if (cell == null)
                cell = row.createCell(2);
            setCellBorder(wb, cell);
            cell.setCellValue(Double.parseDouble(licenseInfoKeyedByLicenseName.get(licenseType).get(1)));
            cell.setCellType(Cell.CELL_TYPE_NUMERIC);

            row = sheetSummary.getRow(iSummaryRowCounter);
            if (row == null)
                row = sheetSummary.createRow(iSummaryRowCounter);
            cell = row.getCell(3);
            if (cell == null)
                cell = row.createCell(3);
            setCellBorder(wb, cell);
            cell.setCellValue(Double.parseDouble(licenseInfoKeyedByLicenseName.get(licenseType).get(0))
                    - Double.parseDouble(licenseInfoKeyedByLicenseName.get(licenseType).get(1)));
            cell.setCellType(Cell.CELL_TYPE_NUMERIC);

            iSummaryRowCounter = iSummaryRowCounter + 1;
        }
        int iSheetCounter = 1;

        for (String licenseType : licenseInfoKeyedByLicenseName.keySet()) {
            Sheet sheet = wb.cloneSheet(0);
            wb.setSheetName(wb.getSheetIndex(sheet), licenseType);
            CellReference cellReference = new CellReference("B1");
            Row row = sheet.getRow(cellReference.getRow());
            Cell cell = row.getCell(cellReference.getCol());
            if (cell == null)
                cell = row.createCell(cellReference.getCol());
            setCellBorder(wb, cell);
            cell.setCellValue(Double.parseDouble(licenseInfoKeyedByLicenseName.get(licenseType).get(0)));
            cell.setCellType(Cell.CELL_TYPE_NUMERIC);

            cellReference = new CellReference("B2");
            row = sheet.getRow(cellReference.getRow());
            cell = row.getCell(cellReference.getCol());
            if (cell == null)
                cell = row.createCell(cellReference.getCol());
            setCellBorder(wb, cell);
            cell.setCellValue(Double.parseDouble(licenseInfoKeyedByLicenseName.get(licenseType).get(1)));
            cell.setCellType(Cell.CELL_TYPE_NUMERIC);

            cellReference = new CellReference("B3");
            row = sheet.getRow(cellReference.getRow());
            cell = row.getCell(cellReference.getCol());
            if (cell == null)
                cell = row.createCell(cellReference.getCol());
            setCellBorder(wb, cell);
            cell.setCellValue(Double.parseDouble(licenseInfoKeyedByLicenseName.get(licenseType).get(0))
                    - Double.parseDouble(licenseInfoKeyedByLicenseName.get(licenseType).get(1)));
            cell.setCellType(Cell.CELL_TYPE_NUMERIC);

            ArrayList<EmployeeBO> allEmployees = new ArrayList<EmployeeBO>(
                    employeeBL.getAllEmployees().values());
            final String selectedLicenseTypeName = licenseType;
            CollectionUtils.filter(allEmployees, new Predicate() {
                @Override
                public boolean evaluate(Object o) {
                    EmployeeBO emp = (EmployeeBO) o;
                    if (CollectionUtils.exists(emp.getLicenses(), new Predicate() {
                        @Override
                        public boolean evaluate(Object o) {
                            return ((LicenseBO) o).getLicenseTypeName()
                                    .equalsIgnoreCase(selectedLicenseTypeName); //To change body of implemented methods use File | Settings | File Templates.
                        }
                    }))
                        return true;
                    else
                        return false;
                }
            });

            int iRowCounter = 5;
            for (EmployeeBO employeeBO : allEmployees) {
                row = sheet.getRow(iRowCounter);
                if (row == null)
                    row = sheet.createRow(iRowCounter);
                cell = row.getCell(0);
                if (cell == null)
                    cell = row.createCell(0);
                setCellBorder(wb, cell);
                cell.setCellValue(employeeBO.getEmployeeId());

                cell = row.getCell(1);
                if (cell == null)
                    cell = row.createCell(1);
                setCellBorder(wb, cell);
                cell.setCellValue(employeeBO.getEmployeeUid());

                cell = row.getCell(2);
                if (cell == null)
                    cell = row.createCell(2);
                setCellBorder(wb, cell);
                cell.setCellValue(employeeBO.getEmployeeName());

                cell = row.getCell(3);
                if (cell == null)
                    cell = row.createCell(3);
                setCellBorder(wb, cell);
                cell.setCellValue(employeeBO.getEmail());

                cell = row.getCell(4);
                if (cell == null)
                    cell = row.createCell(4);
                setCellBorder(wb, cell);
                cell.setCellValue(employeeBO.getManager());

                cell = row.getCell(5);
                if (cell == null)
                    cell = row.createCell(5);
                setCellBorder(wb, cell);
                cell.setCellValue(employeeBO.getMobileNumber());

                cell = row.getCell(6);
                if (cell == null)
                    cell = row.createCell(6);
                setCellBorder(wb, cell);
                cell.setCellValue(employeeBO.getExtensionNum());

                cell = row.getCell(7);
                if (cell == null)
                    cell = row.createCell(7);
                setCellBorder(wb, cell);
                cell.setCellValue(employeeBO.getWorkspace());

                iRowCounter = iRowCounter + 1;
            }
        }
        iSheetCounter = iSheetCounter + 1;
        wb.removeSheetAt(0);
        FileOutputStream fileOut = new FileOutputStream(file);
        wb.write(fileOut);
        fileOut.close();
        return new FileInputStream(file);
    } catch (Exception ex) {
        throw new ExceptionWrapper(ex);
    }

}

From source file:com.plugin.excel.util.ExcelFileHelper.java

License:Apache License

/**
 * It generates new Excel file at given locaiton
 * /*from   w w w. ja  va 2s.c om*/
 * @param directory
 * @param fileName
 * @param workbook
 */
private static void writeWorkBook(String directory, String fileName, Workbook workbook) {

    try {
        File dir = new File(directory);
        if (!dir.exists()) {
            dir.mkdir();
        }
        File file = new File(directory + "/" + fileName);
        if (file.exists()) {
            file.delete();
        }
        FileOutputStream out = new FileOutputStream(file);
        workbook.write(out);
        out.close();
    } catch (Exception e) {
        String msg = "File creation fialed for dir,fileName: " + directory + "," + fileName + e.getMessage();
        throw new IllegalStateException(msg, e);
    }

}

From source file:com.qihang.winter.poi.excel.imports.base.ImportBaseService.java

License:Apache License

/**
 * isNeedSave()excel?/*w w  w .jav a 2  s .com*/
 * @param params
 * @param pojoClass
 * @param isXSSFWorkbook
 * @param book
 * @return
 * @throws Exception
 */
public String saveThisExcel(com.qihang.winter.poi.excel.entity.ImportParams params, Class<?> pojoClass,
        boolean isXSSFWorkbook, Workbook book) throws Exception {
    String path = PoiPublicUtil.getWebRootPath(getSaveExcelUrl(params, pojoClass));
    File savefile = new File(path);
    if (!savefile.exists()) {
        savefile.mkdirs();
    }
    SimpleDateFormat format = new SimpleDateFormat("yyyMMddHHmmss");
    String excelName = format.format(new Date()) + "_" + Math.round(Math.random() * 100000)
            + (isXSSFWorkbook == true ? ".xlsx" : ".xls");
    FileOutputStream fos = new FileOutputStream(path + "/" + excelName);
    book.write(fos);
    fos.close();
    return excelName;
}

From source file:com.qihang.winter.poi.excel.view.JeecgMapExcelView.java

License:Apache License

@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    String codedFileName = "";
    Workbook workbook = ExcelExportUtil.exportExcel((ExportParams) model.get(MapExcelConstants.PARAMS),
            (List<ExcelExportEntity>) model.get(MapExcelConstants.ENTITY_LIST),
            (Collection<? extends Map<?, ?>>) model.get(MapExcelConstants.MAP_LIST));
    if (model.containsKey(MapExcelConstants.FILE_NAME)) {
        codedFileName = (String) model.get(MapExcelConstants.FILE_NAME);
    }/*from w w w  . j av a  2s.  com*/
    if (workbook instanceof HSSFWorkbook) {
        codedFileName += HSSF;
    } else {
        codedFileName += XSSF;
    }
    if (isIE(request)) {
        codedFileName = java.net.URLEncoder.encode(codedFileName, "UTF8");
    } else {
        codedFileName = new String(codedFileName.getBytes("UTF-8"), "ISO-8859-1");
    }
    response.setHeader("content-disposition", "attachment;filename=" + codedFileName);
    ServletOutputStream out = response.getOutputStream();
    workbook.write(out);
    out.flush();
}