Excel file created using apache poi (Java) cannot be opened on Windows

On my system, I have a class that creates excel with some data.

Basically, I read all String values ​​from the variable ArrayList> and write them to excel cells.

public void writeData(Data data, int sheetNumber) throws EncryptedDocumentException, InvalidFormatException, IOException { org.apache.poi.ss.usermodel.Workbook workbook; try { workbook = WorkbookFactory.create(new File(path)); } catch (FileNotFoundException e) { workbook = new HSSFWorkbook(); } org.apache.poi.ss.usermodel.Sheet sheet; try { sheet = workbook.createSheet("Sheet" + sheetNumber); } catch (IllegalArgumentException e) { sheet = workbook.getSheet("Sheet" + sheetNumber); } int dataListSize = data.getData().size(); for (int i = 0; i < dataListSize; i++) { Row row = sheet.createRow(i); int rowSize = data.getData().get(i).size(); for (int j = 0; j < rowSize; j++) { row.createCell(j); row.getCell(j).setCellValue(String.valueOf(data.getData().get(i).get(j))); } } FileOutputStream fos = null; try { fos = new FileOutputStream(new File(path)); workbook.write(fos); } catch (IOException e) { e.printStackTrace(); } finally { workbook.close(); if (fos != null) { try { fos.flush(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } 

The code works fine, as far as I know, I develop on Ubuntu and always try to use the code created by excels first - this is good and I have no problems at all.

When I take one of them in Windows (XP and 7, checked both), I can not open them from Microsoft Excel.

Does anyone have any experience?

Thanks.

+1
java windows excel apache-poi
source share
1 answer

As Axel noted, the problem was the file extension.

I can open files created this way in Ubuntu (both 14.04 and 16.04), but not on Windows (7, 8, and 10).

The solution is to use the .xls and NOT .xlsx extensions, so I can open and use files on any OS.

+1
source share

All Articles