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

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

Introduction

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

Prototype

@Override
public void setColumnWidth(int columnIndex, int width) 

Source Link

Document

Set the width (in units of 1/256th of a character width)

The maximum column width for an individual cell is 255 characters.

Usage

From source file:com.plugin.excel.util.ExcelUtil.java

License:Apache License

/**
 * @param newSheet//from   ww  w . j a  v a 2 s  . com
 *            the sheet to create from the copy.
 * @param sheet
 *            the sheet to copy.
 * @param copyStyle
 *            true copy the style.
 */
public static void copySheets(SXSSFSheet newSheet, SXSSFSheet sheet, boolean copyStyle) {
    int maxColumnNum = 0;
    Map<Integer, CellStyle> styleMap = (copyStyle) ? new HashMap<Integer, CellStyle>() : null;
    for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
        Row srcRow = sheet.getRow(i);
        Row destRow = newSheet.createRow(i);
        if (srcRow != null) {
            ExcelUtil.copyRow(sheet, newSheet, srcRow, destRow, styleMap);
            if (srcRow.getLastCellNum() > maxColumnNum) {
                maxColumnNum = srcRow.getLastCellNum();
            }
        }
    }
    for (int i = 0; i <= maxColumnNum; i++) {
        newSheet.setColumnWidth(i, sheet.getColumnWidth(i));
    }
}

From source file:eu.alpinweiss.filegen.util.processor.SheetProcessor.java

License:Apache License

public void generateSheetData(ParameterVault parameterVault, CellStyle cs, SXSSFSheet sheet, int columnCount,
        Map<Integer, Input2TableInfo> hashMap) {
    Cell cell;// Row and column indexes
    int idx = 0;/*www . j  a  v a  2  s. co m*/
    int idy = 0;

    // Generate column headings
    Row row = sheet.createRow(idx);
    Input2TableInfo input2TableInfo;

    for (Integer key : hashMap.keySet()) {
        input2TableInfo = hashMap.get(key);
        cell = row.createCell(idy);
        cell.setCellValue(input2TableInfo.getFieldText());
        cell.setCellStyle(cs);
        sheet.setColumnWidth(idy, (input2TableInfo.getFieldText().trim().length() * 500));
        idy++;
    }

    ThreadLocalRandom randomGenerator = ThreadLocalRandom.current();
    for (int i = 1; i < parameterVault.rowCount() + 1; i++) {

        row = sheet.createRow(i);
        if (i != 0 && i % 10000 == 0) {
            outputWriterHolder.writeValueInLine(Thread.currentThread().getName() + " Processed " + i + " rows");
        }
        for (int colCount = 0; colCount < columnCount; colCount++) {

            final Cell dataCell = row.createCell(colCount);
            input2TableInfo = hashMap.get(colCount);

            final CellStyle cellStyle = input2TableInfo.getCellStyle();

            input2TableInfo.generator().generate(parameterVault.setIterationNumber(i), randomGenerator,
                    new ValueVault() {
                        @Override
                        public void storeValue(DataWrapper wrapper) {
                            FieldType fieldType = wrapper.getFieldType();
                            switch (fieldType) {
                            case DATE:
                                dataCell.setCellValue(wrapper.getDateValue());
                                dataCell.setCellStyle(cellStyle);
                                break;
                            case FLOAT:
                            case INTEGER:
                                dataCell.setCellType(Cell.CELL_TYPE_NUMERIC);
                                dataCell.setCellValue(wrapper.getNumberValue());
                                break;
                            case AUTONUMBER:
                                dataCell.setCellType(Cell.CELL_TYPE_NUMERIC);
                                dataCell.setCellValue(wrapper.getNumberValue());
                            case SEQUENCE:
                            default:
                                dataCell.setCellValue(wrapper.getStringValue());
                            }

                        }
                    });
        }

    }
}

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);/*from w  w w .  j a v  a2s .co  m*/

    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"));
}