Example usage for org.apache.poi.xssf.streaming SXSSFSheet autoSizeColumn

List of usage examples for org.apache.poi.xssf.streaming SXSSFSheet autoSizeColumn

Introduction

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

Prototype

@Override
public void autoSizeColumn(int column) 

Source Link

Document

Adjusts the column width to fit the contents.

Usage

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

License:Apache License

private void dumpReportToWorkbook(Statement st, SXSSFWorkbook wb) throws IOException, SQLException {
    ResultSet rs = st.executeQuery(sql);

    SXSSFSheet sheet = wb.createSheet("tika-eval Report");
    sheet.trackColumnForAutoSizing(0);//  ww  w  .j  ava 2s  .  com

    int rowCount = 0;
    ResultSetMetaData meta = rs.getMetaData();
    Set<String> colNames = new HashSet<>();

    Row xssfRow = sheet.createRow(rowCount++);
    //write headers and cache them to check against styles
    for (int i = 1; i <= meta.getColumnCount(); i++) {
        Cell cell = xssfRow.createCell(i - 1);
        cell.setCellValue(meta.getColumnLabel(i));
        colNames.add(meta.getColumnLabel(i));
    }

    ResultSetMetaData resultSetMetaData = rs.getMetaData();
    while (rs.next()) {
        xssfRow = sheet.createRow(rowCount++);
        for (int i = 1; i <= meta.getColumnCount(); i++) {
            Cell cell = xssfRow.createCell(i - 1);
            XSLXCellFormatter formatter = cellFormatters.get(meta.getColumnLabel(i));
            if (formatter == null) {
                formatter = getDefaultFormatter(resultSetMetaData.getColumnType(i));
            }
            if (formatter != null) {
                formatter.applyStyleAndValue(i, rs, cell);
            } else {
                writeCell(meta, i, rs, cell);
            }
        }
    }
    sheet.autoSizeColumn(0);

    if (!includeSql) {
        return;
    }

    SXSSFSheet sqlSheet = wb.createSheet("tika-eval SQL");
    sqlSheet.setColumnWidth(0, 100 * 250);
    Row sqlRow = sqlSheet.createRow(0);
    short height = 5000;
    sqlRow.setHeight(height);
    Cell cell = sqlRow.createCell(0);
    cell.setCellStyle(sqlCellStyle);

    cell.setCellValue(sql.trim());//.replaceAll("[\r\n]+", "\r\n"));
}

From source file:org.dashbuilder.dataset.service.DataSetExportServicesImpl.java

License:Apache License

SXSSFWorkbook dataSetToWorkbook(DataSet dataSet) {
    // TODO?: Excel 2010 limits: 1,048,576 rows by 16,384 columns; row width 255 characters
    if (dataSet == null) {
        throw new IllegalArgumentException("Null dataSet specified!");
    }//  ww  w .j  a v a  2  s .  com
    int columnCount = dataSet.getColumns().size();
    int rowCount = dataSet.getRowCount() + 1; //Include header row;
    int row = 0;

    SXSSFWorkbook wb = new SXSSFWorkbook(100); // keep 100 rows in memory, exceeding rows will be flushed to disk
    Map<String, CellStyle> styles = createStyles(wb);
    SXSSFSheet sh = wb.createSheet("Sheet 1");

    // General setup
    sh.setDisplayGridlines(true);
    sh.setPrintGridlines(false);
    sh.setFitToPage(true);
    sh.setHorizontallyCenter(true);
    sh.trackAllColumnsForAutoSizing();
    PrintSetup printSetup = sh.getPrintSetup();
    printSetup.setLandscape(true);

    // Create header
    Row header = sh.createRow(row++);
    header.setHeightInPoints(20f);
    for (int i = 0; i < columnCount; i++) {
        Cell cell = header.createCell(i);
        cell.setCellStyle(styles.get("header"));
        cell.setCellValue(dataSet.getColumnByIndex(i).getId());
    }

    // Create data rows
    for (; row < rowCount; row++) {
        Row _row = sh.createRow(row);
        for (int cellnum = 0; cellnum < columnCount; cellnum++) {
            Cell cell = _row.createCell(cellnum);
            Object value = dataSet.getValueAt(row - 1, cellnum);
            if (value instanceof Short || value instanceof Long || value instanceof Integer
                    || value instanceof BigInteger) {
                cell.setCellType(CellType.NUMERIC);
                cell.setCellStyle(styles.get("integer_number_cell"));
                cell.setCellValue(((Number) value).doubleValue());
            } else if (value instanceof Float || value instanceof Double || value instanceof BigDecimal) {
                cell.setCellType(CellType.NUMERIC);
                cell.setCellStyle(styles.get("decimal_number_cell"));
                cell.setCellValue(((Number) value).doubleValue());
            } else if (value instanceof Date) {
                cell.setCellType(CellType.STRING);
                cell.setCellStyle(styles.get("date_cell"));
                cell.setCellValue((Date) value);
            } else if (value instanceof Interval) {
                cell.setCellType(CellType.STRING);
                cell.setCellStyle(styles.get(TEXT_CELL));
                cell.setCellValue(((Interval) value).getName());
            } else {
                cell.setCellType(CellType.STRING);
                cell.setCellStyle(styles.get(TEXT_CELL));
                String val = value == null ? "" : value.toString();
                cell.setCellValue(val);
            }
        }
    }

    // Adjust column size
    for (int i = 0; i < columnCount; i++) {
        sh.autoSizeColumn(i);
    }
    return wb;
}

From source file:org.jkiss.dbeaver.data.office.export.DataExporterXLSX.java

License:Apache License

private void printHeader(Worksheet wsh) {
    boolean hasDescription = false;
    if (showDescription) {
        for (DBDAttributeBinding column : columns) {
            if (!CommonUtils.isEmpty(column.getDescription())) {
                hasDescription = true;/*  w  w  w. j  a  va  2s.co  m*/
                break;
            }
        }
    }

    SXSSFSheet sh = (SXSSFSheet) wsh.getSh();
    Row row = sh.createRow(wsh.getCurrentRow());

    int startCol = rowNumber ? 1 : 0;

    for (int i = 0, columnsSize = columns.size(); i < columnsSize; i++) {
        sh.trackColumnForAutoSizing(i);
        DBDAttributeBinding column = columns.get(i);

        String colName = column.getLabel();
        if (CommonUtils.isEmpty(colName)) {
            colName = column.getName();
        }
        Cell cell = row.createCell(i + startCol, CellType.STRING);
        cell.setCellValue(colName);
        cell.setCellStyle(styleHeader);
    }

    if (hasDescription) {
        wsh.incRow();
        Row descRow = sh.createRow(wsh.getCurrentRow());
        for (int i = 0, columnsSize = columns.size(); i < columnsSize; i++) {
            Cell descCell = descRow.createCell(i + startCol, CellType.STRING);
            String description = columns.get(i).getDescription();
            if (CommonUtils.isEmpty(description)) {
                description = "";
            }
            descCell.setCellValue(description);
            descCell.setCellStyle(styleHeader);
        }
    }

    for (int i = 0, columnsSize = columns.size(); i < columnsSize; i++) {
        sh.autoSizeColumn(i);
    }

    wsh.incRow();
}

From source file:tools.xor.service.AggregateManager.java

License:Apache License

private void writeSheetMap(SXSSFWorkbook wb, Map<String, String> sheetMap) {
    SXSSFSheet sh = (SXSSFSheet) wb.createSheet(Constants.XOR.EXCEL_INDEX_SHEET);

    int rowNo = 0;
    for (Map.Entry<String, String> entry : sheetMap.entrySet()) {
        Row row = sh.createRow(rowNo++);
        Cell sheetNameCell = row.createCell(0);
        Cell propertyNameCell = row.createCell(1);
        sheetNameCell.setCellValue(entry.getValue());
        propertyNameCell.setCellValue(entry.getKey());
    }/*ww w.j  av  a 2 s  .c om*/

    sh.autoSizeColumn(0);
    sh.autoSizeColumn(1);
    wb.setSheetOrder(Constants.XOR.EXCEL_INDEX_SHEET, 1);
}

From source file:tools.xor.service.AggregateManager.java

License:Apache License

private void writeColumnNames(SXSSFSheet sh, Map<String, Integer> propertyColIndex) {
    Row row = sh.getRow(0);//  w w  w . j  a  v a  2s  .  c  o  m
    if (row != null) {
        // Column names have already been populated
        return;
    }

    row = sh.createRow(0);
    for (Map.Entry<String, Integer> entry : propertyColIndex.entrySet()) {
        Cell cell = row.createCell(entry.getValue());
        cell.setCellValue(entry.getKey());
        sh.autoSizeColumn(entry.getValue());
    }
}