Java excel poi:将前景色应用于空白单元格

Java excel poi:将前景色应用于空白单元格,java,apache-poi,Java,Apache Poi,我想使用poi将excel文件的所有单元格设置为特定颜色。然而,我不断地得到一个空单元格的空指针异常。这就是我到目前为止所做的: HSSFWorkbook workBook = new HSSFWorkbook(); HSSFCellStyle whiteFG = workBook.createCellStyle(); whiteFG.setFillForegroundColor(HSSFColor.AQUA.index); whi

我想使用poi将excel文件的所有单元格设置为特定颜色。然而,我不断地得到一个空单元格的空指针异常。这就是我到目前为止所做的:

        HSSFWorkbook workBook = new HSSFWorkbook();
        HSSFCellStyle whiteFG = workBook.createCellStyle();
        whiteFG.setFillForegroundColor(HSSFColor.AQUA.index);
        whiteFG.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

        //each row has 20 columns
        int numColumns = 20;

        for (int colNum = 0 ; colNum < numColumns ; colNum++){  

            HSSFCell cell = row.getCell((short)colNum); 

            if (cell != null){
                cell.setCellStyle(whiteFG);
            }

            else if ( "".equals(cell.getStringCellValue()) ){       
                cell.setCellStyle(whiteFG);
            } 

                            else () {
                                 cell.setCellStyle(whiteFG);
                            }
HSSFWorkbook工作簿=新的HSSFWorkbook();
HSSFCellStyle whiteFG=工作簿.createCellStyle();
whiteFG.setFillForegroundColor(HSSFColor.AQUA.index);
whiteFG.setFillPattern(HSSFCellStyle.SOLID_前景);
//每行有20列
int numColumns=20;
对于(int colNum=0;colNum

关于如何着色空白单元格有什么建议吗?

< P>你的代码甚至不能编译。

但是您得到一个
NullPointerException
的原因是

if (cell != null){
   cell.setCellStyle(whiteFG);
}
else if ( "".equals(cell.getStringCellValue()) ){       
    cell.setCellStyle(whiteFG);
}
所有非空单元格都将进入第一个条件,因此唯一进入第二个条件的单元格是
null


*更新:回答评论*

我假设您想创建一个带有彩色单元格的新xls文件。但是,您的代码遗漏了一点-新创建的
工作簿
不包含任何工作表/行/单元格,您必须自己创建一个

这是我写的一个例子

HSSFWorkbook workBook = new HSSFWorkbook();
HSSFCellStyle style = workBook.createCellStyle();
style.setFillForegroundColor(HSSFColor.BROWN.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

HSSFSheet sheet = workBook.createSheet();
int numRow = 20;
int numCol = 20;

for (int rowIndex = 0; rowIndex < numRow; rowIndex++) {
    HSSFRow row = sheet.createRow(rowIndex);
    for (int colIndex = 0; colIndex < numCol; colIndex++) {
        HSSFCell cell = row.createCell(colIndex);
        cell.setCellStyle(brownBG);
    }
}

FileOutputStream fos = new FileOutputStream("test.xls");
workBook.write(fos);
fos.flush();
fos.close();
System.out.println("done");
HSSFWorkbook工作簿=新的HSSFWorkbook();
HSSFCellStyle=workBook.createCellStyle();
花柱:setFillForegroundColor(HSSFColor.BROWN.index);
style.setFillPattern(HSSFCellStyle.SOLID\u前景);
HSSFSheet sheet=workBook.createSheet();
int numRow=20;
int numCol=20;
对于(int-rowIndex=0;rowIndex

您编写的代码使用
getCell(index)
从行中检索单元格,此方法仅在您编辑新xls文件时返回
null

您的代码甚至无法编译

但是您得到一个
NullPointerException
的原因是

if (cell != null){
   cell.setCellStyle(whiteFG);
}
else if ( "".equals(cell.getStringCellValue()) ){       
    cell.setCellStyle(whiteFG);
}
所有非空单元格都将进入第一个条件,因此唯一进入第二个条件的单元格是
null


*更新:回答评论*

我假设您想创建一个带有彩色单元格的新xls文件。但是,您的代码遗漏了一点-新创建的
工作簿
不包含任何工作表/行/单元格,您必须自己创建一个

这是我写的一个例子

HSSFWorkbook workBook = new HSSFWorkbook();
HSSFCellStyle style = workBook.createCellStyle();
style.setFillForegroundColor(HSSFColor.BROWN.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

HSSFSheet sheet = workBook.createSheet();
int numRow = 20;
int numCol = 20;

for (int rowIndex = 0; rowIndex < numRow; rowIndex++) {
    HSSFRow row = sheet.createRow(rowIndex);
    for (int colIndex = 0; colIndex < numCol; colIndex++) {
        HSSFCell cell = row.createCell(colIndex);
        cell.setCellStyle(brownBG);
    }
}

FileOutputStream fos = new FileOutputStream("test.xls");
workBook.write(fos);
fos.flush();
fos.close();
System.out.println("done");
HSSFWorkbook工作簿=新的HSSFWorkbook();
HSSFCellStyle=workBook.createCellStyle();
花柱:setFillForegroundColor(HSSFColor.BROWN.index);
style.setFillPattern(HSSFCellStyle.SOLID\u前景);
HSSFSheet sheet=workBook.createSheet();
int numRow=20;
int numCol=20;
对于(int-rowIndex=0;rowIndex
您编写的代码使用
getCell(index)
从行中检索单元格,此方法仅在您编辑新xls文件时返回
null