Example usage for org.apache.poi.ss.usermodel Sheet setDefaultColumnStyle

List of usage examples for org.apache.poi.ss.usermodel Sheet setDefaultColumnStyle

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Sheet setDefaultColumnStyle.

Prototype

void setDefaultColumnStyle(int column, CellStyle style);

Source Link

Document

Sets the default column style for a given column.

Usage

From source file:se.mithlond.services.content.impl.ejb.report.ExcelReportServiceBean.java

License:Apache License

/**
 * {@inheritDoc}/*  w  w  w  . j  a  va2  s .c o m*/
 */
@Override
public Sheet createStandardExcelSheet(@NotNull final Workbook workbook, @NotNull final String sheetName,
        @NotNull final String sheetTitle, @NotNull final List<String> columnTitles) {

    // Check sanity
    Validate.notEmpty(sheetName, "sheetName");
    Validate.notEmpty(sheetTitle, "sheetTitle");
    Validate.notEmpty(columnTitles, "columnTitles");
    Validate.notNull(workbook, "workbook");

    // Create a new Workbook if required.
    final LocalDateTime timestamp = LocalDateTime.now();
    final String now = TimeFormat.YEAR_MONTH_DATE_HOURS_MINUTES.print(timestamp).replace(":", " ");

    // Create the Sheet to return
    final Sheet toReturn = workbook.createSheet(sheetName + "_" + now);
    toReturn.setFitToPage(true);
    toReturn.setHorizontallyCenter(true);

    // Create a "Title" row containing a single cell (i.e. merged cells)
    // and where the sheet title is presented and centered.
    final Row titleRow = toReturn.createRow(0);
    titleRow.setHeightInPoints(45);
    final Cell titleCell = titleRow.createCell(0);
    titleCell.setCellValue(sheetTitle + " " + now);
    titleCell.setCellStyle(getCellStyle(ExcelElement.TITLE, workbook));
    // toReturn.addMergedRegion(CellRangeAddress.valueOf("$A$1:$E$1"));
    toReturn.addMergedRegion(CellRangeAddress
            .valueOf("$A$1:$" + CellReference.convertNumToColString(columnTitles.size() - 1) + "1"));

    // Create a header Row with the column names defined above.
    final Row headerRow = toReturn.createRow(1);

    // headerRow.setHeightInPoints(40);
    // This *could* adjust the header row to fit its internal height.
    titleRow.setHeight((short) -1);

    Cell headerCell;

    for (int i = 0; i < columnTitles.size(); i++) {
        headerCell = headerRow.createCell(i);
        headerCell.setCellValue(columnTitles.get(i));
        headerCell.setCellStyle(getCellStyle(ExcelElement.HEADER, workbook));
        toReturn.setDefaultColumnStyle(i, getCellStyle(ExcelElement.NON_WRAPPING, workbook));
        toReturn.autoSizeColumn(i);
    }

    // All done
    return toReturn;
}