Bug 51780

Summary: [PATCH] Feature Request: How to convert a dotx file to docx via poi?
Product: POI Reporter: Sebastian K. <seba>
Component: POI OverallAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: normal CC: seba
Priority: P2    
Version: 3.8-dev   
Target Milestone: ---   
Hardware: PC   
OS: All   
Attachments: With this patch I am able to change the mimetype.

Description Sebastian K. 2011-09-07 09:37:08 UTC
Created attachment 27465 [details]
With this patch I am able to change the mimetype.

I can't found a solution with the unmodified poi code for the following problem.

It should be possible to convert the following templates:

".dotx" to ".docx"
".dotm" to ".docm"
".xltx" to "xlsx"
".xltm" to ".xlsm"
".potx" to ".pptx"
".potm" to ".pptm"


I am able to do this after installing the attached patch.

Usage:
OPCPackage pack = new ZipPackage(stream, PackageAccess.READ_WRITE);
if (pack != null) {
  pack.getParts();
  ArrayList<PackagePart> list = pack.getPartsByContentType(oldContentType);
  for (PackagePart packagePart : list) {
    if (packagePart.getContentType().equals(oldContentType)) {
      PackagePartName partName = packagePart.getPartName();
      pack.getContenTypeManager().addContentType(partName, newContentType);
    }
  }
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream(1024);
  pack.save(outputStream);
  //TODO save as file with the new file extention
}

Please include this patch for the next version or implement an other nicer way to change the contenttype.
Comment 1 Yegor Kozlov 2012-02-29 07:55:06 UTC
Replacement of content types supported in r1294998. 

There is a new method  OPCPackage.replaceContentType(String oldType, String newType) that does the job. 

Usage:
 
OPCPackage pkg = OPCPackage.open(new FileInputStream("macro-workbook.xlsm"));
pkg.replaceContentType(
  "application/vnd.ms-excel.sheet.macroEnabled.main+xml",
  "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml");


  FileOutputStream out = new FileOutputStream("workbook.xlsx");
  pkg.save(out);
  out.close();

Yegor