Example usage for org.apache.poi.xssf.streaming SXSSFWorkbook SXSSFWorkbook

List of usage examples for org.apache.poi.xssf.streaming SXSSFWorkbook SXSSFWorkbook

Introduction

In this page you can find the example usage for org.apache.poi.xssf.streaming SXSSFWorkbook SXSSFWorkbook.

Prototype

public SXSSFWorkbook(XSSFWorkbook workbook, int rowAccessWindowSize, boolean compressTmpFiles,
        boolean useSharedStringsTable) 

Source Link

Document

Constructs an workbook from an existing workbook.

Usage

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

License:Open Source License

public static boolean exportDatastore(ODLDatastore<? extends ODLTableReadOnly> ds, File file, boolean xlsx,
        ProcessingApi processing, ExecutionReport report) {
    //tmpFileBugFix();

    Workbook wb = null;//from w  w  w  .  jav a  2  s.c o  m
    SXSSFWorkbook sxssfwb = null;
    HSSFWorkbook hssfwb = null;
    if (xlsx == false) {
        hssfwb = new HSSFWorkbook();
        hssfwb.createInformationProperties();
        hssfwb.getSummaryInformation().setAuthor(AppConstants.ORG_NAME);
        wb = hssfwb;
    } else {
        //   sxssfwb = new SXSSFWorkbook(100); // keep 100 rows in memory, exceeding rows will be flushed to disk
        sxssfwb = new SXSSFWorkbook(null, 100, false, true);
        wb = sxssfwb;

        //   XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
        ///   POIXMLProperties xmlProps = sxssfwb.
        //POIXMLProperties.CoreProperties coreProps = xmlProps.getCoreProperties();
        //   coreProps.setCreator(AppConstants.ORG_NAME);
        //   wb = xssfWorkbook;
    }

    try {
        // save schema
        addSchema(ds, wb);

        for (ODLTableDefinition table : TableUtils.getAlphabeticallySortedTables(ds)) {
            ODLTableReadOnly tro = (ODLTableReadOnly) table;
            Sheet sheet = wb.createSheet(tro.getName());
            if (sheet == null) {
                return false;
            }

            exportTable(sheet, tro, 0, processing, report);

            if (processing != null && processing.isCancelled()) {
                return false;
            }
        }

        if (processing != null) {
            processing.postStatusMessage("Saving whole workbook to disk.");
        }

        saveWorkbook(file, wb);

    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (sxssfwb != null) {
            sxssfwb.dispose();
        }

        if (hssfwb != null) {
            try {
                hssfwb.close();
            } catch (Exception e2) {
                // TODO: handle exception
            }
        }
    }

    return true;
}

From source file:com.rapidminer.operator.io.ExcelExampleSetWriter.java

License:Open Source License

/**
 * Writes the example set into a excel file with XLSX format. If you want to write it in XLS
 * format use {@link #write(ExampleSet, Charset, OutputStream)}.
 *
 * @param exampleSet//from  w  ww  . ja  v  a  2  s . co m
 *            the exampleSet to write
 * @param sheetName
 *            name of the excel sheet which will be created.
 * @param dateFormat
 *            a string which describes the format used for dates.
 * @param numberFormat
 *            a string which describes the format used for numbers.
 * @param outputStream
 *            the stream to write the file to
 * @param op
 *            needed for checkForStop
 */
public static void writeXLSX(ExampleSet exampleSet, String sheetName, String dateFormat, String numberFormat,
        OutputStream outputStream, Operator op) throws WriteException, IOException, ProcessStoppedException {
    // .xlsx files can only store up to 16384 columns, so throw error in case of more
    if (exampleSet.getAttributes().allSize() > 16384) {
        throw new IllegalArgumentException(
                I18N.getMessage(I18N.getErrorBundle(), "export.excel.excel_xlsx_file_exceeds_column_limit"));
    }

    try (SXSSFWorkbook workbook = new SXSSFWorkbook(null, SXSSFWorkbook.DEFAULT_WINDOW_SIZE, false, true)) {
        Sheet sheet = workbook.createSheet(WorkbookUtil.createSafeSheetName(sheetName));

        dateFormat = dateFormat == null ? DEFAULT_DATE_FORMAT : dateFormat;

        numberFormat = numberFormat == null ? "#.0" : numberFormat;
        writeXLSXDataSheet(workbook, sheet, dateFormat, numberFormat, exampleSet, op);
        workbook.write(outputStream);
    } finally {
        outputStream.flush();
        outputStream.close();
    }
}

From source file:com.squid.core.poi.ExcelWriter.java

License:Open Source License

public ExcelWriter(OutputStream stream, ExcelSettingsBean settings) {
    this.stream = stream;
    this.insertHeader = settings.isInsertHeader();

    // NPE can occur in XMLBeans but no impact
    if (ExcelFile.XLSX.equals(settings.getExcelFile())) {
        excelFile = ExcelFile.XLSX;//from   www .  ja  v a  2 s .c o m
        wb = new SXSSFWorkbook(null, ROW_ACCESS_WINDOW_SIZE, true, true);
    } else {
        wb = new HSSFWorkbook();
    }
    maxRows = getMaxRows();

    dataFormat = wb.createDataFormat();
    sheet = wb.createSheet();
}

From source file:ec.tss.sa.output.SpreadsheetOutput.java

License:EUPL

@Override
public void end(Object context) throws Exception {
    String file = new File(folder_, config_.getFileName()).getAbsolutePath();
    file = Paths.changeExtension(file, "xlsx");
    File ssfile = new File(file);
    //File ssfile = new File("C:\\test.xls");
    SXSSFWorkbook workbook = new SXSSFWorkbook(null, 100, false, true);

    try (FileOutputStream stream = new FileOutputStream(ssfile)) {
        switch (config_.getLayout()) {
        case ByComponent: {
            HashMap<String, List<NamedObject<TsData>>> allData = new HashMap<>();
            for (DefaultSummary summary : summaries_) {
                for (Entry<String, TsData> keyValue : summary.getAllSeries().entrySet()) {
                    List<NamedObject<TsData>> list = null;
                    if (!allData.containsKey(keyValue.getKey())) {
                        list = new ArrayList<>();
                        allData.put(keyValue.getKey(), list);
                    } else {
                        list = allData.get(keyValue.getKey());
                    }//from   www.  jav a  2 s  . c om
                    String name;

                    if (fullName) {
                        name = MultiLineNameUtil.join(summary.getName(), " * ");
                    } else {
                        name = MultiLineNameUtil.last(summary.getName());
                    }
                    list.add(new NamedObject<>(name, keyValue.getValue()));
                }
            }
            for (Entry<String, List<NamedObject<TsData>>> keyValue : allData.entrySet()) {
                TsDataTable byComponentTable = new TsDataTable();
                List<NamedObject<TsData>> value = keyValue.getValue();
                String[] headers = new String[value.size()];
                for (int i = 0; i < headers.length; i++) {
                    NamedObject<TsData> data = value.get(i);
                    headers[i] = data.name;
                    byComponentTable.insert(-1, data.object);
                }
                //ADD SHEET
                XSSFHelper.addSheet(workbook, keyValue.getKey(), new String[] { keyValue.getKey() }, headers,
                        byComponentTable, config_.isVerticalOrientation());
            }
            break;
        }
        case BySeries: {
            for (int i = 0; i < summaries_.size(); i++) {
                DefaultSummary summary = summaries_.get(i);
                Set<Entry<String, TsData>> tmp = summary.getAllSeries().entrySet();
                TsDataTable bySeriesTable = new TsDataTable();
                String[] componentHeaders = new String[tmp.size()];
                int j = 0;
                for (Entry<String, TsData> keyValue : tmp) {
                    componentHeaders[j++] = keyValue.getKey();
                    bySeriesTable.insert(-1, keyValue.getValue());
                }
                //ADD SHEET
                String name;
                if (fullName) {
                    name = MultiLineNameUtil.join(summary.getName(), " * ");
                } else {
                    name = MultiLineNameUtil.last(summary.getName());
                }
                XSSFHelper.addSheet(workbook, "Series" + Integer.toString(i), new String[] { name },
                        componentHeaders, bySeriesTable, config_.isVerticalOrientation());
            }
            break;
        }
        case OneSheet: {
            List<String> headers0 = new ArrayList<>();
            List<String> headers1 = new ArrayList<>();
            TsDataTable oneSheetTable = new TsDataTable();

            for (DefaultSummary summary : summaries_) {
                String name;
                if (fullName) {
                    name = MultiLineNameUtil.join(summary.getName(), " * ");
                } else {
                    name = MultiLineNameUtil.last(summary.getName());
                }
                headers0.add(name);
                Map<String, TsData> data = summary.getAllSeries();
                for (Entry<String, TsData> keyValue : data.entrySet()) {
                    headers1.add(keyValue.getKey());
                    oneSheetTable.insert(-1, keyValue.getValue());
                }
                for (int i = 1; i < data.size(); i++) {
                    headers0.add("");
                }
            }
            //ADD SHEET
            XSSFHelper.addSheet(workbook, "Series", Iterables.toArray(headers0, String.class),
                    Iterables.toArray(headers1, String.class), oneSheetTable, config_.isVerticalOrientation());
            break;
        }
        }
        workbook.write(stream);
    } finally {
        workbook.dispose();
    }
}

From source file:ec.util.spreadsheet.poi.ExcelBookFactory.java

License:EUPL

@Override
public void store(OutputStream stream, Book book) throws IOException {
    // Currenty, inline string is not supported in FastPoiBook -> use of shared strings table
    SXSSFWorkbook target = new SXSSFWorkbook(null, 100, false, true);
    try {/*  ww w .  j a v  a2  s  . c  o m*/
        PoiBookWriter.copy(book, target);
        target.write(stream);
    } finally {
        // dispose of temporary files backing this workbook on disk
        target.dispose();
    }
}

From source file:org.apache.tika.eval.reports.Report.java

License:Apache License

private void dumpXLSX(Connection c, Path reportsRoot) throws IOException, SQLException {
    Statement st = c.createStatement();
    Path out = reportsRoot.resolve(reportFilename);
    Files.createDirectories(out.getParent());

    SXSSFWorkbook wb = new SXSSFWorkbook(new XSSFWorkbook(), 100, true, true);
    wb.setCompressTempFiles(true);/*from w w  w.  j  a v a2 s . c o  m*/
    defaultIntegerFormatter.reset(wb.getXSSFWorkbook());
    defaultDoubleFormatter.reset(wb.getXSSFWorkbook());
    sqlCellStyle = wb.createCellStyle();
    sqlCellStyle.setVerticalAlignment(VerticalAlignment.TOP);
    sqlCellStyle.setWrapText(true);

    try {
        dumpReportToWorkbook(st, wb);
    } finally {
        try (OutputStream os = Files.newOutputStream(out)) {
            wb.write(os);
        } finally {
            wb.dispose();
        }
    }
}