Example usage for org.apache.poi.xssf.usermodel XSSFCellStyle setFont

List of usage examples for org.apache.poi.xssf.usermodel XSSFCellStyle setFont

Introduction

In this page you can find the example usage for org.apache.poi.xssf.usermodel XSSFCellStyle setFont.

Prototype

@Override
public void setFont(Font font) 

Source Link

Document

Set the font for this style

Usage

From source file:uk.gov.ofwat.fountain.api.report.PoiReportBuilder.java

License:Open Source License

private void makeHeaderCell(String content, short colour) {
    Cell cell = row.createCell(cellIdx++);
    cell.setCellType(Cell.CELL_TYPE_STRING);
    RichTextString rts = creationHelper.createRichTextString(content);
    cell.setCellValue(rts);//from   w  w w. j  a  v  a2  s  . c  o m
    XSSFCellStyle style = workBook.createCellStyle();
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setFillForegroundColor(colour);
    style.setFont(font);
    cell.setCellStyle(style);
}

From source file:uk.gov.ofwat.fountain.api.report.POIReportWriter.java

License:Open Source License

private void renderReport(ReportStructure reportStructure, ReportDto report) {

    Sheet sheet = workBook.createSheet();
    Map<Integer, Set<GroupEntry>> groupEntries = report.getGroupEntriesByCompany();
    inputDataNumericStyleMap = new HashMap<Short, XSSFCellStyle>();
    calcDataNumericStyleMap = new HashMap<Short, XSSFCellStyle>();

    yellow = new XSSFColor(new java.awt.Color(255, 255, 0));
    lightYellow = new XSSFColor(new java.awt.Color(255, 255, 224));
    lightBlue = new XSSFColor(new java.awt.Color(224, 255, 255));

    // Styles//  www.jav a 2 s  .c  om
    // Row header style
    rowHeaderStyle = workBook.createCellStyle();
    // Col header style
    colHeaderStyle = workBook.createCellStyle();
    colHeaderStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
    colHeaderStyle.setFillForegroundColor(yellow);
    Font colHeaderFont = workBook.createFont();
    colHeaderFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    colHeaderStyle.setFont(colHeaderFont);
    // Input numeric Data cell style
    inputDataNumericStyle = workBook.createCellStyle();
    inputDataNumericStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
    inputDataNumericStyle.setFillForegroundColor(lightYellow);
    // Calc numeric Data cell style
    calcDataNumericStyle = workBook.createCellStyle();
    calcDataNumericStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
    calcDataNumericStyle.setFillForegroundColor(lightBlue);
    // Input text data cell style
    inputDataTextStyle = workBook.createCellStyle();
    inputDataTextStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
    inputDataTextStyle.setFillForegroundColor(lightYellow);
    // Calc text data cell style
    calcDataTextStyle = workBook.createCellStyle();
    calcDataTextStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
    calcDataTextStyle.setFillForegroundColor(lightBlue);
    // Input CG style
    inputCGStyle = workBook.createCellStyle();
    inputCGStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
    inputCGStyle.setFillForegroundColor(lightYellow);
    // Calc CG style
    calcCGStyle = workBook.createCellStyle();
    calcCGStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
    calcCGStyle.setFillForegroundColor(lightBlue);

    // data format
    DataFormat format = workBook.createDataFormat();

    int rownum = 1; // starting point
    Row infoRow = sheet.createRow(rownum);
    Cell titleCell = infoRow.createCell(2);
    titleCell.setCellType(Cell.CELL_TYPE_STRING);
    String title = reportStructure.getReportName();
    RichTextString rts = creationHelper.createRichTextString(title);
    titleCell.setCellValue(rts);
    XSSFCellStyle style = workBook.createCellStyle();
    Font font = workBook.createFont();
    font.setBoldweight(Font.BOLDWEIGHT_BOLD);
    font.setUnderline(Font.U_SINGLE);
    style.setFont(font);
    titleCell.setCellStyle(style);

    Cell dateCell = infoRow.createCell(4);
    dateCell.setCellType(Cell.CELL_TYPE_STRING);
    String DATE_FORMAT = "dd MMM yyyy H:mm";
    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
    Calendar c1 = Calendar.getInstance(); // today
    String today = sdf.format(c1.getTime());
    RichTextString dateRts = creationHelper.createRichTextString("Run on " + today);
    dateCell.setCellValue(dateRts);
    dateCell.setCellStyle(style);

    rownum++;
    rownum++;

    ReportDefinition reportDefinition = reportService.getReportDefinition(report.getId());

    if (reportStructure.isCompanyAcrossTop()) {
        for (ReportLine line : reportStructure.getLines()) {
            // get the group entries for this line
            // ungrouped line
            Row row = sheet.createRow(rownum);
            rownum++;
            List<ReportCell> nonRepeatHeaderCells = new ArrayList<ReportCell>();
            writeLine(line, report, workBook, row, format, null, nonRepeatHeaderCells, groupEntries,
                    reportDefinition);
        }
    } else {
        for (ReportLine line : reportStructure.getLines()) {
            // get the group entries for this line
            Set<GroupEntry> groupEntrySet = null;
            for (ReportCell cell : line.getCells()) {
                if (cell.getCellType() == CellType.CALC || cell.getCellType() == CellType.INPUT) {
                    String id = cell.getCellContents();
                    DataKey key = new DataKey(id);
                    // get the company
                    if (report.getCompany() != null) {
                        // interchangable company. A single company supplied with data rather than with the report template.
                        groupEntrySet = groupEntries.get(report.getCompany().getId());
                    } else {
                        // get the company for this line
                        groupEntrySet = groupEntries.get(key.getCompanyIdInteger());
                    }
                    break;
                }
            }
            if (null != groupEntrySet) {
                // group line
                List<ReportCell> nonRepeatHeaderCells = new ArrayList<ReportCell>();
                for (GroupEntry ge : groupEntrySet) {
                    Row row = sheet.createRow(rownum);
                    rownum++;
                    writeLine(line, report, workBook, row, format, ge, nonRepeatHeaderCells, null,
                            reportDefinition);
                }
            } else {
                // ungrouped line
                Row row = sheet.createRow(rownum);
                rownum++;
                writeLine(line, report, workBook, row, format, null, null, null, reportDefinition);
            }
        }
    }

}

From source file:uk.gov.ofwat.RefTest.java

License:Open Source License

public void writeXLS() throws IOException {
    XSSFWorkbook wb = new XSSFWorkbook();
    CreationHelper creationHelper = wb.getCreationHelper();
    // create a new sheet
    Sheet s = wb.createSheet();//from  ww  w  .j a  v a 2 s.  c o m
    // declare a row object reference
    Row r = null;
    // declare a cell object reference
    Cell c = null;
    // create 2 cell styles
    XSSFCellStyle cs = wb.createCellStyle();

    XSSFCellStyle cs2 = wb.createCellStyle();
    DataFormat df = wb.createDataFormat();

    // create 2 fonts objects
    Font f = wb.createFont();
    Font f2 = wb.createFont();

    // Set font 1 to 12 point type, blue and bold
    f.setFontHeightInPoints((short) 12);
    f.setColor(IndexedColors.RED.getIndex());
    f.setBoldweight(Font.BOLDWEIGHT_BOLD);

    // Set font 2 to 10 point type, red and bold
    f2.setFontHeightInPoints((short) 10);
    f2.setColor(IndexedColors.RED.getIndex());
    f2.setBoldweight(Font.BOLDWEIGHT_BOLD);

    // Set cell style and formatting
    cs.setFont(f);
    cs.setDataFormat(df.getFormat("#,##0.0"));

    // Set the other cell style and formatting
    cs2.setBorderBottom(cs2.BORDER_THIN);
    cs2.setDataFormat((short) BuiltinFormats.getBuiltinFormat("text"));
    cs2.setFont(f2);

    // Define a few rows
    for (int rownum = 0; rownum < 30; rownum++) {
        r = s.createRow(rownum);
        for (int cellnum = 0; cellnum < 10; cellnum += 2) {
            c = r.createCell(cellnum);
            Cell c2 = r.createCell(cellnum + 1);

            c.setCellValue((double) rownum + (cellnum / 10));
            c2.setCellValue(creationHelper.createRichTextString("Hello! " + cellnum));
        }
    }

    File file = new File("d:\\out.xls");
    FileOutputStream fos = new FileOutputStream(file);
    wb.write(fos);
    //      fos.write(wb.getBytes());
    //      fos.flush();
    //      fos.close();

}

From source file:ve.zoonosis.utils.XLSCreator.java

License:Apache License

public void addRow(CeldaCustomizer... objs) {
    //        int r, w;
    //        final int n = r = w = objs.length;
    //        while (r > 0) {
    //            final CeldaCustomizer s = objs[--r];
    //            if (s != null) {
    //                objs[--w] = s;
    //            }
    //        }//  w  w  w  .  j  a  v a  2s.com
    //        objs = Arrays.copyOfRange(objs, w, n);
    for (int i = 0; i < objs.length; i++) {
        if (objs[i] == null) {
            objs[i] = new CeldaCustomizer("");
        }
        int inic = i;
        int fin = i;
        if (i > 1) {
            if (objs[i - 1].getColumn() != 1) {
                inic = i * objs[i - 1].getColumn() - 1;
            }
        }
        if (objs[i].getColumn() != 1) {
            CeldaCustomizer cc = objs[i];
            fin = inic + cc.getColumn() - 1;
        }
        if (inic < fin) {
            CellRangeAddress cellRangeAddress = new CellRangeAddress(rowIndex, rowIndex, (inic), (fin));
            sheet.addMergedRegion(cellRangeAddress);
        }
    }
    Row row = sheet.createRow(rowIndex++);
    if (rowIndex == 1) {
        sheet.setColumnWidth(0, 10000);
    }
    for (int i = 0; i < objs.length; i++) {

        int inic = i;

        if (i > 1) {
            if (objs[i - 1].getColumn() != 1) {
                inic = i * objs[i - 1].getColumn() - 1;
            }
        }
        XSSFFont font = wb.createFont();
        XSSFCellStyle style = wb.createCellStyle();
        font.setBold(objs[i].isBold());
        style.setFont(font);
        Object obj = objs[i].getObj();
        Cell cell = row.createCell(inic);
        cell.setCellStyle(style);
        if (obj instanceof String) {
            cell.setCellValue((String) obj);
        } else if (obj instanceof Boolean) {
            cell.setCellValue((Boolean) obj);
        } else if (obj instanceof Date) {
            cell.setCellValue((Date) obj);
        } else if (obj instanceof Double) {
            cell.setCellValue((Double) obj);
        } else if (obj instanceof Integer) {
            cell.setCellValue(Double.valueOf("" + (int) obj));
        }

    }
}