How can I convert POI HSSFWorkbook to bytes?

后端 未结 2 1035
旧巷少年郎
旧巷少年郎 2020-12-13 23:54

Calling Simple toBytes() does produce the bytes but exel throws Warning.

Lost Document information

Googling around gave me this

2条回答
  •  时光说笑
    2020-12-14 00:58

    As that mailing list post said

    Invoking HSSFWorkbook.getBytes() does not return all of the data necessary to re- construct a complete Excel file.

    You can use the write method with a ByteArrayOutputStream to get at the byte array.

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
        workbook.write(bos);
    } finally {
        bos.close();
    }
    byte[] bytes = bos.toByteArray();
    

    (The close call is not really needed for a ByteArrayOutputStream, but imho it is good style to include anyway in case its later changed to a different kind of stream.)

提交回复
热议问题