Java apache poi: excel 单元格颜色
Java apache poi: excel cell color
我正在尝试使用 apache poi 更改单元格的背景。
我知道关于这个问题有很多答案,但我使用的是最新版本 (3.16),它们都是 已弃用.
例如所有答案都建议我使用
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();
}
}
}
我建议您将它粘贴到您选择的 IDE 中的空 Maven 项目中,并请将这些依赖项添加到您的 pom.xml
:
现在,在 Excel 的最新版本中,这会打印全黑单元格或普通的白色背景单元格,具体取决于颜色。
我试过几种颜色和款式,似乎都行不通。文本始终存在,但背景不适用。
伙计们,我做错了什么?
您忘记添加
cell.setCellStyle(style);
希望对您有所帮助)
看起来像错误,但您可以尝试设置前景色而不是背景色。
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);
我正在尝试使用 apache poi 更改单元格的背景。
我知道关于这个问题有很多答案,但我使用的是最新版本 (3.16),它们都是 已弃用.
例如所有答案都建议我使用
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();
}
}
}
我建议您将它粘贴到您选择的 IDE 中的空 Maven 项目中,并请将这些依赖项添加到您的 pom.xml
:
现在,在 Excel 的最新版本中,这会打印全黑单元格或普通的白色背景单元格,具体取决于颜色。 我试过几种颜色和款式,似乎都行不通。文本始终存在,但背景不适用。
伙计们,我做错了什么?
您忘记添加
cell.setCellStyle(style);
希望对您有所帮助)
看起来像错误,但您可以尝试设置前景色而不是背景色。
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);