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() 

Source Link

Document

Construct a new workbook with default row window size

Usage

From source file:info.informationsea.tableio.excel.test.ExcelImageWriterTest.java

License:Open Source License

@Test
public void testWriteImage() throws Exception {
    File buildDir = new File(System.getProperty("user.dir"), "build");
    File testOutput = new File(buildDir, "test-data");
    testOutput.mkdirs();/*  ww w.j  ava2s .c  om*/

    SXSSFWorkbook workbook = new SXSSFWorkbook();
    TableWorkbookWriter workbookWriter = new ExcelWorkbookWriter(workbook);
    ImageSheetWriter imageSheetWriter = workbookWriter.createImageSheet("testsheet");
    imageSheetWriter.addImage(ExcelImageSheetWriter.ImageType.TYPE_JPEG,
            IOUtils.toByteArray(getClass().getResourceAsStream("ashinari-osaka.jpg")));
    imageSheetWriter.addImage(ExcelImageSheetWriter.ImageType.TYPE_PNG,
            IOUtils.toByteArray(getClass().getResourceAsStream("ashinari-momiji.png")));

    List<? extends PictureData> list = workbook.getAllPictures();
    for (PictureData one : list) {
        if (one.suggestFileExtension().endsWith("jpeg")) {
            Assert.assertArrayEquals(IOUtils.toByteArray(getClass().getResourceAsStream("ashinari-osaka.jpg")),
                    one.getData());
        } else if (one.suggestFileExtension().endsWith("png")) {
            Assert.assertArrayEquals(IOUtils.toByteArray(getClass().getResourceAsStream("ashinari-momiji.png")),
                    one.getData());
        } else {
            Assert.fail();
        }
    }

    try (FileOutputStream outputStream = new FileOutputStream(new File(testOutput, "image.xlsx"))) {
        workbook.write(outputStream);
    }
}

From source file:info.informationsea.tableio.excel.XlsxWorkbookWriter.java

License:Open Source License

public XlsxWorkbookWriter(OutputStream outputStream) {
    super(new SXSSFWorkbook());
    this.outputStream = outputStream;
}

From source file:info.informationsea.tableio.excel.XlsxWriter.java

License:Open Source License

public XlsxWriter(OutputStream os) {
    workbook = new SXSSFWorkbook();
    setSheet(workbook.createSheet());
    outputStream = os;
}

From source file:info.informationsea.tableio.excel.XlsxWriter.java

License:Open Source License

public XlsxWriter(OutputStream os, String sheetName) {
    workbook = new SXSSFWorkbook();
    setSheet(workbook.createSheet(sheetName));
    outputStream = os;
}

From source file:info.informationsea.venn.CombinationImporterExporter.java

License:Open Source License

public static <T, U> void export(File file, CombinationSolver<T, U> combinationSolver,
        List<VennFigureParameters.Attribute<T>> keyList) throws Exception {
    if (file.getName().endsWith(".csv")) {
        try (FileWriter writer = new FileWriter(file); TableCSVWriter csvWriter = new TableCSVWriter(writer)) {
            export(csvWriter, combinationSolver, keyList);
        }/*from  w w w . j  a  v a 2 s.c  om*/
    } else if (file.getName().endsWith(".xlsx") || file.getName().endsWith(".xls")) {
        Workbook workbook;
        if (file.getName().endsWith(".xlsx"))
            workbook = new SXSSFWorkbook();
        else
            workbook = new HSSFWorkbook();

        // Export dataset
        Sheet sheet = workbook.createSheet("Data");
        try (ExcelSheetWriter writer = new ExcelSheetWriter(sheet)) {
            writer.setPrettyTable(true);
            export(writer, combinationSolver, keyList);
        }

        // Export combinations
        sheet = workbook.createSheet("Combinations");
        try (ExcelSheetWriter writer = new ExcelSheetWriter(sheet)) {
            writer.setPrettyTable(false);
            writer.setEnableHeaderStyle(true);
            writer.setAlternativeBackground(true);

            writer.printRecord("Combination", "# of items", "items");

            for (Map.Entry<Set<T>, Set<U>> entry : combinationSolver.getCombinationResult().entrySet()) {
                String keys = "";
                for (T one : entry.getKey()) {
                    if (keys.length() > 0)
                        keys += ", ";
                    keys += one.toString();
                }

                List<Object> row = new ArrayList<>();
                row.add(keys);
                row.add(entry.getValue().size());

                for (U one : entry.getValue()) {
                    row.add(one.toString());
                }

                writer.printRecord(row.toArray());
            }
        }

        // Export venn diagram
        switch (combinationSolver.getValues().size()) {
        case 2:
        case 3:
        case 4:
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            VennExporter.exportAsPNG(new VennFigureParameters<T>(combinationSolver, keyList), outputStream, 800,
                    10);
            outputStream.close();

            sheet = workbook.createSheet("Venn");
            try (ExcelImageSheetWriter imageSheetWriter = new ExcelImageSheetWriter(sheet)) {
                imageSheetWriter.addImage(ImageSheetWriter.ImageType.TYPE_PNG, outputStream.toByteArray());
            }
            break;
        default: // Do nothing
            break;
        }

        try (FileOutputStream fos = new FileOutputStream(file)) {
            workbook.write(fos);
        }

    } else {
        throw new IllegalArgumentException("Unsupported file type");
    }
}

From source file:it.isislab.sof.client.application.ui.Controller.java

public void getresultExcel(String... params) {

    if (params == null) {
        //            c.printf("Error few parameters!\n Usage: getresult simID [destinationDirPath]");

    } else {//from ww  w  .  j a v  a2s. co m
        Simulations sims = SofManager.getSimulationsData(session);
        if (sims == null) {
            //               c.printf("No such simulation");

        }
        Simulation sim = null;
        try {
            for (Simulation s : sims.getSimulations())
                if (s.getId().equals(params[0])) {
                    sim = s;
                    break;
                }
        } catch (IndexOutOfBoundsException e) {
            //               c.printf("No such simulation");

        }
        //if no path is specified, saves in current directory
        String path = (params.length < 2) ? System.getProperty("user.dir") : params[1];
        path += File.separator + "SIM-" + sim.getId() + ".xls";

        SXSSFWorkbook workbook = new SXSSFWorkbook();
        Sheet sheet = workbook.createSheet("Simulation ID " + sim.getId());

        int row_num = 0;
        if (sim.getLoop()) {
            List<Loop> loops = sim.getRuns().getLoops();
            Collections.sort(loops, new Comparator<Loop>() {

                @Override
                public int compare(Loop o1, Loop o2) {
                    return Integer.compare(o1.getId(), o2.getId());
                }
            });
            for (Loop l : loops) {
                Row row_loop = sheet.createRow(++row_num);
                Cell c_loop_id = row_loop.createCell(0);
                c_loop_id.setCellValue("Loop ID " + l.getId());
                class PointTree {
                    public Input getI() {
                        return i;
                    }

                    public void setI(Input i) {
                        this.i = i;
                    }

                    public Output getO() {
                        return o;
                    }

                    public void setO(Output o) {
                        this.o = o;
                    }

                    private Input i;
                    private Output o;
                }
                HashMap<Integer, PointTree> mapio = new HashMap<Integer, PointTree>();

                if (l.getInputs() != null) {

                    for (Input i : l.getInputs().getinput_list()) {
                        PointTree p = new PointTree();
                        p.setI(i);
                        mapio.put(i.id, p);

                    }

                    if (l.getOutputs() != null && l.getOutputs().getOutput_list() != null)
                        for (Output i : l.getOutputs().getOutput_list()) {
                            mapio.get(i.getIdInput()).setO(i);
                        }
                    else {
                        System.out.println("No output found.");
                    }

                    for (Integer pt : mapio.keySet()) {

                        Row row_input_id = sheet.createRow(++row_num);
                        Cell c_input_id = row_input_id.createCell(1);
                        c_input_id.setCellValue("Input ID " + pt);

                        Row row_input_names = sheet.createRow(++row_num);
                        Row row_input_values = sheet.createRow(++row_num);

                        Row row_output_id = sheet.createRow(++row_num);
                        Cell c_output_id = row_output_id.createCell(1);
                        c_output_id.setCellValue("Output ID " + pt);

                        Row row_output_names = sheet.createRow(++row_num);
                        Row row_output_values = sheet.createRow(++row_num);

                        int cell_input = 1, cell_output = 1;
                        for (Parameter p : mapio.get(pt).getI().param_element) {
                            Cell c_input_name = row_input_names.createCell(cell_input);
                            Cell c_input_value = row_input_values.createCell(cell_input);
                            cell_input++;
                            c_input_name.setCellValue(p.getvariable_name());
                            if (p.getparam() instanceof ParameterDouble)
                                c_input_value.setCellValue(((ParameterDouble) p.getparam()).getvalue());
                            else if (p.getparam() instanceof ParameterString)
                                c_input_value.setCellValue(((ParameterString) p.getparam()).getvalue());
                            else if (p.getparam() instanceof ParameterLong)
                                c_input_value.setCellValue(((ParameterLong) p.getparam()).getvalue());

                        }
                        if (mapio.get(pt).getO() != null)
                            for (Parameter p : mapio.get(pt).getO().output_params) {
                                Cell c_output_name = row_output_names.createCell(cell_output);
                                Cell c_output_value = row_output_values.createCell(cell_output);
                                cell_output++;
                                c_output_name.setCellValue(p.getvariable_name());
                                if (p.getparam() instanceof ParameterDouble)
                                    c_output_value.setCellValue(((ParameterDouble) p.getparam()).getvalue());
                                else if (p.getparam() instanceof ParameterString)
                                    c_output_value.setCellValue(((ParameterString) p.getparam()).getvalue());
                                else if (p.getparam() instanceof ParameterLong)
                                    c_output_value.setCellValue(((ParameterLong) p.getparam()).getvalue());
                            }
                    }

                }

            }
        }

        try {
            FileOutputStream out = new FileOutputStream(new File(path));
            workbook.write(out);
            out.close();
            System.out.println("Excel written successfully..");

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

From source file:lucee.runtime.poi.Excel.java

License:Open Source License

public Excel(String sheetName, short xmlFormat, int rows) {
    if (FORMAT_XSSF == xmlFormat)
        workbook = new XSSFWorkbook();
    else if (FORMAT_HSSF == xmlFormat)
        workbook = new HSSFWorkbook();
    else if (FORMAT_SXSSF == xmlFormat)
        workbook = new SXSSFWorkbook();
    this.xmlFormat = xmlFormat;
    Sheet sheet = workbook.createSheet();
    drawing = sheet.createDrawingPatriarch();
    workbook.setSheetName(0, sheetName);
}

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

License:Apache License

@Benchmark
public Object poiStreaming() throws Exception {
    return poiPopulate(new SXSSFWorkbook());
}

From source file:org.eclipse.sw360.exporter.ExcelExporter.java

License:Open Source License

public InputStream makeExcelExport(List<T> documents) throws IOException, SW360Exception {
    final SXSSFWorkbook workbook = new SXSSFWorkbook();
    final ByteArrayInputStream stream;
    try {/*from   w ww.j av a2s. c o m*/
        SXSSFSheet sheet = workbook.createSheet("Data");

        /** Adding styles to cells */
        CellStyle cellStyle = createCellStyle(workbook);
        CellStyle headerStyle = createHeaderStyle(workbook);

        /** Create header row */
        Row headerRow = sheet.createRow(0);
        List<String> headerNames = helper.getHeaders();
        fillRow(headerRow, headerNames, headerStyle);

        /** Create data rows */
        fillValues(sheet, documents, cellStyle);

        // removed autosizing of spreadsheet columns for performance reasons

        /** Copy the streams */
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        workbook.write(out);
        stream = new ByteArrayInputStream(out.toByteArray());
    } finally {
        workbook.dispose();
    }
    return stream;
}

From source file:org.exist.xquery.corenlp.Tokenize.java

License:Open Source License

private void createXSLXSpreadsheet(List<List<CoreLabel>> sentences, List<CoreLabel> tokens) {
    Workbook workbook = null;//  www  . j av  a2 s.  c o m
    if (outputFormat == OutDocType.XSLX) {
        workbook = new SXSSFWorkbook();
    } else {
        workbook = new HSSFWorkbook();
    }
    CreationHelper creationHelper = workbook.getCreationHelper();
    org.apache.poi.ss.usermodel.Sheet sheet = workbook.createSheet();

    Font boldFont = workbook.createFont();
    boldFont.setBoldweight(Font.BOLDWEIGHT_BOLD);

    // Header
    CellStyle headerStyle = workbook.createCellStyle();
    headerStyle.setFont(boldFont);
    int lineIndex = 0;
    for (List<CoreLabel> sentence : sentences) {
        for (CoreLabel token : sentence) {
            String value = token.get(CoreAnnotations.OriginalTextAnnotation.class);
            Row row = sheet.createRow(lineIndex);
            row.createCell(0).setCellValue(creationHelper.createRichTextString(value));
            row.createCell(1).setCellValue(creationHelper.createRichTextString(backgroundSymbol));
            lineIndex++;
        }
        Row row = sheet.createRow(lineIndex);
        row.createCell(0).setCellValue(creationHelper.createRichTextString(""));
        row.createCell(1).setCellValue(creationHelper.createRichTextString(""));
        lineIndex++;
    }

    try (OutputStream os = Files.newOutputStream(tempOutFile)) {
        workbook.write(os);
    } catch (FileNotFoundException fe) {
        LOG.error(fe);
    } catch (IOException ioe) {
        LOG.error(ioe);
    } finally {
        if (workbook != null) {
            if (workbook instanceof SXSSFWorkbook) {
                ((SXSSFWorkbook) workbook).dispose();
            } else {
                workbook = null;
            }
        }
    }
}