Java APACHE POI-无法为单元格提供背景颜色
我正在使用JavaPOI生成excel文件,并尝试为excel中的列标题添加背景色Java APACHE POI-无法为单元格提供背景颜色,java,excel,apache-poi,Java,Excel,Apache Poi,我正在使用JavaPOI生成excel文件,并尝试为excel中的列标题添加背景色 Row row = sheet.createRow(0); CellStyle style = workbook.createCellStyle(); style.setFillForegroundColor(IndexedColors.YELLOW.getIndex()); style.setFillPattern(CellStyle.BIG_SPOTS);
Row row = sheet.createRow(0);
CellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
style.setFillPattern(CellStyle.BIG_SPOTS);
row.setRowStyle(style);
for(RiskVo h : selectedExcel){
row.createCell(h.getIndex()).setCellValue(h.getRisk_disc_en());
}
但颜色仅显示在从低到空的列中
我想要第一行的背景色。如何解决此问题。我猜您的
h.getIndex()
从工作表的开头开始。因此,在设置行的样式后,可以在行的开头创建新单元格,并将现有单元格向右移动。您的解决方案是:
Row row = sheet.createRow(0);
for(RiskVo h : selectedExcel){
row.createCell(h.getIndex()).setCellValue(h.getRisk_disc_en());
}
CellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
style.setFillPattern(CellStyle.BIG_SPOTS);
row.setRowStyle(style);
首先,创建单元,然后在循环内设置样式。这是我的工作
Row row = sheet.createRow(0);
CellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
for(RiskVo h : selectedExcel){
row.createCell(h.getIndex()).setCellValue(h.getRisk_disc_en());
row.getCell(h.getIndex()).setCellStyle(style);
}
我创建了一个非常方便的实用程序类,可以非常轻松地给单元格的背景上色,更改文本颜色,以及许多其他可能需要的东西,以便从代码中创建非常棒的电子表格。它们是静态方法,它们立即返回
CellStyle
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Workbook;
/*
Utility Class for Spreadsheet Cell Styling
*/
public class CellStyles {
public static CellStyle getTableHeaderStyle(Workbook workbook){
CellStyle style = workbook.createCellStyle();
Font font = workbook.createFont();
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
font.setFontName("Times New Roman");
style.setWrapText(true);
style.setFont(font);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setLocked(true);
return style;
}
public static CellStyle getShadedCellStyle(Workbook workbook, HSSFColor foreground, HSSFColor background ){
CellStyle style = workbook.createCellStyle();
Font font = workbook.createFont();
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
font.setFontName("Times New Roman");
style.setFont(font);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setFillForegroundColor(foreground.getIndex());
style.setFillBackgroundColor(background.getIndex());
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBorderTop(CellStyle.BORDER_THIN);
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setBorderRight(CellStyle.BORDER_THIN);
style.setWrapText(true);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setLocked(true);
return style;
}
public static CellStyle getColoredTextCellStyle(Workbook workbook, HSSFColor color){
CellStyle style = workbook.createCellStyle();
Font font = workbook.createFont();
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
font.setFontName("Times New Roman");
style.setFont(font);
style.setFillBackgroundColor(color.getIndex());
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setWrapText(true);
font.setColor(color.getIndex());
return style;
}
}
例如:
Workbook workbook = new HSSFWorkbook();
CellStyle style = CellStyles.getShadedCellStyle(workbook, new HSSFColor.BRIGHT_GREEN(), new HSSFColor.BLACK()));
...
someCell.setCellStyle(style);
对于(RiskVo h:selectedExcel){row.createCell(h.getIndex()).setCellValue(h.getRisk_disc_en());row.getCell(h.getIndex()).setCellStyle(style);}“像这样。”纳文,但我认为你的解决办法也应该奏效