Excel breaks up when you open an Excel worksheet generated by an HSSFWorkbook that was attached to an email

I created an Excel spreadsheet using the HSSFWorkbook, and then created a custom javax.activation.DataSource file to attach the spreadsheet to the email. However, when you open a spreadsheet in Excel, it crashes. Excel may recover some data, but it will lose most of its formatting.

+3
source share
2 answers

When returning an InputStream in a DataSource implementation, make sure that you are not using HSSWorkbook.getBytes (), as this will only return a specific part of the table (which cannot be used by itself), and not the entire file. Use the write () method instead of ByteArrayOuputStream. For instance:

public InputStream getInputStream() throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    workbook.write(baos);
    return new ByteArrayInputStream(baos.toByteArray());
}

Just be careful about the size of the spreadsheet as this happens in memory. Consider storing a spreadsheet instead and adding it using a regular FileDataSource file.

HSSFWorkbook.getBytes ()

+8
source

I think the problem is in a POI that does not generate an excel readable file.

0
source

Source: https://habr.com/ru/post/1730079/


All Articles