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:org.datanucleus.store.excel.AbstractExcelManagedConnection.java

License:Open Source License

public Object getConnection() {
    if (conn == null) {
        try {//from   w  w  w .j a v  a  2s. c o  m
            file = new File(filename);
            if (!file.exists()) {
                // Excel document doesn't exist, so create
                Workbook wb = getWorkbook();
                FileOutputStream fileOut = new FileOutputStream(file);
                wb.write(fileOut);
                fileOut.close();
            }

            conn = getWorkbook(new FileInputStream(file));
        } catch (IOException e) {
            throw new NucleusException(e.getMessage(), e);
        }
    }
    return conn;
}

From source file:org.dbunit.dataset.excel.XlsDataSetWriter.java

License:Open Source License

/**
 * Write the specified dataset to the specified Excel document.
 *///from www  .  jav  a 2  s. com
public void write(IDataSet dataSet, OutputStream out) throws IOException, DataSetException {
    logger.debug("write(dataSet={}, out={}) - start", dataSet, out);

    Workbook workbook = createWorkbook();

    this.dateCellStyle = createDateCellStyle(workbook);

    int index = 0;
    ITableIterator iterator = dataSet.iterator();
    while (iterator.next()) {
        // create the table i.e. sheet
        ITable table = iterator.getTable();
        ITableMetaData metaData = table.getTableMetaData();
        Sheet sheet = workbook.createSheet(metaData.getTableName());

        // write table metadata i.e. first row in sheet
        workbook.setSheetName(index, metaData.getTableName());

        Row headerRow = sheet.createRow(0);
        Column[] columns = metaData.getColumns();
        for (int j = 0; j < columns.length; j++) {
            Column column = columns[j];
            Cell cell = headerRow.createCell(j);
            cell.setCellValue(column.getColumnName());
        }

        // write table data
        for (int j = 0; j < table.getRowCount(); j++) {
            Row row = sheet.createRow(j + 1);
            for (int k = 0; k < columns.length; k++) {
                Column column = columns[k];
                Object value = table.getValue(j, column.getColumnName());
                if (value != null) {
                    Cell cell = row.createCell(k);
                    if (value instanceof Date) {
                        setDateCell(cell, (Date) value, workbook);
                    } else if (value instanceof BigDecimal) {
                        setNumericCell(cell, (BigDecimal) value, workbook);
                    } else if (value instanceof Long) {
                        setDateCell(cell, new Date(((Long) value).longValue()), workbook);
                    } else {
                        cell.setCellValue(DataType.asString(value));
                    }
                }
            }
        }

        index++;
    }

    // write xls document
    workbook.write(out);
    out.flush();
}

From source file:org.dhatim.fastexcel.Benchmarks.java

License:Apache License

private int poiPopulate(org.apache.poi.ss.usermodel.Workbook wb) throws Exception {
    Sheet ws = wb.createSheet("Sheet 1");
    CellStyle dateStyle = wb.createCellStyle();
    dateStyle.setDataFormat(wb.getCreationHelper().createDataFormat().getFormat("yyyy-mm-dd hh:mm:ss"));
    for (int r = 0; r < NB_ROWS; ++r) {
        Row row = ws.createRow(r);//from  ww  w.j  a va2s. co m
        row.createCell(0).setCellValue(r);
        row.createCell(1).setCellValue(Integer.toString(r % 1000));
        row.createCell(2).setCellValue(r / 87.0);
        Cell c = row.createCell(3);
        c.setCellStyle(dateStyle);
        c.setCellValue(new Date(1549915044));
    }
    CountingOutputStream count = new CountingOutputStream(new NullOutputStream());
    wb.write(count);
    return count.getCount();
}

From source file:org.dkpro.lab.reporting.FlexTable.java

License:Apache License

public StreamWriter getExcelWriter() {
    return new StreamWriter() {
        @Override/*from w w  w  .ja va  2s. c o m*/
        public void write(OutputStream aStream) throws Exception {
            String[] colIds = FlexTable.this.compact ? getCompactColumnIds(false) : getColumnIds();

            Workbook wb = new HSSFWorkbook();
            Sheet sheet = wb.createSheet("Summary");

            PrintSetup printSetup = sheet.getPrintSetup();
            printSetup.setLandscape(true);
            sheet.setFitToPage(true);
            sheet.setHorizontallyCenter(true);

            // Header row
            {
                Row row = sheet.createRow(0);
                Cell rowIdCell = row.createCell(0);
                rowIdCell.setCellValue("ID");

                int colNum = 1;
                for (String colId : colIds) {
                    Cell cell = row.createCell(colNum);
                    cell.setCellValue(colId);
                    colNum++;
                }
            }

            // Body rows
            {
                int rowNum = 1;
                for (String rowId : getRowIds()) {
                    Row row = sheet.createRow(rowNum);
                    Cell rowIdCell = row.createCell(0);
                    rowIdCell.setCellValue(rowId);

                    int colNum = 1;
                    for (String colId : colIds) {
                        Cell cell = row.createCell(colNum);
                        String value = getValueAsString(rowId, colId);
                        try {
                            cell.setCellValue(Double.valueOf(value));
                        } catch (NumberFormatException e) {
                            cell.setCellValue(value);
                        }
                        colNum++;
                    }
                    rowNum++;
                }
            }

            wb.write(aStream);
        }
    };
}

From source file:org.drools.workbench.screens.guided.dtable.backend.server.conversion.DecisionTableGuidedToDecisionTableXLSConverter.java

License:Apache License

public XLSConversionResult convert(final Path originPath) throws IOException {
    final PackageDataModelOracle dmo = dataModelService.getDataModel(originPath);
    final GuidedDecisionTable52 dtable = guidedDecisionTableEditorService.load(originPath);
    final XLSBuilder.BuildResult buildResult = new XLSBuilder(dtable, dmo).build();

    if (buildResult.getConversionResult().isConverted()) {

        final Workbook workbook = buildResult.getWorkbook();

        final ByteArrayOutputStream fileOut = new ByteArrayOutputStream();
        workbook.write(fileOut);

        final ByteArrayInputStream inStream = new ByteArrayInputStream(fileOut.toByteArray());

        decisionTableXLSService.create(getDestinationFilePath(originPath), inStream, sessionInfo.getId(),
                "Converted from " + originPath.getFileName());

        fileOut.close();/*  w w  w. ja v  a 2s.  c om*/
        workbook.close();
        inStream.close();
    }

    return buildResult.getConversionResult();
}

From source file:org.drugepi.table.TableCreator.java

License:Mozilla Public License

/**
 * Replace the shell table in an Excel file with data from tables.
 * //from   ww w.java2  s .co m
 * @param inWorkbookName  Name of the Excel workbook (.xls or .xlsx) with the defined tables.
 * @param outWorkbookName  Name of the Excel workbook (.xls or .xlsx) to use for output.  Any existing file will be replaced.
 * @throws Exception
 */
public void writeTablesToWorkbook(String inWorkbookName, String outWorkbookName) throws Exception {
    InputStream fileIn = new FileInputStream(inWorkbookName);

    Workbook workbook = WorkbookFactory.create(fileIn);
    for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
        Sheet sheet = workbook.getSheetAt(i);
        String tableId = TableElement.makeId(sheet.getSheetName());
        Table t = this.tables.get(tableId);
        if (t != null)
            this.fillTableInSheet(sheet, t);
    }

    FileOutputStream fileOut = new FileOutputStream(outWorkbookName);
    workbook.write(fileOut);
    fileOut.close();
}

From source file:org.eclipse.emfforms.internal.spreadsheet.file.EMFFormsSpreadsheetFileExporterImpl.java

License:Open Source License

@Override
public void render(File file, Collection<EObject> domainObjects, EObject viewEobject,
        VViewModelProperties properties) {
    final Workbook workbook = EMFFormsSpreadsheetExporter.INSTANCE.render(domainObjects, viewEobject,
            properties);//from w ww. j  a va  2s.com

    final BundleContext bundleContext = FrameworkUtil.getBundle(getClass()).getBundleContext();
    final ServiceReference<ReportService> serviceReference = bundleContext
            .getServiceReference(ReportService.class);
    final ReportService reportService = bundleContext.getService(serviceReference);

    FileOutputStream outputStream = null;
    try {
        outputStream = new FileOutputStream(file);
        workbook.write(outputStream);
    } catch (final IOException ex) {
        reportService.report(new EMFFormsSpreadsheetReport(ex, 4));
    } finally {
        try {
            outputStream.close();
        } catch (final IOException ex) {
            reportService.report(new EMFFormsSpreadsheetReport(ex, 4));
        }
    }
    bundleContext.ungetService(serviceReference);
}

From source file:org.eclipse.emfforms.internal.spreadsheet.stream.EMFFormsSpreadsheetStreamExporterImpl.java

License:Open Source License

@Override
public void render(OutputStream outputStream, Collection<EObject> domainObjects, EObject viewEobject,
        VViewModelProperties properties) {
    final Workbook workbook = EMFFormsSpreadsheetExporter.INSTANCE.render(domainObjects, viewEobject,
            properties);/*  w w w . j  av a  2  s  .c  o  m*/

    final BundleContext bundleContext = FrameworkUtil.getBundle(getClass()).getBundleContext();
    final ServiceReference<ReportService> serviceReference = bundleContext
            .getServiceReference(ReportService.class);
    final ReportService reportService = bundleContext.getService(serviceReference);

    try {
        workbook.write(outputStream);
    } catch (final IOException ex) {
        reportService.report(new EMFFormsSpreadsheetReport(ex, 4));
    } finally {
        try {
            outputStream.close();
        } catch (final IOException ex) {
            reportService.report(new EMFFormsSpreadsheetReport(ex, 4));
        }
    }
    bundleContext.ungetService(serviceReference);
}

From source file:org.eclipse.emfforms.spreadsheet.integrationtest.Roundtrip_ITest.java

License:Open Source License

private void saveWorkbook(Workbook wb, String absolutePath) {
    FileOutputStream fileOut = null;
    try {//from  w w  w.  j a v  a 2s.  c  om
        fileOut = new FileOutputStream(absolutePath);
        wb.write(fileOut);
    } catch (final FileNotFoundException e) {
        e.printStackTrace();
    } catch (final IOException e) {
        e.printStackTrace();
    } finally {
        try {
            fileOut.close();
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }
}