Example usage for org.apache.poi.xssf.usermodel XSSFWorkbook createFont

List of usage examples for org.apache.poi.xssf.usermodel XSSFWorkbook createFont

Introduction

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

Prototype

@Override
public XSSFFont createFont() 

Source Link

Document

Create a new Font and add it to the workbook's font table

Usage

From source file:se.minstrel.tools.xssfbuilder.style.impl.StyleBuilderImpl.java

License:Open Source License

private XSSFCellStyle buildStyle() {
    XSSFWorkbook wb = support.getWorkbook();

    XSSFFont font = wb.createFont();
    font.setFontName(style.getFont());/*w ww . ja v a2  s  .  com*/
    font.setFontHeightInPoints(style.getFontSize());
    font.setBold(style.isBold());
    font.setItalic(style.isItalics());
    if (style.getFgColor() != null) {
        font.setColor(new XSSFColor(style.getFgColor()));
    }

    XSSFCellStyle cs = support.getWorkbook().createCellStyle();
    cs.setFont(font);

    if (style.getBgColor() != null) {
        cs.setFillForegroundColor(new XSSFColor(style.getBgColor()));
        cs.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    }

    if (style.getFormat() != null) {
        cs.setDataFormat(support.getDataFormat().getFormat(style.getFormat()));
    }
    // style.getBgColor();
    // style.getFgColor();
    // style.isBold();
    // style.isItalics();

    return cs;
}

From source file:step.datapool.excel.StyleSyntax.java

License:Open Source License

/**
 * Analysiert den angegebenen Stil und setzt diesen excel-konform.
 * /*from ww  w  .  j  av  a  2  s.com*/
 * @param strStyle strStyle
 * @param _wb _wb
 * @return XSSFCellStyle
 */
public static XSSFCellStyle composeStyle(String strStyle, Workbook _wb) {
    if (_wb instanceof XSSFWorkbook) {
        XSSFWorkbook wb = (XSSFWorkbook) _wb;
        if (strStyle == null || strStyle.isEmpty())
            return null;
        XSSFCellStyle style = wb.createCellStyle();
        XSSFFont font = wb.createFont();
        strStyle = replaceColors(strStyle);

        /* Unterteilung der verschiedenen Stilangaben: Schriftschnitt, Vordergrund/Hintergrundfarbe, Schriftgroesse, Schriftsatz */
        String[] arr = strStyle.split(",");
        for (String str : arr) {
            /* Abhandlung des Schriftschnitts. Diese koennen alle zusammen auftreten und schliessen sich nicht aus */
            if (str.matches("( *bold *| *italic *| *underline *| *strikeout *)*")) {
                if (str.contains("bold")) {
                    font.setBoldweight(Font.BOLDWEIGHT_BOLD);
                }
                if (str.contains("italic")) {
                    font.setItalic(true);
                }
                if (str.contains("underline")) {
                    font.setUnderline(Font.U_SINGLE);
                }
                if (str.contains("strikeout")) {
                    font.setStrikeout(true);
                }
                continue;
            }
            /* Abhandlung der Farben */
            if (str.contains("/") && !str.contains(":")) {
                continue;
            }
            if (str.contains(":")) {
                String[] fgBg;
                /* Vordergrund / Hintergrund */
                if (str.contains("/")) {
                    fgBg = str.split("/");
                } else {
                    /* Verkuerzte Angabe ohne / (nur Vordergrund) */
                    fgBg = new String[1];
                    fgBg[0] = str;
                }

                for (int i = 0; i < fgBg.length; i++) {
                    fgBg[i] = fgBg[i].trim();
                    if (!fgBg[i].isEmpty()) {
                        String[] clr = fgBg[i].split(":");
                        if (clr.length != 3) {
                            return null;
                        }
                        int red = Integer.parseInt(clr[0].trim());
                        int green = Integer.parseInt(clr[1].trim());
                        int blue = Integer.parseInt(clr[2].trim());

                        if (i == 0) {
                            /* Schriftfarbe
                             *  -------------------------------------------------------------
                             * | ACHTUNG: poi hat einen Fehler beim Setzen der Schriftfarbe! |
                             *  -------------------------------------------------------------
                             *  Weiss und Schwarz sind verwechselt worden! Gilt NUR fuer
                             *  font.setColor!
                             *  Deshalb wird hier einfach Weiss auf Schwarz korrigiert und
                             *  umgekehrt.
                             */
                            if (red == 0 && green == 0 && blue == 0) {
                                red = 255;
                                green = 255;
                                blue = 255;
                            } else if (red == 255 && green == 255 && blue == 255) {
                                red = 0;
                                green = 0;
                                blue = 0;
                            }
                            XSSFColor xssfColor = new XSSFColor(new Color(red, green, blue));
                            font.setColor(xssfColor);
                        } else {
                            // Vordergrund/Hintergrundfarbe der Zelle
                            XSSFColor xssfColor = new XSSFColor(new Color(red, green, blue));
                            style.setFillForegroundColor(xssfColor);
                            style.setFillBackgroundColor(xssfColor);
                            style.setFillPattern(CellStyle.SOLID_FOREGROUND);
                        }

                    }
                }
                continue;
            }
            /* Abhandlung der Schriftgroesse */
            if (str.matches(" *[1-9][0-9]* *")) {
                short fontHeightInPoints = Short.parseShort(str.trim());
                font.setFontHeightInPoints(fontHeightInPoints);
                continue;
            }
            /* Abhandlung der Schriftart */
            if (!str.isEmpty()) {
                font.setFontName(str);
                continue;
            }
        }
        style.setFont(font);
        return style;
    } else {
        return null;
    }
}

From source file:uk.gov.ofwat.fountain.api.table.POITableRenderer.java

License:Open Source License

public Workbook renderTable(XSSFWorkbook workBook, TableStructure tableStructure, DataTable table) {
    this.workBook = workBook;
    creationHelper = workBook.getCreationHelper();

    sheet = workBook.createSheet(table.getCompany().getCode() + " Table " + tableStructure.getTableName());
    PrintSetup printSetup = sheet.getPrintSetup();
    printSetup.setPaperSize(PrintSetup.A4_PAPERSIZE);

    inputDataNumericStyleMap = new HashMap<Short, XSSFCellStyle>();
    copyCellDataNumericStyleMap = 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));
    pink = new XSSFColor(new java.awt.Color(255, 204, 204));

    // Styles/*from  w  ww . j  a  v  a  2  s.  co m*/
    // Row header style
    rowHeaderStyle = workBook.createCellStyle();
    // Col header style
    colHeaderStyle = workBook.createCellStyle();
    colHeaderStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    colHeaderStyle.setFillForegroundColor(yellow);
    Font colHeaderFont = workBook.createFont();
    colHeaderFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    colHeaderStyle.setFont(colHeaderFont);
    // Copycell text data cell style
    copyCellTextStyle = workBook.createCellStyle();
    copyCellTextStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    copyCellTextStyle.setFillForegroundColor(pink);
    // Input text data cell style
    inputDataTextStyle = workBook.createCellStyle();
    inputDataTextStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    inputDataTextStyle.setFillForegroundColor(lightYellow);
    // Calc text data cell style
    calcDataTextStyle = workBook.createCellStyle();
    calcDataTextStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    calcDataTextStyle.setFillForegroundColor(lightBlue);
    // Input CG style
    inputCGStyle = workBook.createCellStyle();
    inputCGStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    inputCGStyle.setFillForegroundColor(lightYellow);
    // Input CG style
    copyCellCGStyle = workBook.createCellStyle();
    copyCellCGStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    copyCellCGStyle.setFillForegroundColor(pink);
    // Calc CG style
    calcCGStyle = workBook.createCellStyle();
    calcCGStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    calcCGStyle.setFillForegroundColor(lightBlue);

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

    int rownum = 1; // starting point
    Row infoRow1 = sheet.createRow(rownum);
    CellStyle style = workBook.createCellStyle();
    Font font = workBook.createFont();
    font.setBoldweight(Font.BOLDWEIGHT_BOLD);
    style.setFont(font);

    Cell titleCell1 = infoRow1.createCell(0);
    titleCell1.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(today + ": "
            + tableStructure.getModelPage().getModel().getCode() + " for " + table.getCompany().getName());
    titleCell1.setCellValue(dateRts);
    titleCell1.setCellStyle(style);
    sheet.addMergedRegion(new CellRangeAddress(rownum, rownum, 0, 9));

    rownum++;
    Row infoRow2 = sheet.createRow(rownum);
    Cell titleCell = infoRow2.createCell(0);
    titleCell.setCellType(Cell.CELL_TYPE_STRING);
    RichTextString rts = creationHelper.createRichTextString(tableStructure.getModelPage().getTable().getName()
            + " - " + tableStructure.getModelPage().getTableDescription());
    titleCell.setCellValue(rts);
    titleCell.setCellStyle(style);
    sheet.addMergedRegion(new CellRangeAddress(rownum, rownum, 0, 4));

    rownum++;
    rownum++;

    if (tableStructure.getModelPage().isGroupSelect()) {
        // group dropdown
        groupSelectTable(tableStructure, table, workBook, sheet, format, rownum);
    } else {
        tableWithoutGroupSelect(tableStructure, table, workBook, sheet, format, rownum);
    }
    return workBook;
}

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();/*w w w. ja va2 s.c om*/
    // 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:util.ExcelConverter.java

public static File createXlsx(String[] header, String[][] data, String path) {

    try {//w  w  w. j  a  va 2s .  c om
        XSSFWorkbook xwb = new XSSFWorkbook();
        XSSFSheet sheet = xwb.createSheet();

        CellStyle cellStyle = xwb.createCellStyle();
        cellStyle.setAlignment(CellStyle.ALIGN_LEFT);
        cellStyle.setAlignment(CellStyle.VERTICAL_TOP);
        cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
        cellStyle.setWrapText(false);

        Font bold = xwb.createFont();
        bold.setBoldweight(Font.BOLDWEIGHT_BOLD);
        bold.setFontHeightInPoints((short) 10);

        CellStyle cellStyleHeader = xwb.createCellStyle();
        cellStyleHeader.setAlignment(CellStyle.ALIGN_LEFT);
        cellStyleHeader.setAlignment(CellStyle.VERTICAL_TOP);
        cellStyleHeader.setBorderBottom(XSSFCellStyle.BORDER_THIN);
        cellStyleHeader.setBorderTop(XSSFCellStyle.BORDER_THIN);
        cellStyleHeader.setBorderRight(XSSFCellStyle.BORDER_THIN);
        cellStyleHeader.setBorderLeft(XSSFCellStyle.BORDER_THIN);
        cellStyleHeader.setFont(bold);
        cellStyleHeader.setWrapText(false);

        XSSFRow row;
        Cell cell;

        //header
        row = sheet.createRow(0);
        for (int i = 0; i < header.length; i++) {
            cell = row.createCell(i);
            cell.setCellStyle(cellStyleHeader);
            cell.setCellValue(header[i]);
        }

        int colCount = header.length;
        int no = 1;

        for (String[] obj : data) {
            row = sheet.createRow(no);
            for (int i = 0; i < colCount; i++) {
                cell = row.createCell(i);
                cell.setCellStyle(cellStyle);
                cell.setCellValue(obj[i]);
            }
            no++;
        }

        for (int i = 0; i < header.length; i++) {
            sheet.autoSizeColumn(i);
        }

        File newFile = new File(path);
        try (FileOutputStream fileOut = new FileOutputStream(path)) {
            xwb.write(fileOut);
        }

        return newFile;
    } catch (IOException e) {
        return null;
    }
}