Apache poi "формат или расширение являются файла недопустимыми"
Генерирую xlsx с помощью apache poi, использую последнюю версию
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
public String generateXlsx(List<Section> sections, int idJob) throws IOException {
HSSFWorkbook ExcelBook = new HSSFWorkbook();
filename = "filestorage/workbook"+idJob+".xlsx";
File file = new File(filename);
file.getParentFile().mkdirs();
file.createNewFile();
FileOutputStream fileOut = new FileOutputStream(file);
HSSFSheet sheet = ExcelBook.createSheet();
HSSFRow row = sheet.createRow((short)0);
HSSFCell cell = row.createCell(0);
cell.setCellValue("Section Name");
for(int i = 1; i <= getMaxLength(sections)*2; i++)
row.createCell(i)
.setCellValue( i % 2 == 1 ?
"class " + i % 2 + i / 2 + " name" :
"class " + i % 2 + i / 2 + " code");
short rowNum = 1;
for (Section section : sections) {
row = sheet.createRow(rowNum++);
cell = row.createCell(0);
cell.setCellValue(section.getName());
int cellNum = 1;
for (GeologicalClass geologicalClass : section.getGeologicalClasses()) {
cell = row.createCell(cellNum++);
cell.setCellValue(geologicalClass.getName());
cell = row.createCell(cellNum++);
cell.setCellValue(geologicalClass.getCode());
}
}
ExcelBook.write(fileOut);
fileOut.close();
return filename;
}
Файл создается, что то в него пишется, размер ненулевой. При попытке открыть в Excel 2013 пишет неверный формат или файл поврежден. В чем загвоздка?
Ответы (1 шт):
Автор решения: Anton Charov
→ Ссылка
Для поддержки xlsx формата Excel 2007+ нужно добавить библиотеку
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
Соответственно генерация файла
public String generateXlsx(List<Section> sections, int idJob) throws IOException {
Workbook ExcelBook = new XSSFWorkbook();
Sheet sheet = ExcelBook.createSheet();
Row row = sheet.createRow((short)0);
Cell cell = row.createCell(0);
Font font = ExcelBook.createFont();
font.setBold(true);
CellStyle style = ExcelBook.createCellStyle();
style.setFont(font);
style.setWrapText(true);
style.setAlignment(HorizontalAlignment.CENTER);
cell.setCellStyle(style);
cell.setCellValue("Section Name");
int columnNum = getMaxLength(sections)*2;
for(int i = 1; i <= columnNum; i++) {
cell = row.createCell(i);
cell.setCellValue(i % 2 == 1 ?
"class " + i % 2 + i / 2 + " name" :
"class " + i % 2 + i / 2 + " code");
cell.setCellStyle(style);
}
short rowNum = 1;
for (Section section : sections) {
row = sheet.createRow(rowNum++);
cell = row.createCell(0);
cell.setCellValue(section.getName());
int cellNum = 1;
for (GeologicalClass geologicalClass : section.getGeologicalClasses()) {
cell = row.createCell(cellNum++);
cell.setCellValue(geologicalClass.getName());
cell = row.createCell(cellNum++);
cell.setCellValue(geologicalClass.getCode());
}
}
for(int i = 0; i <= columnNum; i++){
sheet.autoSizeColumn(i);
}
PropertyTemplate propertyTemplate=new PropertyTemplate();
propertyTemplate.drawBorders(new CellRangeAddress(0,0,0,columnNum),
BorderStyle.MEDIUM,BorderExtent.ALL);
propertyTemplate.drawBorders(new CellRangeAddress(0,sections.size(),0,0),
BorderStyle.MEDIUM,BorderExtent.ALL);
propertyTemplate.drawBorders(new CellRangeAddress(0,sections.size(),0,columnNum),
BorderStyle.MEDIUM,BorderExtent.BOTTOM);
propertyTemplate.applyBorders(sheet);
filename = "filestorage/workbook"+idJob+".xlsx";
File file = new File(filename);
file.getParentFile().mkdirs();
file.createNewFile();
FileOutputStream fileOut = new FileOutputStream(file);
ExcelBook.write(fileOut);
fileOut.close();
ExcelBook.close();
return filename;
}