如何更改文本颜色和填充颜色

时间:2022-11-20 22:56:38

How do I change the header font color to white and the fill green? These are the classes that I am using:

如何将标题字体颜色改为白色,填充为绿色?这些是我正在使用的类:

import static org.apache.poi.ss.usermodel.CellStyle.*
import static org.apache.poi.ss.usermodel.IndexedColors.*
import org.apache.poi.hssf.usermodel.*
import org.apache.poi.hssf.usermodel.HSSFWorkbook
import org.apache.poi.ss.usermodel.Cell
import org.apache.poi.ss.usermodel.CellStyle
import org.apache.poi.ss.usermodel.Row
import org.apache.poi.ss.usermodel.Sheet
import org.apache.poi.ss.usermodel.Workbook
import org.apache.poi.ss.usermodel.Font

And this is the code where, I believe, it will have to be inserted into.

这是代码,我相信,它必须被插入。

Font headerFont = wb.createFont();
headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD)
CellStyle headerStyle = wb.createCellStyle()
headerStyle.setFont(headerFont)

cellMOPID.setCellStyle(headerStyle)
cellType.setCellStyle(headerStyle)
cellStatus.setCellStyle(headerStyle)
cellState.setCellStyle(headerStyle)
cellStartDate.setCellStyle(headerStyle)
cellEndDate.setCellStyle(headerStyle)
cellDesc.setCellStyle(headerStyle)

3 个解决方案

#1


12  

     HSSFCellStyle cellStyle = workBook.createCellStyle();        
     HSSFFont font = wb.createFont();
     font.setFontName(XSSFFont.DEFAULT_FONT_NAME);
     font.setFontHeightInPoints((short)10);
     font.setColor(IndexedColors.BLUE.getIndex());
     cellStyle.setFont(font);
    //the version i am using is poi-3.8

#2


9  

If you want to set the color as simple cellstyle... you can write code like.

如果你想将颜色设置为简单的cellstyle……您可以像这样编写代码。

 cell.setCellValue("Header Text");
 XSSFCellStyle headerStyle = wb.createCellStyle();
 Font headerFont = wb.createFont();
 headerStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
 headerFont.setColor(IndexedColors.WHITE.getIndex());
 headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
 headerStyle.setFont(headerFont);
 cell.setCellStyle(headerStyle);

This will fill the cell with GREEN color and the font will be of Bold WHITE color

这将使单元格充满绿色,字体将是粗体白色

#3


8  

For xls file I have checked the following and is working fine on my end.

对于xls文件,我已经检查了以下内容,在我这边运行良好。

I need to import following:

我需要导入以下内容:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;

and the piece of code is following:

这段代码如下:

    FileInputStream fin = new FileInputStream (XLSFileAddress);
    HSSFWorkbook wb = new HSSFWorkbook(fin);
    HSSFCell cell=wb.getSheetAt(2).getRow(0).getCell(0);
    cell.setCellValue("Header Text");
    Font headerFont = wb.createFont();
    headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    CellStyle headerStyle = wb.createCellStyle();
    headerStyle.setFont(headerFont);
    headerStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
    headerFont.setColor(IndexedColors.WHITE.getIndex());
    headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    cell.setCellStyle(headerStyle);
    FileOutputStream fileOut = new FileOutputStream(XLSFileAddress);
    wb.write(fileOut);
    fileOut.close();

The output of this code is the first cell of first Row of sheet at index two, will show the text "Header Text" with cell color as Green and Text Color is White with Bold Font.

该代码的输出是索引2第一行的第一个单元格,将显示以绿色为单元格颜色的文本“标题文本”,文本颜色为白色,字体为粗体。

I am using apache POI 3.9.

我正在使用apache POI 3.9。

#1


12  

     HSSFCellStyle cellStyle = workBook.createCellStyle();        
     HSSFFont font = wb.createFont();
     font.setFontName(XSSFFont.DEFAULT_FONT_NAME);
     font.setFontHeightInPoints((short)10);
     font.setColor(IndexedColors.BLUE.getIndex());
     cellStyle.setFont(font);
    //the version i am using is poi-3.8

#2


9  

If you want to set the color as simple cellstyle... you can write code like.

如果你想将颜色设置为简单的cellstyle……您可以像这样编写代码。

 cell.setCellValue("Header Text");
 XSSFCellStyle headerStyle = wb.createCellStyle();
 Font headerFont = wb.createFont();
 headerStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
 headerFont.setColor(IndexedColors.WHITE.getIndex());
 headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
 headerStyle.setFont(headerFont);
 cell.setCellStyle(headerStyle);

This will fill the cell with GREEN color and the font will be of Bold WHITE color

这将使单元格充满绿色,字体将是粗体白色

#3


8  

For xls file I have checked the following and is working fine on my end.

对于xls文件,我已经检查了以下内容,在我这边运行良好。

I need to import following:

我需要导入以下内容:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;

and the piece of code is following:

这段代码如下:

    FileInputStream fin = new FileInputStream (XLSFileAddress);
    HSSFWorkbook wb = new HSSFWorkbook(fin);
    HSSFCell cell=wb.getSheetAt(2).getRow(0).getCell(0);
    cell.setCellValue("Header Text");
    Font headerFont = wb.createFont();
    headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    CellStyle headerStyle = wb.createCellStyle();
    headerStyle.setFont(headerFont);
    headerStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
    headerFont.setColor(IndexedColors.WHITE.getIndex());
    headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    cell.setCellStyle(headerStyle);
    FileOutputStream fileOut = new FileOutputStream(XLSFileAddress);
    wb.write(fileOut);
    fileOut.close();

The output of this code is the first cell of first Row of sheet at index two, will show the text "Header Text" with cell color as Green and Text Color is White with Bold Font.

该代码的输出是索引2第一行的第一个单元格,将显示以绿色为单元格颜色的文本“标题文本”,文本颜色为白色,字体为粗体。

I am using apache POI 3.9.

我正在使用apache POI 3.9。