Apache POI 4.0: XSSFColor from java.awt.Color









up vote
2
down vote

favorite












org.apache.poi 4.0 removed the XSSFColor constructor that just uses java.awt.Color. In org.apache.poi 3.7 it was very easy to create the object by just writing



Color inputColor = Color.RED;
XSSFColor test = new XSSFColor(inputColor);


However, this constructor no longer works in 4.0. The documentation at https://poi.apache.org/apidocs/dev/org/apache/poi/xssf/usermodel/XSSFColor.html shows several other constructors, but ideally i want to change as few lines as possible.



So, my question is, what is the best way to create an XSSFColor from a java.awt.Color now (in apache poi 4.0)?




As requested in the comments, here is my test code using the suggestion style.setFillForegroundColor(new XSSFColor(java.awt.Color.RED, null));
Opening this with LibreOffice 6.1 yields an Error (Attempt to repair, which then fails). Commented out the POI 3.7 version which works normally.



@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 Double1.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();



Solution:

Replaced fileout.close(); with bos.close(); and it works. So tempBodyStyle.setFillForegroundColor(new XSSFColor(Color.RED,null)); as suggested in the comments by Alex Richter is a good solution & will accept this as answer.










share|improve this question























  • Thanks for answering, however the table resulting from the first method is reported as damaged when trying to open it. The second method fails because setColor(Color) has protected access in ExtendedColor.
    – ptstone
    Nov 9 at 14:56







  • 2




    Sorry but style.setFillForegroundColor(new XSSFColor(java.awt.Color.RED, null)); works for me using apache poi 4.0.0 and creating XSSFWorkbook from scratch. Please show minimal complete verifiable example which shows where it fails.
    – Axel Richter
    Nov 9 at 15:31











  • Thank you, I added my test for your suggestion to the main post now.
    – ptstone
    Nov 9 at 16:21










  • Just to clarify, the code doesn't cause an error, but the output file cannot be read (but the version using POI 3.7, see commented out section, is readable)
    – ptstone
    Nov 9 at 16:35






  • 2




    The damaging has nothing to do with the colors. You are wrapping the FileOutputStream in an BufferedOutputStream but do closing then only the inner FileOutputStream but not the BufferedOutputStream. Do workbook.write(bos); bos.close(); and it will work. At least it works for me then. It might had worked in former apache poi versions because Workbook.write had closed all streams when it was ready. This it does not more.
    – Axel Richter
    Nov 9 at 16:36















up vote
2
down vote

favorite












org.apache.poi 4.0 removed the XSSFColor constructor that just uses java.awt.Color. In org.apache.poi 3.7 it was very easy to create the object by just writing



Color inputColor = Color.RED;
XSSFColor test = new XSSFColor(inputColor);


However, this constructor no longer works in 4.0. The documentation at https://poi.apache.org/apidocs/dev/org/apache/poi/xssf/usermodel/XSSFColor.html shows several other constructors, but ideally i want to change as few lines as possible.



So, my question is, what is the best way to create an XSSFColor from a java.awt.Color now (in apache poi 4.0)?




As requested in the comments, here is my test code using the suggestion style.setFillForegroundColor(new XSSFColor(java.awt.Color.RED, null));
Opening this with LibreOffice 6.1 yields an Error (Attempt to repair, which then fails). Commented out the POI 3.7 version which works normally.



@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 Double1.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();



Solution:

Replaced fileout.close(); with bos.close(); and it works. So tempBodyStyle.setFillForegroundColor(new XSSFColor(Color.RED,null)); as suggested in the comments by Alex Richter is a good solution & will accept this as answer.










share|improve this question























  • Thanks for answering, however the table resulting from the first method is reported as damaged when trying to open it. The second method fails because setColor(Color) has protected access in ExtendedColor.
    – ptstone
    Nov 9 at 14:56







  • 2




    Sorry but style.setFillForegroundColor(new XSSFColor(java.awt.Color.RED, null)); works for me using apache poi 4.0.0 and creating XSSFWorkbook from scratch. Please show minimal complete verifiable example which shows where it fails.
    – Axel Richter
    Nov 9 at 15:31











  • Thank you, I added my test for your suggestion to the main post now.
    – ptstone
    Nov 9 at 16:21










  • Just to clarify, the code doesn't cause an error, but the output file cannot be read (but the version using POI 3.7, see commented out section, is readable)
    – ptstone
    Nov 9 at 16:35






  • 2




    The damaging has nothing to do with the colors. You are wrapping the FileOutputStream in an BufferedOutputStream but do closing then only the inner FileOutputStream but not the BufferedOutputStream. Do workbook.write(bos); bos.close(); and it will work. At least it works for me then. It might had worked in former apache poi versions because Workbook.write had closed all streams when it was ready. This it does not more.
    – Axel Richter
    Nov 9 at 16:36













up vote
2
down vote

favorite









up vote
2
down vote

favorite











org.apache.poi 4.0 removed the XSSFColor constructor that just uses java.awt.Color. In org.apache.poi 3.7 it was very easy to create the object by just writing



Color inputColor = Color.RED;
XSSFColor test = new XSSFColor(inputColor);


However, this constructor no longer works in 4.0. The documentation at https://poi.apache.org/apidocs/dev/org/apache/poi/xssf/usermodel/XSSFColor.html shows several other constructors, but ideally i want to change as few lines as possible.



So, my question is, what is the best way to create an XSSFColor from a java.awt.Color now (in apache poi 4.0)?




As requested in the comments, here is my test code using the suggestion style.setFillForegroundColor(new XSSFColor(java.awt.Color.RED, null));
Opening this with LibreOffice 6.1 yields an Error (Attempt to repair, which then fails). Commented out the POI 3.7 version which works normally.



@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 Double1.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();



Solution:

Replaced fileout.close(); with bos.close(); and it works. So tempBodyStyle.setFillForegroundColor(new XSSFColor(Color.RED,null)); as suggested in the comments by Alex Richter is a good solution & will accept this as answer.










share|improve this question















org.apache.poi 4.0 removed the XSSFColor constructor that just uses java.awt.Color. In org.apache.poi 3.7 it was very easy to create the object by just writing



Color inputColor = Color.RED;
XSSFColor test = new XSSFColor(inputColor);


However, this constructor no longer works in 4.0. The documentation at https://poi.apache.org/apidocs/dev/org/apache/poi/xssf/usermodel/XSSFColor.html shows several other constructors, but ideally i want to change as few lines as possible.



So, my question is, what is the best way to create an XSSFColor from a java.awt.Color now (in apache poi 4.0)?




As requested in the comments, here is my test code using the suggestion style.setFillForegroundColor(new XSSFColor(java.awt.Color.RED, null));
Opening this with LibreOffice 6.1 yields an Error (Attempt to repair, which then fails). Commented out the POI 3.7 version which works normally.



@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 Double1.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();



Solution:

Replaced fileout.close(); with bos.close(); and it works. So tempBodyStyle.setFillForegroundColor(new XSSFColor(Color.RED,null)); as suggested in the comments by Alex Richter is a good solution & will accept this as answer.







java colors apache-poi apache-poi-4






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 16:53

























asked Nov 9 at 14:00









ptstone

11410




11410











  • Thanks for answering, however the table resulting from the first method is reported as damaged when trying to open it. The second method fails because setColor(Color) has protected access in ExtendedColor.
    – ptstone
    Nov 9 at 14:56







  • 2




    Sorry but style.setFillForegroundColor(new XSSFColor(java.awt.Color.RED, null)); works for me using apache poi 4.0.0 and creating XSSFWorkbook from scratch. Please show minimal complete verifiable example which shows where it fails.
    – Axel Richter
    Nov 9 at 15:31











  • Thank you, I added my test for your suggestion to the main post now.
    – ptstone
    Nov 9 at 16:21










  • Just to clarify, the code doesn't cause an error, but the output file cannot be read (but the version using POI 3.7, see commented out section, is readable)
    – ptstone
    Nov 9 at 16:35






  • 2




    The damaging has nothing to do with the colors. You are wrapping the FileOutputStream in an BufferedOutputStream but do closing then only the inner FileOutputStream but not the BufferedOutputStream. Do workbook.write(bos); bos.close(); and it will work. At least it works for me then. It might had worked in former apache poi versions because Workbook.write had closed all streams when it was ready. This it does not more.
    – Axel Richter
    Nov 9 at 16:36

















  • Thanks for answering, however the table resulting from the first method is reported as damaged when trying to open it. The second method fails because setColor(Color) has protected access in ExtendedColor.
    – ptstone
    Nov 9 at 14:56







  • 2




    Sorry but style.setFillForegroundColor(new XSSFColor(java.awt.Color.RED, null)); works for me using apache poi 4.0.0 and creating XSSFWorkbook from scratch. Please show minimal complete verifiable example which shows where it fails.
    – Axel Richter
    Nov 9 at 15:31











  • Thank you, I added my test for your suggestion to the main post now.
    – ptstone
    Nov 9 at 16:21










  • Just to clarify, the code doesn't cause an error, but the output file cannot be read (but the version using POI 3.7, see commented out section, is readable)
    – ptstone
    Nov 9 at 16:35






  • 2




    The damaging has nothing to do with the colors. You are wrapping the FileOutputStream in an BufferedOutputStream but do closing then only the inner FileOutputStream but not the BufferedOutputStream. Do workbook.write(bos); bos.close(); and it will work. At least it works for me then. It might had worked in former apache poi versions because Workbook.write had closed all streams when it was ready. This it does not more.
    – Axel Richter
    Nov 9 at 16:36
















Thanks for answering, however the table resulting from the first method is reported as damaged when trying to open it. The second method fails because setColor(Color) has protected access in ExtendedColor.
– ptstone
Nov 9 at 14:56





Thanks for answering, however the table resulting from the first method is reported as damaged when trying to open it. The second method fails because setColor(Color) has protected access in ExtendedColor.
– ptstone
Nov 9 at 14:56





2




2




Sorry but style.setFillForegroundColor(new XSSFColor(java.awt.Color.RED, null)); works for me using apache poi 4.0.0 and creating XSSFWorkbook from scratch. Please show minimal complete verifiable example which shows where it fails.
– Axel Richter
Nov 9 at 15:31





Sorry but style.setFillForegroundColor(new XSSFColor(java.awt.Color.RED, null)); works for me using apache poi 4.0.0 and creating XSSFWorkbook from scratch. Please show minimal complete verifiable example which shows where it fails.
– Axel Richter
Nov 9 at 15:31













Thank you, I added my test for your suggestion to the main post now.
– ptstone
Nov 9 at 16:21




Thank you, I added my test for your suggestion to the main post now.
– ptstone
Nov 9 at 16:21












Just to clarify, the code doesn't cause an error, but the output file cannot be read (but the version using POI 3.7, see commented out section, is readable)
– ptstone
Nov 9 at 16:35




Just to clarify, the code doesn't cause an error, but the output file cannot be read (but the version using POI 3.7, see commented out section, is readable)
– ptstone
Nov 9 at 16:35




2




2




The damaging has nothing to do with the colors. You are wrapping the FileOutputStream in an BufferedOutputStream but do closing then only the inner FileOutputStream but not the BufferedOutputStream. Do workbook.write(bos); bos.close(); and it will work. At least it works for me then. It might had worked in former apache poi versions because Workbook.write had closed all streams when it was ready. This it does not more.
– Axel Richter
Nov 9 at 16:36





The damaging has nothing to do with the colors. You are wrapping the FileOutputStream in an BufferedOutputStream but do closing then only the inner FileOutputStream but not the BufferedOutputStream. Do workbook.write(bos); bos.close(); and it will work. At least it works for me then. It might had worked in former apache poi versions because Workbook.write had closed all streams when it was ready. This it does not more.
– Axel Richter
Nov 9 at 16:36













1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










If you are wrapping the FileOutputStream in an BufferedOutputStream but do closing then only the inner FileOutputStream but not the BufferedOutputStream, then the BufferedOutputStream remains open and the file will not have all bytes in.



That's why the damaging of the file.



So the damaging has noting to do with constructing the XSSFColor. The constructor style.setFillForegroundColor(new XSSFColor(java.awt.Color.RED, null)); works.



Do instead:



...
FileOutputStream fileOut = new FileOutputStream(new File("testfile.xlsx"));
BufferedOutputStream bos = new BufferedOutputStream(fileOut);
workbook.write(bos);
bos.close();
workbook.close();
...


It might had worked in former apache poi versions because XSSFWorkbook.write had closed all streams when it was ready. This it does not more. And this is correct because write should not closing streams.



But since POIXMLDocument implements java.io.Closeable at least workbook.close() should closing all streams. But that also it does not. So explicitly closing all streams is necessary in apache poi 4.0.0.






share|improve this answer






















  • Thanks again. Could you maybeadd the line tempBodyStyle.setFillForegroundColor(new XSSFColor(Color.RED,null)); in the answer for people who just look for a oneliner to replace the old method (and are not intersted in my example)?
    – ptstone
    Nov 9 at 17:01











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53227136%2fapache-poi-4-0-xssfcolor-from-java-awt-color%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










If you are wrapping the FileOutputStream in an BufferedOutputStream but do closing then only the inner FileOutputStream but not the BufferedOutputStream, then the BufferedOutputStream remains open and the file will not have all bytes in.



That's why the damaging of the file.



So the damaging has noting to do with constructing the XSSFColor. The constructor style.setFillForegroundColor(new XSSFColor(java.awt.Color.RED, null)); works.



Do instead:



...
FileOutputStream fileOut = new FileOutputStream(new File("testfile.xlsx"));
BufferedOutputStream bos = new BufferedOutputStream(fileOut);
workbook.write(bos);
bos.close();
workbook.close();
...


It might had worked in former apache poi versions because XSSFWorkbook.write had closed all streams when it was ready. This it does not more. And this is correct because write should not closing streams.



But since POIXMLDocument implements java.io.Closeable at least workbook.close() should closing all streams. But that also it does not. So explicitly closing all streams is necessary in apache poi 4.0.0.






share|improve this answer






















  • Thanks again. Could you maybeadd the line tempBodyStyle.setFillForegroundColor(new XSSFColor(Color.RED,null)); in the answer for people who just look for a oneliner to replace the old method (and are not intersted in my example)?
    – ptstone
    Nov 9 at 17:01















up vote
2
down vote



accepted










If you are wrapping the FileOutputStream in an BufferedOutputStream but do closing then only the inner FileOutputStream but not the BufferedOutputStream, then the BufferedOutputStream remains open and the file will not have all bytes in.



That's why the damaging of the file.



So the damaging has noting to do with constructing the XSSFColor. The constructor style.setFillForegroundColor(new XSSFColor(java.awt.Color.RED, null)); works.



Do instead:



...
FileOutputStream fileOut = new FileOutputStream(new File("testfile.xlsx"));
BufferedOutputStream bos = new BufferedOutputStream(fileOut);
workbook.write(bos);
bos.close();
workbook.close();
...


It might had worked in former apache poi versions because XSSFWorkbook.write had closed all streams when it was ready. This it does not more. And this is correct because write should not closing streams.



But since POIXMLDocument implements java.io.Closeable at least workbook.close() should closing all streams. But that also it does not. So explicitly closing all streams is necessary in apache poi 4.0.0.






share|improve this answer






















  • Thanks again. Could you maybeadd the line tempBodyStyle.setFillForegroundColor(new XSSFColor(Color.RED,null)); in the answer for people who just look for a oneliner to replace the old method (and are not intersted in my example)?
    – ptstone
    Nov 9 at 17:01













up vote
2
down vote



accepted







up vote
2
down vote



accepted






If you are wrapping the FileOutputStream in an BufferedOutputStream but do closing then only the inner FileOutputStream but not the BufferedOutputStream, then the BufferedOutputStream remains open and the file will not have all bytes in.



That's why the damaging of the file.



So the damaging has noting to do with constructing the XSSFColor. The constructor style.setFillForegroundColor(new XSSFColor(java.awt.Color.RED, null)); works.



Do instead:



...
FileOutputStream fileOut = new FileOutputStream(new File("testfile.xlsx"));
BufferedOutputStream bos = new BufferedOutputStream(fileOut);
workbook.write(bos);
bos.close();
workbook.close();
...


It might had worked in former apache poi versions because XSSFWorkbook.write had closed all streams when it was ready. This it does not more. And this is correct because write should not closing streams.



But since POIXMLDocument implements java.io.Closeable at least workbook.close() should closing all streams. But that also it does not. So explicitly closing all streams is necessary in apache poi 4.0.0.






share|improve this answer














If you are wrapping the FileOutputStream in an BufferedOutputStream but do closing then only the inner FileOutputStream but not the BufferedOutputStream, then the BufferedOutputStream remains open and the file will not have all bytes in.



That's why the damaging of the file.



So the damaging has noting to do with constructing the XSSFColor. The constructor style.setFillForegroundColor(new XSSFColor(java.awt.Color.RED, null)); works.



Do instead:



...
FileOutputStream fileOut = new FileOutputStream(new File("testfile.xlsx"));
BufferedOutputStream bos = new BufferedOutputStream(fileOut);
workbook.write(bos);
bos.close();
workbook.close();
...


It might had worked in former apache poi versions because XSSFWorkbook.write had closed all streams when it was ready. This it does not more. And this is correct because write should not closing streams.



But since POIXMLDocument implements java.io.Closeable at least workbook.close() should closing all streams. But that also it does not. So explicitly closing all streams is necessary in apache poi 4.0.0.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 9 at 17:08

























answered Nov 9 at 16:56









Axel Richter

23.6k21733




23.6k21733











  • Thanks again. Could you maybeadd the line tempBodyStyle.setFillForegroundColor(new XSSFColor(Color.RED,null)); in the answer for people who just look for a oneliner to replace the old method (and are not intersted in my example)?
    – ptstone
    Nov 9 at 17:01

















  • Thanks again. Could you maybeadd the line tempBodyStyle.setFillForegroundColor(new XSSFColor(Color.RED,null)); in the answer for people who just look for a oneliner to replace the old method (and are not intersted in my example)?
    – ptstone
    Nov 9 at 17:01
















Thanks again. Could you maybeadd the line tempBodyStyle.setFillForegroundColor(new XSSFColor(Color.RED,null)); in the answer for people who just look for a oneliner to replace the old method (and are not intersted in my example)?
– ptstone
Nov 9 at 17:01





Thanks again. Could you maybeadd the line tempBodyStyle.setFillForegroundColor(new XSSFColor(Color.RED,null)); in the answer for people who just look for a oneliner to replace the old method (and are not intersted in my example)?
– ptstone
Nov 9 at 17:01


















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53227136%2fapache-poi-4-0-xssfcolor-from-java-awt-color%23new-answer', 'question_page');

);

Post as a guest














































































Popular posts from this blog

Kleinkühnau

Makov (Slowakei)

Deutsches Schauspielhaus