Search in sources :

Example 6 with XSSFFont

use of org.apache.poi.xssf.usermodel.XSSFFont in project poi by apache.

the class CellStyleDetails method main.

public static void main(String[] args) throws Exception {
    if (args.length == 0) {
        throw new IllegalArgumentException("Filename must be given");
    }
    Workbook wb = WorkbookFactory.create(new File(args[0]));
    DataFormatter formatter = new DataFormatter();
    for (int sn = 0; sn < wb.getNumberOfSheets(); sn++) {
        Sheet sheet = wb.getSheetAt(sn);
        System.out.println("Sheet #" + sn + " : " + sheet.getSheetName());
        for (Row row : sheet) {
            System.out.println("  Row " + row.getRowNum());
            for (Cell cell : row) {
                CellReference ref = new CellReference(cell);
                System.out.print("    " + ref.formatAsString());
                System.out.print(" (" + cell.getColumnIndex() + ") ");
                CellStyle style = cell.getCellStyle();
                System.out.print("Format=" + style.getDataFormatString() + " ");
                System.out.print("FG=" + renderColor(style.getFillForegroundColorColor()) + " ");
                System.out.print("BG=" + renderColor(style.getFillBackgroundColorColor()) + " ");
                Font font = wb.getFontAt(style.getFontIndex());
                System.out.print("Font=" + font.getFontName() + " ");
                System.out.print("FontColor=");
                if (font instanceof HSSFFont) {
                    System.out.print(renderColor(((HSSFFont) font).getHSSFColor((HSSFWorkbook) wb)));
                }
                if (font instanceof XSSFFont) {
                    System.out.print(renderColor(((XSSFFont) font).getXSSFColor()));
                }
                System.out.println();
                System.out.println("        " + formatter.formatCellValue(cell));
            }
        }
        System.out.println();
    }
    wb.close();
}
Also used : CellReference(org.apache.poi.ss.util.CellReference) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) XSSFFont(org.apache.poi.xssf.usermodel.XSSFFont) Font(org.apache.poi.ss.usermodel.Font) HSSFFont(org.apache.poi.hssf.usermodel.HSSFFont) XSSFFont(org.apache.poi.xssf.usermodel.XSSFFont) HSSFFont(org.apache.poi.hssf.usermodel.HSSFFont) Row(org.apache.poi.ss.usermodel.Row) CellStyle(org.apache.poi.ss.usermodel.CellStyle) File(java.io.File) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) DataFormatter(org.apache.poi.ss.usermodel.DataFormatter)

Example 7 with XSSFFont

use of org.apache.poi.xssf.usermodel.XSSFFont in project poi by apache.

the class BigGridDemo method createStyles.

/**
     * Create a library of cell styles.
     */
private static Map<String, XSSFCellStyle> createStyles(XSSFWorkbook wb) {
    Map<String, XSSFCellStyle> styles = new HashMap<String, XSSFCellStyle>();
    XSSFDataFormat fmt = wb.createDataFormat();
    XSSFCellStyle style1 = wb.createCellStyle();
    style1.setAlignment(HorizontalAlignment.RIGHT);
    style1.setDataFormat(fmt.getFormat("0.0%"));
    styles.put("percent", style1);
    XSSFCellStyle style2 = wb.createCellStyle();
    style2.setAlignment(HorizontalAlignment.CENTER);
    style2.setDataFormat(fmt.getFormat("0.0X"));
    styles.put("coeff", style2);
    XSSFCellStyle style3 = wb.createCellStyle();
    style3.setAlignment(HorizontalAlignment.RIGHT);
    style3.setDataFormat(fmt.getFormat("$#,##0.00"));
    styles.put("currency", style3);
    XSSFCellStyle style4 = wb.createCellStyle();
    style4.setAlignment(HorizontalAlignment.RIGHT);
    style4.setDataFormat(fmt.getFormat("mmm dd"));
    styles.put("date", style4);
    XSSFCellStyle style5 = wb.createCellStyle();
    XSSFFont headerFont = wb.createFont();
    headerFont.setBold(true);
    style5.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    style5.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style5.setFont(headerFont);
    styles.put("header", style5);
    return styles;
}
Also used : XSSFCellStyle(org.apache.poi.xssf.usermodel.XSSFCellStyle) HashMap(java.util.HashMap) XSSFFont(org.apache.poi.xssf.usermodel.XSSFFont) XSSFDataFormat(org.apache.poi.xssf.usermodel.XSSFDataFormat)

Example 8 with XSSFFont

use of org.apache.poi.xssf.usermodel.XSSFFont in project poi by apache.

the class TestThemesTable method testThemesTableColors.

@Test
public void testThemesTableColors() throws Exception {
    // Load our two test workbooks
    XSSFWorkbook simple = XSSFTestDataSamples.openSampleWorkbook(testFileSimple);
    XSSFWorkbook complex = XSSFTestDataSamples.openSampleWorkbook(testFileComplex);
    // Save and re-load them, to check for stability across that
    XSSFWorkbook simpleRS = XSSFTestDataSamples.writeOutAndReadBack(simple);
    XSSFWorkbook complexRS = XSSFTestDataSamples.writeOutAndReadBack(complex);
    // Fetch fresh copies to test with
    simple = XSSFTestDataSamples.openSampleWorkbook(testFileSimple);
    complex = XSSFTestDataSamples.openSampleWorkbook(testFileComplex);
    // Files and descriptions
    Map<String, XSSFWorkbook> workbooks = new LinkedHashMap<String, XSSFWorkbook>();
    workbooks.put(testFileSimple, simple);
    workbooks.put("Re-Saved_" + testFileSimple, simpleRS);
    workbooks.put(testFileComplex, complex);
    workbooks.put("Re-Saved_" + testFileComplex, complexRS);
    // Sanity check
    assertEquals(rgbExpected.length, rgbExpected.length);
    // For offline testing
    boolean createFiles = false;
    //  for the theme-applied cells in Column A are correct
    for (String whatWorkbook : workbooks.keySet()) {
        XSSFWorkbook workbook = workbooks.get(whatWorkbook);
        XSSFSheet sheet = workbook.getSheetAt(0);
        int startRN = 0;
        if (whatWorkbook.endsWith(testFileComplex))
            startRN++;
        for (int rn = startRN; rn < rgbExpected.length + startRN; rn++) {
            XSSFRow row = sheet.getRow(rn);
            assertNotNull("Missing row " + rn + " in " + whatWorkbook, row);
            String ref = (new CellReference(rn, 0)).formatAsString();
            XSSFCell cell = row.getCell(0);
            assertNotNull("Missing cell " + ref + " in " + whatWorkbook, cell);
            int expectedThemeIdx = rn - startRN;
            ThemeElement themeElem = ThemeElement.byId(expectedThemeIdx);
            assertEquals("Wrong theme at " + ref + " in " + whatWorkbook, themeElem.name.toLowerCase(Locale.ROOT), cell.getStringCellValue());
            // Fonts are theme-based in their colours
            XSSFFont font = cell.getCellStyle().getFont();
            CTColor ctColor = font.getCTFont().getColorArray(0);
            assertNotNull(ctColor);
            assertEquals(true, ctColor.isSetTheme());
            assertEquals(themeElem.idx, ctColor.getTheme());
            // Get the colour, via the theme
            XSSFColor color = font.getXSSFColor();
            // Theme colours aren't tinted
            assertEquals(false, color.hasTint());
            // Check the RGB part (no tint)
            assertEquals("Wrong theme colour " + themeElem.name + " on " + whatWorkbook, rgbExpected[expectedThemeIdx], Hex.encodeHexString(color.getRGB()));
            long themeIdx = font.getCTFont().getColorArray(0).getTheme();
            assertEquals("Wrong theme index " + expectedThemeIdx + " on " + whatWorkbook, expectedThemeIdx, themeIdx);
            if (createFiles) {
                XSSFCellStyle cs = row.getSheet().getWorkbook().createCellStyle();
                cs.setFillForegroundColor(color);
                cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
                row.createCell(1).setCellStyle(cs);
            }
        }
        if (createFiles) {
            FileOutputStream fos = new FileOutputStream("Generated_" + whatWorkbook);
            workbook.write(fos);
            fos.close();
        }
    }
}
Also used : CellReference(org.apache.poi.ss.util.CellReference) CTColor(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor) LinkedHashMap(java.util.LinkedHashMap) ThemeElement(org.apache.poi.xssf.model.ThemesTable.ThemeElement) XSSFColor(org.apache.poi.xssf.usermodel.XSSFColor) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFCellStyle(org.apache.poi.xssf.usermodel.XSSFCellStyle) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFFont(org.apache.poi.xssf.usermodel.XSSFFont) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) Test(org.junit.Test)

Example 9 with XSSFFont

use of org.apache.poi.xssf.usermodel.XSSFFont in project bamboobsc by billchen198318.

the class PerspectivesDashboardExcelCommand method putCharts.

@SuppressWarnings("unchecked")
private int putCharts(XSSFWorkbook wb, XSSFSheet sh, Context context) throws Exception {
    String pieBase64Content = SimpleUtils.getPNGBase64Content((String) context.get("pieCanvasToData"));
    String barBase64Content = SimpleUtils.getPNGBase64Content((String) context.get("barCanvasToData"));
    BufferedImage pieImage = SimpleUtils.decodeToImage(pieBase64Content);
    BufferedImage barImage = SimpleUtils.decodeToImage(barBase64Content);
    ByteArrayOutputStream pieBos = new ByteArrayOutputStream();
    ImageIO.write(pieImage, "png", pieBos);
    pieBos.flush();
    ByteArrayOutputStream barBos = new ByteArrayOutputStream();
    ImageIO.write(barImage, "png", barBos);
    barBos.flush();
    SimpleUtils.setCellPicture(wb, sh, pieBos.toByteArray(), 0, 0);
    SimpleUtils.setCellPicture(wb, sh, barBos.toByteArray(), 0, 9);
    int row = 21;
    List<Map<String, Object>> chartDatas = (List<Map<String, Object>>) context.get("chartDatas");
    String year = (String) context.get("year");
    XSSFCellStyle cellHeadStyle = wb.createCellStyle();
    cellHeadStyle.setFillForegroundColor(new XSSFColor(SimpleUtils.getColorRGB4POIColor("#f5f5f5")));
    cellHeadStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    XSSFFont cellHeadFont = wb.createFont();
    cellHeadFont.setBold(true);
    //cellHeadFont.setColor( new XSSFColor( SimpleUtils.getColorRGB4POIColor( "#000000" ) ) );		
    cellHeadStyle.setFont(cellHeadFont);
    int titleRow = row - 1;
    int titleCellSize = 14;
    Row headRow = sh.createRow(titleRow);
    for (int i = 0; i < titleCellSize; i++) {
        Cell headCell = headRow.createCell(i);
        headCell.setCellStyle(cellHeadStyle);
        headCell.setCellValue("Perspectives metrics gauge ( " + year + " )");
    }
    sh.addMergedRegion(new CellRangeAddress(titleRow, titleRow, 0, titleCellSize - 1));
    int cellLeft = 10;
    int rowSpace = 17;
    for (Map<String, Object> data : chartDatas) {
        Map<String, Object> nodeData = (Map<String, Object>) ((List<Object>) data.get("datas")).get(0);
        String pngImageData = SimpleUtils.getPNGBase64Content((String) nodeData.get("outerHTML"));
        BufferedImage imageData = SimpleUtils.decodeToImage(pngImageData);
        ByteArrayOutputStream imgBos = new ByteArrayOutputStream();
        ImageIO.write(imageData, "png", imgBos);
        imgBos.flush();
        SimpleUtils.setCellPicture(wb, sh, imgBos.toByteArray(), row, 0);
        XSSFColor bgColor = new XSSFColor(SimpleUtils.getColorRGB4POIColor((String) nodeData.get("bgColor")));
        XSSFColor fnColor = new XSSFColor(SimpleUtils.getColorRGB4POIColor((String) nodeData.get("fontColor")));
        XSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setFillForegroundColor(bgColor);
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        XSSFFont cellFont = wb.createFont();
        cellFont.setBold(true);
        cellFont.setColor(fnColor);
        cellStyle.setFont(cellFont);
        int perTitleCellSize = 4;
        Row nowRow = sh.createRow(row);
        for (int i = 0; i < perTitleCellSize; i++) {
            Cell cell1 = nowRow.createCell(cellLeft);
            cell1.setCellStyle(cellStyle);
            cell1.setCellValue((String) nodeData.get("name"));
        }
        sh.addMergedRegion(new CellRangeAddress(row, row, cellLeft, cellLeft + perTitleCellSize - 1));
        nowRow = sh.createRow(row + 1);
        Cell cell2 = nowRow.createCell(cellLeft);
        cell2.setCellValue("Target: " + String.valueOf(nodeData.get("target")));
        nowRow = sh.createRow(row + 2);
        Cell cell3 = nowRow.createCell(cellLeft);
        cell3.setCellValue("Min: " + String.valueOf(nodeData.get("min")));
        nowRow = sh.createRow(row + 3);
        Cell cell4 = nowRow.createCell(cellLeft);
        cell4.setCellValue("Score: " + String.valueOf(nodeData.get("score")));
        row += rowSpace;
    }
    return row;
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedImage(java.awt.image.BufferedImage) XSSFColor(org.apache.poi.xssf.usermodel.XSSFColor) XSSFCellStyle(org.apache.poi.xssf.usermodel.XSSFCellStyle) XSSFFont(org.apache.poi.xssf.usermodel.XSSFFont) List(java.util.List) Row(org.apache.poi.ss.usermodel.Row) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) Map(java.util.Map) Cell(org.apache.poi.ss.usermodel.Cell)

Example 10 with XSSFFont

use of org.apache.poi.xssf.usermodel.XSSFFont in project bamboobsc by billchen198318.

the class OrganizationReportExcelCommand method createMainBody.

private int createMainBody(XSSFWorkbook wb, XSSFSheet sh, int row, VisionVO vision) throws Exception {
    XSSFCellStyle cellStyle = wb.createCellStyle();
    cellStyle.setFillForegroundColor(new XSSFColor(SimpleUtils.getColorRGB4POIColor("#ffffff")));
    cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    XSSFFont cellFont = wb.createFont();
    cellFont.setBold(false);
    cellFont.setColor(new XSSFColor(SimpleUtils.getColorRGB4POIColor("#000000")));
    cellStyle.setFont(cellFont);
    cellStyle.setWrapText(true);
    cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
    cellStyle.setBorderBottom(BorderStyle.THIN);
    cellStyle.setBorderTop(BorderStyle.THIN);
    cellStyle.setBorderRight(BorderStyle.THIN);
    cellStyle.setBorderLeft(BorderStyle.THIN);
    int mrRow = row;
    for (int px = 0; px < vision.getPerspectives().size(); px++) {
        PerspectiveVO perspective = vision.getPerspectives().get(px);
        for (int ox = 0; ox < perspective.getObjectives().size(); ox++) {
            ObjectiveVO objective = perspective.getObjectives().get(ox);
            for (int kx = 0; kx < objective.getKpis().size(); kx++) {
                KpiVO kpi = objective.getKpis().get(kx);
                Row contentRow = sh.createRow(row++);
                Cell cell1 = contentRow.createCell(0);
                cell1.setCellValue(perspective.getName());
                cell1.setCellStyle(cellStyle);
                Cell titleCell2 = contentRow.createCell(1);
                titleCell2.setCellValue(objective.getName());
                titleCell2.setCellStyle(cellStyle);
                Cell titleCell3 = contentRow.createCell(2);
                titleCell3.setCellValue(kpi.getName());
                titleCell3.setCellStyle(cellStyle);
                Cell titleCell4 = contentRow.createCell(3);
                titleCell4.setCellValue(kpi.getWeight() + "%");
                titleCell4.setCellStyle(cellStyle);
                Cell titleCell5 = contentRow.createCell(4);
                titleCell5.setCellValue("max: " + kpi.getMax() + "\n" + "target: " + kpi.getTarget() + "\n" + "min: " + kpi.getMin() + "\n" + "unit: " + kpi.getUnit());
                titleCell5.setCellStyle(cellStyle);
                DateRangeScoreVO dateRangeScore = kpi.getDateRangeScores().get(0);
                XSSFCellStyle cellStyle2 = wb.createCellStyle();
                cellStyle2.setFillForegroundColor(new XSSFColor(SimpleUtils.getColorRGB4POIColor(dateRangeScore.getBgColor())));
                cellStyle2.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                XSSFFont cellFont2 = wb.createFont();
                cellFont2.setBold(false);
                cellFont2.setColor(new XSSFColor(SimpleUtils.getColorRGB4POIColor(dateRangeScore.getFontColor())));
                cellStyle2.setFont(cellFont2);
                cellStyle2.setWrapText(true);
                cellStyle2.setVerticalAlignment(VerticalAlignment.CENTER);
                cellStyle2.setBorderBottom(BorderStyle.THIN);
                cellStyle2.setBorderTop(BorderStyle.THIN);
                cellStyle2.setBorderRight(BorderStyle.THIN);
                cellStyle2.setBorderLeft(BorderStyle.THIN);
                Cell titleCell6 = contentRow.createCell(5);
                titleCell6.setCellValue(BscReportSupportUtils.parse2(dateRangeScore.getScore()));
                titleCell6.setCellStyle(cellStyle2);
            }
        }
    }
    for (int px = 0; px < vision.getPerspectives().size(); px++) {
        PerspectiveVO perspective = vision.getPerspectives().get(px);
        // 2016-12-13 old work with POI 3.12
        //sh.addMergedRegion(new CellRangeAddress(mrRow, mrRow + perspective.getRow()-1, 0, 0));
        // 2016-12-13 new work with POI 3.15
        int mrRow1 = mrRow + perspective.getRow() - 1;
        if (mrRow1 > mrRow) {
            sh.addMergedRegion(new CellRangeAddress(mrRow, mrRow1, 0, 0));
        }
        for (int ox = 0; ox < perspective.getObjectives().size(); ox++) {
            ObjectiveVO objective = perspective.getObjectives().get(ox);
            // 2016-12-13 old work with POI 3.12
            //sh.addMergedRegion(new CellRangeAddress(mrRow, mrRow + objective.getRow()-1, 1, 1));
            // 2016-12-13 new work with POI 3.15
            int mrRow2 = mrRow + objective.getRow() - 1;
            if (mrRow2 > mrRow) {
                sh.addMergedRegion(new CellRangeAddress(mrRow, mrRow2, 1, 1));
            }
            mrRow += objective.getKpis().size();
        }
    }
    return row++;
}
Also used : XSSFColor(org.apache.poi.xssf.usermodel.XSSFColor) XSSFCellStyle(org.apache.poi.xssf.usermodel.XSSFCellStyle) ObjectiveVO(com.netsteadfast.greenstep.vo.ObjectiveVO) XSSFFont(org.apache.poi.xssf.usermodel.XSSFFont) KpiVO(com.netsteadfast.greenstep.vo.KpiVO) DateRangeScoreVO(com.netsteadfast.greenstep.vo.DateRangeScoreVO) PerspectiveVO(com.netsteadfast.greenstep.vo.PerspectiveVO) Row(org.apache.poi.ss.usermodel.Row) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) Cell(org.apache.poi.ss.usermodel.Cell)

Aggregations

XSSFFont (org.apache.poi.xssf.usermodel.XSSFFont)27 XSSFCellStyle (org.apache.poi.xssf.usermodel.XSSFCellStyle)22 Cell (org.apache.poi.ss.usermodel.Cell)17 Row (org.apache.poi.ss.usermodel.Row)17 XSSFColor (org.apache.poi.xssf.usermodel.XSSFColor)17 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)13 List (java.util.List)6 KpiVO (com.netsteadfast.greenstep.vo.KpiVO)5 DateRangeScoreVO (com.netsteadfast.greenstep.vo.DateRangeScoreVO)4 ObjectiveVO (com.netsteadfast.greenstep.vo.ObjectiveVO)4 PerspectiveVO (com.netsteadfast.greenstep.vo.PerspectiveVO)4 Map (java.util.Map)4 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)4 BufferedImage (java.awt.image.BufferedImage)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 HashMap (java.util.HashMap)3 XSSFCell (org.apache.poi.xssf.usermodel.XSSFCell)3 XSSFRow (org.apache.poi.xssf.usermodel.XSSFRow)3 XSSFCellBorder (org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder)3 XSSFCellFill (org.apache.poi.xssf.usermodel.extensions.XSSFCellFill)3