APACHE POI - 无法为单元格提供背景颜色

问题描述 投票:0回答:3

我正在使用java poi生成excel文件,并尝试为excel中的列标题添加背景颜色。

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());
        }

但颜色仅显示在低到空的列中。

enter image description here我想要第一行的背景颜色。我怎样才能解决这个问题。

java excel apache-poi
3个回答
2
投票

首先,创建单元格,然后在循环内设置样式。这对我有用。

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);
}

0
投票

我想你的h.getIndex()从工作表的开头开始。因此,在为行设置样式后,您可以在行的开头创建新单元格并将现有单元格移动到右侧。您的解决方案是:

  1. 创建标题单元格,然后设置样式

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);
  1. 设置样式后,不要创建新单元格,而是从行中获取单元格并插入值。

0
投票

我有一个非常方便的实用程序类,我可以非常轻松地为单元格的背景着色,更改文本颜色,以及许多其他可能需要从代码中创建非常棒的电子表格的东西。它们是静态方法,它们立即返回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);
© www.soinside.com 2019 - 2024. All rights reserved.