繁体   English   中英

使用Java Apache POI在Excel行中填充背景色

[英]Filling background color in excel rows using Java apache POI

我正在尝试阅读一个Excel工作表,并使用以下代码填充行的背景色:

....
HSSFCellStyle cellStyle1 = workbook.createCellStyle();
cellStyle1.setFillForegroundColor(new HSSFColor.BLACK().getIndex());
cellStyle1.setFillBackgroundColor(new HSSFColor.RED().getIndex());
cellStyle1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

for(int i=0;i < rowCount;i++){
     sheet.getRow(i).setRowStyle(cellStyle1);
}
...

当我运行代码时,颜色仅填充空白单元格。 对于包含数据的所有单元格,颜色没有变化。 有人可以告诉我为什么会这样吗?

事实证明,set setFillForegroundColor用于设置单元格背景色。 注释掉setFillBackgroundColor,它应该可以工作。

CellStyle cellStyle1 = workbook.createCellStyle();
cellStyle1.setFillForegroundColor(IndexedColors.RED.index);
//cellStyle1.setFillBackgroundColor(IndexedColors.RED.index);
cellStyle1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

编辑**

工作测试代码

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
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.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;


public class TestPoi {
    public static void main(String[] args) throws Exception {
        System.out.println("Started");
        Workbook workbook = new HSSFWorkbook(new FileInputStream("input.xls"));

        CellStyle cellStyle1 = workbook.createCellStyle();
        cellStyle1.setFillForegroundColor(IndexedColors.RED.index);
        //cellStyle1.setFillBackgroundColor(IndexedColors.RED.index);
        cellStyle1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

        Sheet sheet = workbook.getSheet("Sheet1");

        Iterator<Row> rowIterator = sheet.rowIterator();
        while(rowIterator.hasNext()){
             Row row = rowIterator.next();
             Iterator<Cell> cellIterator = row.cellIterator();
             while(cellIterator.hasNext()) {
                 Cell cell = cellIterator.next();
                 cell.setCellStyle(cellStyle1);
                 /*HSSFCellStyle style = (HSSFCellStyle)cell.getCellStyle();
                 style.setFillBackgroundColor(IndexedColors.RED.index);*/
                 System.out.println(cell.getStringCellValue());
             }
        }
        workbook.write(new FileOutputStream("output.xls"));
        System.out.println("Ended");
    }
}

简而言之: setRowStyle并没有像您setRowStyle的那样做。

它所做的一切( 请参阅源代码 )是将样式注册为行的默认样式。

  row.setFormatted(true);
  row.setXFIndex(style.getIndex());

它不会遍历一行中的所有单元格并更改其样式。 因此,该样式仅适用于不存在的单元格( 1 )和默认情况下引用行样式的新创建的单元格。

从上面的代码中可以看到,样式仅由其索引引用。 为了允许在行和各种单元格上使用不同的样式,单元格必须能够引用不同的样式。 然后,单元格样式将在逻辑上取代行样式。 因此,要将样式应用于所有单元格,不仅必须将样式分配给行,还必须将样式分配给所有现有的单元格。

您在问题中说,您正在阅读文档,然后尝试为行着色。 因此,我将假定您实际上不是自己创建新的单元格,因为POI随后应复制行样式,这可能是一个错误。

在您的情况下,可能有点像这样(简化):
文档中的现有单元格引用index 0的样式。 现在,您将创建一个index 1的新样式,并通过setRowStyle将其应用于行。
所有不存在的( 1 )和新单元格将使用index 1的样式。 但是,现有单元格仍指向index 0的样式,因为尚未自动为其分配新样式。

您可能希望setRowStyle的行为类似于Excel应用程序,您可以在其中选择整行并在其上设置样式。 但这不是它的工作方式。 您必须手动进行迭代并将更改应用到所有单元格。


1 :为节省空间,仅逻辑上存在于Excel应用程序中但尚未物理存在于数据结构中的单元格。 这就是为什么默认情况下getCell可以返回null而不只是返回CELL_TYPE_BLANK的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM