ApachePOI4.0:XSSFColor来自java.awt.Color

ApachePOI4.0:XSSFColor来自java.awt.Color,java,colors,apache-poi,apache-poi-4,Java,Colors,Apache Poi,Apache Poi 4,org.apache.poi 4.0删除了仅使用java.awt.Color的XSSFColor构造函数。在org.apache.poi3.7中,只需编写 Color inputColor = Color.RED; XSSFColor test = new XSSFColor(inputColor); 但是,该构造函数在4.0中不再有效。中的文档显示了其他几个构造函数,但理想情况下,我希望更改尽可能少的行 所以,我的问题是,现在(在ApachePOI4.0中)从java.awt.Color创建

org.apache.poi 4.0
删除了仅使用
java.awt.Color
XSSFColor
构造函数。在
org.apache.poi3.7
中,只需编写

Color inputColor = Color.RED;
XSSFColor test = new XSSFColor(inputColor);
但是,该构造函数在4.0中不再有效。中的文档显示了其他几个构造函数,但理想情况下,我希望更改尽可能少的行

所以,我的问题是,现在(在ApachePOI4.0中)从
java.awt.Color
创建
XSSFColor
的最佳方法是什么


根据评论中的要求,下面是我使用建议
style.setFillForegroundColor(新XSSFColor(java.awt.Color.RED,null))的测试代码
使用libreoffice6.1打开此文件会产生错误(尝试修复,然后失败)。注释掉了正常工作的POI 3.7版本

@Test
public void testPOI40() throws FileNotFoundException, IOException {
    Workbook workbook = new XSSFWorkbook();
    XSSFSheet fSheet = (XSSFSheet) workbook.createSheet("new Sheet");
    XSSFRow hRow = fSheet.createRow((short) 0);
    //header
    String[] astrHeaders = new String[]{"Header1", "Header2", "Header3", "Header4"};
    for (int col = 0; col < astrHeaders.length; col++) {
        XSSFCell cell = hRow.createCell((short) col);
        XSSFCellStyle tempHeaderStyle = (XSSFCellStyle) workbook.createCellStyle();
        tempHeaderStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        tempHeaderStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cell.setCellValue(astrHeaders[col]);
        cell.setCellStyle(tempHeaderStyle);
    }        
    //body
    Double[] astrContent = new Double[]{1.3, 0.3, 0.87, 1.0};     
    Color[] colors = new Color[] {Color.RED,Color.BLUE,Color.WHITE,Color.GREEN};        
    XSSFRow fRow = fSheet.createRow((short) 1);
    for (int iCol = 0; iCol < 4; iCol++) {
        XSSFCell cell = fRow.createCell((short) iCol);
        XSSFCellStyle tempBodyStyle = (XSSFCellStyle) workbook.createCellStyle();
        cell.setCellValue(astrContent[iCol]);
        //working with POI 3.17
        //tempBodyStyle.setFillForegroundColor(new XSSFColor(colors[iCol]));
        tempBodyStyle.setFillForegroundColor(new XSSFColor(colors[iCol],null));
        tempBodyStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cell.setCellStyle(tempBodyStyle);
    }        
    FileOutputStream fileOut = new FileOutputStream(new File("testfile.xlsx"));
    BufferedOutputStream bos = new BufferedOutputStream(fileOut);
    workbook.write(bos);
    fileOut.close();
}
@测试
public void testPOI40()引发FileNotFoundException、IOException{
工作簿=新的XSSF工作簿();
XSSFSheet fSheet=(XSSFSheet)工作簿.createSheet(“新工作表”);
XSSFRow hRow=fSheet.createRow((短)0);
//标题
字符串[]asterheaders=新字符串[]{“Header1”、“Header2”、“Header3”、“Header4”};
for(int col=0;col<0.length;col++){
XSSFCell cell=hRow.createCell((短)列);
XSSFCellStyle tempHeaderStyle=(XSSFCellStyle)工作簿。createCellStyle();
tempHeaderStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
tempHeaderStyle.setFillPattern(FillPatternType.SOLID\u前景);
cell.setCellValue(单元格[col]);
cell.setCellStyle(tempHeaderStyle);
}        
//身体
Double[]astracontent=新的Double[]{1.3,0.3,0.87,1.0};
颜色[]颜色=新颜色[]{Color.RED,Color.BLUE,Color.WHITE,Color.GREEN};
XSSFRow fRow=fSheet.createRow((短)1);
对于(int iCol=0;iCol<4;iCol++){
XSSFCell cell=fRow.createCell((短)iCol);
XSSFCellStyle tempBodyStyle=(XSSFCellStyle)工作簿。createCellStyle();
cell.setCellValue(AstricContent[iCol]);
//使用POI 3.17
//setFillForegroundColor(新的XSSFColor(颜色[iCol]);
setFillForegroundColor(新的XSSFColor(颜色[iCol],null));
tempBodyStyle.setFillPattern(FillPatternType.SOLID\u前景);
cell.setCellStyle(tempBodyStyle);
}        
FileOutputStream fileOut=新的FileOutputStream(新文件(“testfile.xlsx”);
BufferedOutputStream bos=新的BufferedOutputStream(fileOut);
工作簿。编写(bos);
fileOut.close();
}
解决方案:

替换
fileout.close()带有
bos.close()并且它可以工作。所以
tempBodyStyle.setFillForegroundColor(新的XSSFColor(Color.RED,null))是一个很好的解决方案,我们会接受这个答案。

如果您将
文件输出流
包装在
缓冲输出流
中,但关闭后只关闭内部
文件输出流
,而不关闭
缓冲输出流
,然后,
BufferedOutputStream
保持打开状态,文件中不会包含所有字节

这就是为什么文件会损坏

因此,损坏与构造
XSSFColor
无关。构造函数
style.setFillForegroundColor(新的XSSFColor(java.awt.Color.RED,null))工作

改为:

...
FileOutputStream fileOut = new FileOutputStream(new File("testfile.xlsx"));
BufferedOutputStream bos = new BufferedOutputStream(fileOut);
workbook.write(bos); 
bos.close();
workbook.close();
...
它可能在以前的apache poi版本中工作,因为
xssf工作簿.write
在准备就绪时关闭了所有流。这并没有更多。这是正确的,因为
write
不应该关闭流


但是由于
poimmldocument
实现了
java.io.Closeable
至少
workbook.close()
应该关闭所有流。但事实并非如此。因此,在
apache poi 4.0.0
中明确关闭所有流是必要的,感谢您的回答,但是,第一个方法生成的表在尝试打开时报告为已损坏。第二个方法失败,因为
setColor(Color)在ExtendedColor
中具有受保护的访问权限。很抱歉
style.setFillForegroundColor(新的XSSFColor(java.awt.Color.RED,null))
适用于我使用ApachePOI4.0.0和从头创建XSSFWorkbook。请给出一个最小的完整的可验证的例子,说明它失败的地方。谢谢你,我现在在主帖子中添加了我的测试,以供你的建议。只是为了澄清,代码没有导致错误,但输出文件无法读取(但使用POI 3.7的版本,请参阅注释掉的部分,是可读的)。损坏与颜色无关。您正在将FileOutputStream包装到BufferedOutputStream中,但只关闭内部FileOutputStream,而不关闭BufferedOutputStream。做
工作簿。写(bos);bos.close()和它将工作。至少对我来说是这样。它可能在以前的
apachepoi
版本中工作,因为
Workbook.write
在准备就绪时关闭了所有流。这不多了。再次感谢。您可以添加行
tempBodyStyle.setFillForegroundColor(新XSSFColor(Color.RED,null))
对于那些只想用一行程序来取代旧方法的人(并且在我的示例中不感兴趣)的答案是什么?