使用Java apache POI在excel行中填充背景颜色

时间:2015-11-26 11:02:11

标签: java excel apache-poi

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

document.getElementByID('video1').style.visibility = "visible"; // instead of true

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

2 个答案:

答案 0 :(得分:1)

事实证明,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");
    }
}

答案 1 :(得分:0)

简而言之:setRowStyle没有按照您的假设做到。

所有它(see source)都将样式注册为行的默认样式。

  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的原因。