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

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

Introduction

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

Prototype

public boolean dispose() 

Source Link

Document

Dispose of temporary files backing this workbook on disk.

Usage

From source file:org.ramadda.plugins.media.TabularOutputHandler.java

License:Open Source License

/**
 * _more_//from ww  w  .ja  va  2 s.c o m
 *
 * @param request _more_
 * @param service _more_
 * @param input _more_
 * @param args _more_
 *
 *
 * @return _more_
 * @throws Exception _more_
 */
public boolean extractSheet(Request request, Service service, ServiceInput input, List args) throws Exception {
    Entry entry = null;
    for (Entry e : input.getEntries()) {
        if (isTabular(e)) {
            entry = e;

            break;
        }
    }
    if (entry == null) {
        throw new IllegalArgumentException("No tabular entry found");
    }

    HashSet<Integer> sheetsToShow = getSheetsToShow((String) args.get(0));

    final SXSSFWorkbook wb = new SXSSFWorkbook(100);
    //        final Workbook   wb           = new XSSFWorkbook();

    String name = getStorageManager().getFileTail(entry);
    if (!Utils.stringDefined(name)) {
        name = entry.getName();
    }
    name = IOUtil.stripExtension(name);

    File newFile = new File(IOUtil.joinDir(input.getProcessDir(), name + ".xlsx"));

    TabularVisitor visitor = new TabularVisitor() {
        public boolean visit(Visitor info, String sheetName, List<List<Object>> rows) {
            sheetName = sheetName.replaceAll("[/]+", "-");
            Sheet sheet = wb.createSheet(sheetName);
            int rowCnt = 0;
            for (List<Object> cols : rows) {
                Row row = sheet.createRow(rowCnt++);
                for (int colIdx = 0; colIdx < cols.size(); colIdx++) {
                    Object col = cols.get(colIdx);
                    Cell cell = row.createCell(colIdx);
                    if (col instanceof Double) {
                        cell.setCellValue(((Double) col).doubleValue());
                    } else if (col instanceof Date) {
                        cell.setCellValue((Date) col);
                    } else if (col instanceof Boolean) {
                        cell.setCellValue(((Boolean) col).booleanValue());
                    } else {
                        cell.setCellValue(col.toString());
                    }
                }
            }

            return true;
        }
    };

    TabularVisitInfo visitInfo = new TabularVisitInfo(request, entry, getSkipRows(request, entry),
            getRowCount(request, entry, Integer.MAX_VALUE), sheetsToShow);

    Visitor info = new Visitor();
    info.setSkip(getSkipRows(request, entry));
    info.setMaxRows(getRowCount(request, entry, MAX_ROWS));
    //        http:://localhost:8080/repository/entry/show?entryid=740ae258-805d-4a1f-935d-289d0a6e5519&output=media_tabular_extractsheet&serviceform=true&execute=Execute

    visit(request, entry, info, visitor);

    FileOutputStream fileOut = new FileOutputStream(newFile);
    wb.write(fileOut);
    fileOut.close();
    wb.dispose();

    return true;

}

From source file:org.riflemansd.businessprofit.excel.ExcelExamplePOI.java

License:Open Source License

public static void main(String[] args) throws Throwable {
    SXSSFWorkbook wb = new SXSSFWorkbook(1000); // keep 100 rows in memory, exceeding rows will be flushed to disk

    if (wb.getNumberOfSheets() == 0) {
        wb.createSheet("MySheet");
    }/*from  w w  w. jav  a2s.c  o m*/
    Sheet sh = wb.getSheetAt(0);
    Row row = sh.createRow(3);

    for (int i = 0; i < 10; i++) {
        Cell cell = row.createCell(i);
        //String address = new CellReference(cell).formatAsString();
        cell.setCellValue("? " + i);
        //row.setHeightInPoints(50);
        //sh.setColumnWidth(5, 1200); //4, 33 pixels
        wb.getSheetAt(0).autoSizeColumn(i);
    }

    FileOutputStream out = new FileOutputStream("test.xlsx");
    wb.write(out);
    out.close();

    // dispose of temporary files backing this workbook on disk
    wb.dispose();

    Desktop.getDesktop().open(new File("test.xlsx"));
}

From source file:OutputStyles.DiffExcelDefault.java

/**
 * The output generating class. Creates an excel file at the path indicated
 * by the String, file//from w ww  . j  ava  2 s .c  om
 * @param file The String representation of the output path location
 * @param skip A string "true or false" value derived from my command line
 * option interpreter class.
 */
public void OutputDiffToExcel(String file, String skip) {
    // Set boolean skip flag if zero values should be avoided
    if (skip.equals("true"))
        exclude = true;

    Workbook wb = new SXSSFWorkbook(1000);

    // Get the information that we need from the diff file before proceeding
    TreeSet<String> sampleSet = new TreeSet<>(data.GetRpkmSamples());
    TreeSet<String> comparisonSet = new TreeSet<>(data.GetComp());
    TreeSet<String> locSet = new TreeSet<>(data.GetCoordLocs());

    // Create important styles
    CreateHeaderStyles(comparisonSet, wb);
    highlightStyles.put("yellow", HighlightStyle.YellowBoldHighlight(wb));
    Sheet sheet = GenerateSheetFromWb(wb);

    // Create spreadsheet header
    SetHeaderRow(sheet, sampleSet, comparisonSet);

    // I think that to minimize the memory overhead, I'm going to have to create 
    // a tab delimited text file and read that to generate the excel workbook
    String[] base = file.split("\\.");
    String outTab = base[0] + ".tab";
    try (BufferedWriter out = Files.newBufferedWriter(Paths.get(outTab), Charset.defaultCharset())) {
        CreateTabFileFromData(out, sampleSet, comparisonSet, locSet);
        // Dereferencing for garbage collection
        this.data = null;
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    try (BufferedReader in = Files.newBufferedReader(Paths.get(outTab), Charset.defaultCharset())) {
        String line = null;
        int row = 2;
        while ((line = in.readLine()) != null) {
            CreateRowFromTab(line, sampleSet, sheet, row);
            row++;
            if (row % 1000 == 0) {
                System.err.print("[DIFF EXCEL] Finished with row: " + row + "\r");
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(DiffExcelDefault.class.getName()).log(Level.SEVERE, null, ex);
    }

    System.err.print(System.lineSeparator());
    System.err.println("[DIFF EXCEL] Printing to output file!");
    /*int row = 2;
    for(String l : locSet){
    CreateRowFromData(sheet, l, sampleSet, comparisonSet, row);
    row++;
    }*/

    // Freeze the top two panes
    sheet.createFreezePane(0, 2);

    try (FileOutputStream out = new FileOutputStream(file)) {
        wb.write(out);
        out.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    SXSSFWorkbook b = (SXSSFWorkbook) wb;
    b.dispose();
}

From source file:poi.xssf.streaming.examples.Outlining.java

License:Apache License

private void collapseRow() throws Exception {
    SXSSFWorkbook wb2 = new SXSSFWorkbook(100);
    SXSSFSheet sheet2 = (SXSSFSheet) wb2.createSheet("new sheet");

    int rowCount = 20;
    for (int i = 0; i < rowCount; i++) {
        sheet2.createRow(i);//from w w  w .ja va  2 s  . co  m
    }

    sheet2.groupRow(4, 9);
    sheet2.groupRow(11, 19);

    sheet2.setRowGroupCollapsed(4, true);

    FileOutputStream fileOut = new FileOutputStream("outlining_collapsed.xlsx");
    wb2.write(fileOut);
    fileOut.close();
    wb2.dispose();
}