如何更改文本颜色和填充颜色 [英] How do I change the Text Color and the Fillcolor

查看:131
本文介绍了如何更改文本颜色和填充颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 进口静态org.apache.poi.ss.usermodel.CellStyle。*
引入静态org.apache.poi.ss.usermodel.IndexedColors。*
导入org.apache.poi.hssf.usermodel。*
进口org.apache.poi.hssf.usermodel.HSSFWorkbook
进口org.apache.poi.ss.usermodel.Cell
进口org.apache.poi.ss.usermodel.CellStyle
进口org.apache.poi.ss.usermodel.Row
进口org.apache.poi.ss.usermodel.Sheet
进口org.apache.poi.ss.usermodel.Workbook
进口org.apache.poi.ss.usermodel.Font

和这是code其中,我相信,它必须被插入

 字体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)


解决方案

有关xls文件我已经检查了以下,并在我结束工作的罚款。

我需要输入以下内容:

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

和一块code是以下内容:

 的FileInputStream鳍=新的FileInputStream(XLSFileAddress);
    HSSFWorkbook WB =新HSSFWorkbook(翅);
    HSSFCell细胞= wb.getSheetAt(2).getRow(0).getCell(0);
    cell.setCellValue(头文本);
    字体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 =新的FileOutputStream(XLSFileAddress);
    wb.write(FILEOUT);
    fileOut.close();

这code的输出是片的第一行的索引两个第一个单元格,将显示文本的头文字单元格颜色为绿色和文本颜色为白色,Bold字体

我使用Apache POI 3.9。

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)

解决方案

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

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.

I am using apache POI 3.9.

这篇关于如何更改文本颜色和填充颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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