Search code examples
javaxssf

Can i generate a xlsx file with the Writer class?


Is there a way to generate a xlsx file using the Writer class in java?

The method i use to generate a workbook converted to a byte array:

private byte[] generateXlsxBytes() {
    XSSFWorkbook workbook = new XSSFWorkbook();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    workbook.write(baos);
    baos.close();
    return baos.toByteArray();
}

I try to write the bytes into the writer by doing this:

Writer writer = ..get the writer..
IOUtils.write(generateXlsBytes(), writer);

But when i try to open the file in excel i get a message saying that the markup of the file is not correct for the extension(xlsx).

I work with a custom made framework which won't allow me to use a FileOutputStream to write directly to a file. I would really appreciate it if someone could tell me a way to use the Writer.


Solution

  • @A4L thank you for explaining it to me.

    Writers are just not meant for writing raw bytes.

    I just asked one of my coworkers to make a change in the framework, so that i could use an OutputStream