Java 如何更改文本颜色和填充颜色
如何将标题字体颜色更改为白色,填充为绿色?以下是我正在使用的类:Java 如何更改文本颜色和填充颜色,java,apache-poi,Java,Apache Poi,如何将标题字体颜色更改为白色,填充为绿色?以下是我正在使用的类: 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.apach
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
我相信,这就是必须插入的代码
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)
如果要将颜色设置为简单单元格样式。。。您可以像这样编写代码
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);
这将用绿色填充单元格,字体将为粗体白色对于xls文件,我已经检查了以下内容,并且在我这方面工作正常
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
我需要导入以下内容:
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;
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();
代码如下:
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;
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();
此代码的输出是索引2处工作表第一行的第一个单元格,将显示文本“标题文本”,单元格颜色为绿色,文本颜色为白色,字体为粗体
我使用的是ApachePOI3.9。XSSFCellStyle位于哪个jar文件中?这看起来是我需要的!我正在使用ApachePOI3.9。XSSFCellStyle位于org.apache.poi.xssf.usermodel.XSSFCellStyleI列表导入org.apache.poi.xssf.usermodel.XSSFCellStyle位于文档顶部,但我收到一条“无法解析类org.apache.poi.xssf.usermodel.XSSFCellStyle”消息。这是否意味着我没有它?问题可能是因为您使用的是HSSF和xls文件。我使用XSSF和xlsx文件。如果它没有改变您的需求,请切换xlsx文件并使用XSSF。如果您需要xls文件,请保留在HSSF上。也许会帮助您获得它。不需要在没有任何特定内存问题的情况下从HSSF迁移到XSSF。您也可以使用HSSF为字体应用颜色。