Example usage for org.apache.poi.xssf.usermodel.helpers ColumnHelper setColBestFit

List of usage examples for org.apache.poi.xssf.usermodel.helpers ColumnHelper setColBestFit

Introduction

In this page you can find the example usage for org.apache.poi.xssf.usermodel.helpers ColumnHelper setColBestFit.

Prototype

public void setColBestFit(long index, boolean bestFit) 

Source Link

Usage

From source file:workbench.db.exporter.XlsRowDataConverter.java

License:Apache License

@Override
public StringBuilder getEnd(long totalRows) {
    if (getAppendInfoSheet()) {
        writeInfoSheet();//from   w w w .j av  a 2  s.co  m
    }

    if (getEnableFixedHeader() && writeHeader) {
        sheet.createFreezePane(0, firstRow);
    }

    if (getEnableAutoFilter() && writeHeader) {
        String lastColumn = CellReference.convertNumToColString(metaData.getColumnCount() - 1 + columnOffset);
        String firstColumn = CellReference.convertNumToColString(columnOffset);

        String rangeName = firstColumn + Integer.toString(rowOffset + 1) + ":" + lastColumn
                + Long.toString(totalRows + 1 + rowOffset);
        CellRangeAddress range = CellRangeAddress.valueOf(rangeName);
        sheet.setAutoFilter(range);
    }

    if (optimizeCols) {
        for (int col = 0; col < this.metaData.getColumnCount(); col++) {
            sheet.autoSizeColumn(col + columnOffset);
        }

        // POI seems to use a strange unit for specifying column widths.
        int charWidth = Settings.getInstance().getIntProperty("workbench.export.xls.defaultcharwidth", 200);

        for (int col = 0; col < this.metaData.getColumnCount(); col++) {
            int width = sheet.getColumnWidth(col + columnOffset);
            int minWidth = metaData.getColumnName(col).length() * charWidth;
            if (getEnableAutoFilter()) {
                minWidth += charWidth * 2;
            }
            if (width < minWidth) {
                LogMgr.logDebug("XlsRowDataConverter.getEnd()", "Calculated width of column " + col + " is: "
                        + width + ". Applying min width: " + minWidth);
                sheet.setColumnWidth(col + columnOffset, minWidth);
                if (sheet instanceof XSSFSheet) {
                    ColumnHelper helper = ((XSSFSheet) sheet).getColumnHelper();
                    helper.setColBestFit(col + columnOffset, false);
                    helper.setColHidden(col + columnOffset, false);
                }
            }
        }
    }

    FileOutputStream fileOut = null;
    try {
        fileOut = new FileOutputStream(getOutputFile());
        workbook.write(fileOut);
        outputSheetName = sheet.getSheetName();
        workbook = null;
        sheet = null;
        styles.clear();
        headerStyles.clear();
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        FileUtil.closeQuietely(fileOut);
    }

    return null;
}