3

I am reading .xltx and I want to export .xlsx.

protected static final String ruta = ResourcesPathEnum.ROOT.getId() + "plantillas" + File.separator;    
public static final String CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

protected abstract Workbook xlsx() throws FileNotFoundException, IOException;   
/**
 * Genera excel
 * 
 * @return  InputStream
 */
public InputStream getFile() {
    try { 
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            xlsx().write(bos);
        } finally {
            bos.close();
        }
        return new ByteArrayInputStream(bos.toByteArray());
    } catch (FileNotFoundException e) {
        Logger.error(e.getMessage());
    } catch (IOException e) {
        Logger.error(e.getMessage());
    }
    return null;
}

public StreamedContent getFile() {
    ControllerReportSuceso aux = new ControllerReportSuceso(suceso, sucesosAvionWrapper);
    return new DefaultStreamedContent(aux.getFile(), ControllerReportSuceso.CONTENT_TYPE, "parte suceso: " + suceso.getEntidad().getNumeroOrden() + ".xlsx");
}

When I export, .xlsx always give me a error when open the file, that Excel cannot open file because format or extension is not correct. If I change extension to .xls I can open it.

I am using POI 3.12.

pnuts
  • 58,317
  • 11
  • 87
  • 139
Mathew Rock
  • 989
  • 2
  • 14
  • 32

1 Answers1

1
workbook.setWorkbookType(XSSFWorkbookType.XLSX);
Reznik
  • 2,663
  • 1
  • 11
  • 31
imletterh
  • 11
  • 1
  • 1
    Consider add explnation about your answer, it will be much more helpful – Reznik Oct 21 '19 at 09:36
  • 1
    Welcome to SO! When you answer one question, please, add some more information or explain your answer, even if it is right. – David García Bodego Oct 21 '19 at 09:53
  • This is available starting with POI 3.13. Needed because else excel will complain about xlsx files that have type template internally, which happens if you read an xltx file with POI, change some cells and write out as xlsx. – Manuel Feb 04 '21 at 14:45