【问题标题】:Convert .xltx to .xlsx将 .xltx 转换为 .xlsx
【发布时间】:2015-10-13 06:14:20
【问题描述】:

我正在阅读 .xltx,我想导出 .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");
}

当我导出时,.xlsx 在打开文件时总是给我一个错误,Excel 无法打开文件,因为格式或扩展名不正确。如果我将扩展名更改为 .xls,我可以打开它。

我正在使用 POI 3.12。

【问题讨论】:

    标签: java excel apache-poi


    【解决方案1】:
    workbook.setWorkbookType(XSSFWorkbookType.XLSX);
    

    【讨论】:

    • 考虑为你的答案添加解释,这会更有帮助
    • 欢迎来到 SO!当您回答一个问题时,请添加更多信息或解释您的答案,即使它是正确的。
    • 这从 POI 3.13 开始可用。需要,因为否则 excel 会抱怨内部具有类型模板的 xlsx 文件,如果您读取带有 POI 的 xltx 文件,更改一些单元格并写成 xlsx,就会发生这种情况。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-15
    • 2018-08-27
    • 2012-09-06
    • 2019-02-18
    • 1970-01-01
    相关资源
    最近更新 更多