如何改变特定的细胞的Apache POI 3.9的字体颜色 [英] How to change font color of particular cell apache poi 3.9

查看:173
本文介绍了如何改变特定的细胞的Apache POI 3.9的字体颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以改变前景色与Apache的POI以下code。现在,我想换一个单元格的字体颜色。

  CellStyle风格= wb.createCellStyle();
                        style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
                        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
                        电池= rowxl.createCell((短)7);
                        cell.setCellValue(&所述;&所述;&所述;&所述; ONTRACK>>>>中);
                        cell.setCellStyle(样式);
                        rowxl.createCell(0).setCellValue(TEAM);

我也试过,但它并没有改变前两栏的颜色

code:

 公共类FCLR {
     公共静态无效的主要(字串[] args)抛出异常{         InputStream的INP =新的FileInputStream(C:/workbook1.xls);
            工作簿WB = WorkbookFactory.create(INP);
            CreationHelper createHelper = wb.getCreationHelper();
            苫布苫布= wb.getSheetAt(0);
            行rowxl = sheet.createRow((短)0);
            细胞细胞= rowxl.createCell(0);            //从标准调色板中应用某些颜色,
            //作为在previous例子。
            //我们将在石灰背景用红色文字            CellStyle风格= wb.createCellStyle();
          rowxl.createCell(1).setCellValue(ABC);
        rowxl.createCell(2).setCellValue(AAA);
            字体字型= wb.createFont();
            font.setColor(HSSFColor.BLACK.index);
            style.setFont(字体);
            cell.setCellStyle(样式);            FileOutputStream中FILEOUT =新的FileOutputStream(C:/workbook1.xls);
            wb.write(FILEOUT);
            fileOut.close();     }}


解决方案

您目前正在制作一些你的细胞的两倍,这就是为什么这一切都走错了。

首先,我建议你移动单元格样式创建到更接近你的code的顶部。切记! - 单元格样式范围到工作簿,所以不要创建每单元一个

  CellStyle风格= wb.createCellStyle();
        字体字型= wb.createFont();
        font.setColor(HSSFColor.BLACK.index);
        style.setFont(字体);
        //根据需要设置风格上更多的颜色
        //根据需要的样式设置格式规则

现在,根据您的preference,无论是做你的细胞创造这样的:

 细胞细胞;        细胞= rowxl.createCell(0);
        cell.setCellValue(ABC);
        cell.setCellStyle(样式);        细胞= rowxl.createCell(1);
        cell.setCellValue(AAA);
        cell.setCellStyle(样式);

或者是这样的:

  rowxl.createCell(1).setCellValue(ABC);
    rowxl.createCell(2).setCellValue(AAA);
    rowx1.getCell(1).setCellStyle(样式);
    rowx1.getCell(2).setCellStyle(样式);

只是不要做奇怪的混合,你此刻已经得到了,因为你最终创造细胞两次,并错过了造型!

I can change foreground color with the following code in apache POI. Now I want to change font color of a single cell.

CellStyle style = wb.createCellStyle();
                        style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
                        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
                        cell = rowxl.createCell((short) 7);
                        cell.setCellValue(" <<<<ONTRACK>>>>");
                        cell.setCellStyle(style);


                        rowxl.createCell(0).setCellValue(TEAM);

I have tried this but it does not change the color of first two columns

code:

public class fclr {
     public static void main(String[] args)  throws Exception {

         InputStream inp = new FileInputStream("c:/workbook1.xls");
            Workbook wb = WorkbookFactory.create(inp);
            CreationHelper createHelper = wb.getCreationHelper();
            Sheet sheet = wb.getSheetAt(0);
            Row rowxl = sheet.createRow((short)0);


            Cell cell = rowxl.createCell(0);

            //apply some colors from the standard palette,
            // as in the previous examples.
            //we'll use red text on a lime background

            CellStyle style = wb.createCellStyle();


          rowxl.createCell(1).setCellValue("ABC");
        rowxl.createCell(2).setCellValue("aaa");
            Font font = wb.createFont();
            font.setColor(HSSFColor.BLACK.index);
            style.setFont(font);


            cell.setCellStyle(style);

            FileOutputStream fileOut = new FileOutputStream("c:/workbook1.xls");
            wb.write(fileOut);
            fileOut.close();



     }

} 

解决方案

You're currently creating some of your cells twice, which is why it's all going wrong

Firstly, I'd suggest you move the cell style creation to nearer the top of your code. Remember - cell styles are scoped to a workbook, so don't create one per cell!

        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setColor(HSSFColor.BLACK.index);
        style.setFont(font);
        // Set more colours on the style as needed
        // Set formatting rules on the style as needed

Now, depending on your preference, either do your cell creation like this:

        Cell cell;

        cell = rowxl.createCell(0);
        cell.setCellValue("ABC");
        cell.setCellStyle(style);

        cell = rowxl.createCell(1);
        cell.setCellValue("aaa");
        cell.setCellStyle(style);

Or like this:

    rowxl.createCell(1).setCellValue("ABC");
    rowxl.createCell(2).setCellValue("aaa");
    rowx1.getCell(1).setCellStyle(style);
    rowx1.getCell(2).setCellStyle(style);

Just don't do that weird hybrid you've got at the moment, as you end up creating cells twice and missing out styling!

这篇关于如何改变特定的细胞的Apache POI 3.9的字体颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆