Java apache poi:excel单元格颜色
我正试图用它来改变一个单元格的背景 我知道关于这个问题有很多答案,但我使用的是最新版本(3.16),它们都不推荐使用 例如,所有的答案都建议我使用Java apache poi:excel单元格颜色,java,excel,apache,colors,javadoc,Java,Excel,Apache,Colors,Javadoc,我正试图用它来改变一个单元格的背景 我知道关于这个问题有很多答案,但我使用的是最新版本(3.16),它们都不推荐使用 例如,所有的答案都建议我使用 CellStyle#setFillPattern(CellStyle.SOLID_FOREGROUND); 但它完全被弃用了 因此,遵循apache文档,我用新函数替换了所有不推荐使用的函数,并提出了以下MCVE: import java.io.File; import java.io.FileOutputStream; import java.i
CellStyle#setFillPattern(CellStyle.SOLID_FOREGROUND);
但它完全被弃用了
因此,遵循apache文档,我用新函数替换了所有不推荐使用的函数,并提出了以下MCVE:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Mcve{
public static void main(String[] args) {
//Make workbook and first sheet
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("sheet1");
//Make a style
XSSFCellStyle style = workbook.createCellStyle();
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setFillBackgroundColor(IndexedColors.RED.getIndex());
//Fill first line
Row row = sheet.createRow(0);
int i = 0;
while (i < 5) {
Cell cell = row.createCell(i);
cell.setCellValue("TestCell " + i++);
cell.setCellStyle(style);
}
//Write to file
File f = new File("Yourfilepathhere/document.xlsx"); //<-- FILL HERE
try (FileOutputStream out = new FileOutputStream(f)) {
workbook.write(out);
workbook.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
导入java.io.File;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.ss.usermodel.FillPatternType;
导入org.apache.poi.ss.usermodel.IndexedColors;
导入org.apache.poi.ss.usermodel.Row;
导入org.apache.poi.xssf.usermodel.XSSFCellStyle;
导入org.apache.poi.xssf.usermodel.xssfheet;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
公共级Mcve{
公共静态void main(字符串[]args){
//制作工作簿和第一页
XSSFWorkbook工作簿=新XSSFWorkbook();
XSSFSheet sheet=workbook.createSheet(“sheet1”);
//形成一种风格
XSSFCellStyle=workbook.createCellStyle();
style.setFillPattern(FillPatternType.SOLID\u前景);
style.setFillBackgroundColor(IndexedColors.RED.getIndex());
//填写第一行
Row Row=sheet.createRow(0);
int i=0;
而(i<5){
Cell Cell=行createCell(i);
setCellValue(“TestCell”+i++);
cell.setCellStyle(style);
}
//写入文件
File f=新文件(“Yourfilepathhere/document.xlsx”);//您忘记添加
cell.setCellStyle(style);
(希望有帮助)看起来像一个bug,但是你可以尝试设置圆形而不是背景色
XSSFCellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.RED.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
row.getCell(0).setCellStyle(style);
XSSFWorkbook workbook1 = new XSSFWorkbook();
XSSFCellStyle greyBackgroundBold = workbook1.createCellStyle();
greyBackgroundBold.setFont(font);
greyBackgroundBold.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
greyBackgroundBold.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
这将设置您的背景色。现在将其添加到mcve中。我忘了在那里复制它,但在我的主代码中我有它,并且它不起作用。有什么想法吗?你是对的。有。很抱歉,我没有预料到。setFillForegroundColor设置背景…谈论自我解释的方法名称。我希望这只是一个bug!
XSSFCellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.RED.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
row.getCell(0).setCellStyle(style);
XSSFWorkbook workbook1 = new XSSFWorkbook();
XSSFCellStyle greyBackgroundBold = workbook1.createCellStyle();
greyBackgroundBold.setFont(font);
greyBackgroundBold.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
greyBackgroundBold.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);