• Post Reply Bookmark Topic Watch Topic
  • New Topic

Excel file cannot open the file '****.xlsx' or '****.xlsm'because the file format or ext. is not val

 
Ambi inout
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello There,

I am using apache poi and reading a .xltm template to write output to .xlsm. I am able to generate a .xls file but when i try to generate .xlsx or .xlsm excel, the file genrates,
but when i open the following message is displayed :
"excel cannot open the file 'test.xlsm' because the file format or file extension is not valid".

Any help. I am also setting the content type as below in my code :

try{
in = getClass().getResourceAsStream(template);
Constructor<ZipPackage> constructor;
constructor = ZipPackage.class.getDeclaredConstructor(Object.class);
constructor.setAccessible(true);
OPCPackage pack =constructor.newInstance(in,PackageAccess);
if (pack != null) {
pack.getParts();

// if (packagePart.getContentType().equals(oldContentType)) {
// PackagePartName partName = packagePart.getPartName();
pack.replaceContentType("application/vnd.ms-excel.sheet.macroEnabled.main+xml", "application/vnd.ms-excel");
//TODO save as file with the new file extention
}
} catch (InvocationTargetException x) {
x.printStackTrace();
} catch (NoSuchMethodException x) {
x.printStackTrace();
} catch (InstantiationException x) {
x.printStackTrace();
} catch (IllegalAccessException x) {
x.printStackTrace();
}
catch (InvalidFormatException x) {
x.printStackTrace();
}

The above snippet throws the below error (at statement constructor = ZipPackage.class.getDeclaredConstructor(Object.class);):

java.lang.NoSuchMethodException: org.apache.poi.openxml4j.opc.ZipPackage.<init>(java.lang.Object)
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.getDeclaredConstructor(Unknown Source)
at com.agr.great.offline.io.dataset.ExportToExcel.init(ExportToExcel.java:187)

Any suggestions will be really helpful.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!