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

我想将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. 

当我修改代码来打印旧文件和新文件的内容types时,请执行以下操作:

 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?