使用Apache POI将XLTM转换为XLSX时出错

时间:2015-03-23 10:49:08

标签: excel apache-poi spring-batch

我想将Excel XLTM文件转换为XLSX格式,作为Spring-Batch步骤的一部分。我使用以下代码转换文件:

def convertXltmToXlsx(String oldFilename, String newFileName) {
    OPCPackage pkg = OPCPackage.open(new FileInputStream(oldFilename));
    pkg.replaceContentType(
            "application/vnd.ms-excel.template.macroEnabled.main+xml",
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml");

    FileOutputStream out = new FileOutputStream(newFileName);
    pkg.save(out);
    out.close();
}

但是,当我尝试用Excel打开文件(converted.xlsx)时,收到以下消息:

The file 'converted.xlsx' is a macro-free file, but contains macro-enabled content.

当我通过这样做修改代码以打印旧文件和新文件的内容类型时:

def parts = pkg.getParts()
parts.each { part ->
    println "Part type: ${part.getContentType()}"
}

// do the conversion as before

OPCPackage newPkg = OPCPackage.open(new FileInputStream(newFileName));
parts = newPkg.getParts()
parts.each { part ->
    println "New part type: ${part.getContentType()}"
}

然后我得到以下内容:

Part type: application/vnd.openxmlformats-package.relationships+xml
Part type: application/vnd.openxmlformats-package.relationships+xml
Part type: application/vnd.openxmlformats-package.relationships+xml
Part type: application/vnd.openxmlformats-package.relationships+xml
Part type: application/xml
Part type: application/xml
Part type: application/xml
Part type: application/vnd.openxmlformats-officedocument.customXmlProperties+xml
Part type: application/vnd.openxmlformats-officedocument.customXmlProperties+xml
Part type: application/vnd.openxmlformats-officedocument.customXmlProperties+xml
Part type: application/vnd.openxmlformats-officedocument.extended-properties+xml
Part type: application/vnd.openxmlformats-package.core-properties+xml
Part type: application/vnd.openxmlformats-officedocument.custom-properties+xml
Part type: application/vnd.openxmlformats-package.relationships+xml
Part type: application/vnd.openxmlformats-officedocument.spreadsheetml.calcChain+xml
Part type: application/vnd.ms-excel.controlproperties+xml
Part type: application/vnd.ms-excel.controlproperties+xml
Part type: application/vnd.ms-excel.controlproperties+xml
Part type: application/vnd.openxmlformats-officedocument.drawing+xml
Part type: application/vnd.openxmlformats-officedocument.vmlDrawing
Part type: application/vnd.openxmlformats-officedocument.spreadsheetml.printerSettings
Part type: application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml
Part type: application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml
Part type: application/vnd.openxmlformats-officedocument.theme+xml
Part type: application/vnd.ms-office.vbaProject
Part type: application/vnd.ms-excel.template.macroEnabled.main+xml
Part type: application/vnd.openxmlformats-package.relationships+xml
Part type: application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml
Part type: application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml
Part type: application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml
Part type: application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml
Part type: application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml

New part type: application/vnd.openxmlformats-package.relationships+xml
New part type: application/vnd.openxmlformats-package.relationships+xml
New part type: application/vnd.openxmlformats-package.relationships+xml
New part type: application/vnd.openxmlformats-package.relationships+xml
New part type: application/xml
New part type: application/xml
New part type: application/xml
New part type: application/vnd.openxmlformats-officedocument.customXmlProperties+xml
New part type: application/vnd.openxmlformats-officedocument.customXmlProperties+xml
New part type: application/vnd.openxmlformats-officedocument.customXmlProperties+xml
New part type: application/vnd.openxmlformats-officedocument.extended-properties+xml
New part type: application/vnd.openxmlformats-package.core-properties+xml
New part type: application/vnd.openxmlformats-officedocument.custom-properties+xml
New part type: application/vnd.openxmlformats-package.relationships+xml
New part type: application/vnd.openxmlformats-officedocument.spreadsheetml.calcChain+xml
New part type: application/vnd.ms-excel.controlproperties+xml
New part type: application/vnd.ms-excel.controlproperties+xml
New part type: application/vnd.ms-excel.controlproperties+xml
New part type: application/vnd.openxmlformats-officedocument.drawing+xml
New part type: application/vnd.openxmlformats-officedocument.vmlDrawing
New part type: application/vnd.openxmlformats-officedocument.spreadsheetml.printerSettings
New part type: application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml
New part type: application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml
New part type: application/vnd.openxmlformats-officedocument.theme+xml
New part type: application/vnd.ms-office.vbaProject
New part type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml
New part type: application/vnd.openxmlformats-package.relationships+xml
New part type: application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml
New part type: application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml
New part type: application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml
New part type: application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml
New part type: application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml

如何将XLTM转换为XLSX?

0 个答案:

没有答案
相关问题