java xlsm_Java操作Excel: POI不能创建xlsm问题的方法(源自StackOverFlow)

I have written java file for writing xlsm(Excel 2007).

Using Apache POI Library, Writing xlsx file is success. And Writing xlsm file is success. But I can‘t open the xlsm file because of error when open xlsm file.

Would it feasible to write xlsm file using Apache POI Library?

If it is feasible to write xlsm, Please kindly provide guide line How to write xlsm file using Apache poi library.

XSSFWorkbook workBook = newXSSFWorkbook();

XSSFSheet sheet= workBook.createSheet("Related_SRC");

String realName= "Test.xlsm";

File file= new File("C:\\sdpApp", realName);try{

FileOutputStream fileOutput= newFileOutputStream(file);

workBook.write(fileOutput);

fileOutput.close();

}catch(Exception e) {//TODO: handle exception

e.printStackTrace();

}

Thanks

According to the documentation of Apache POI it is not possible to create macros:http://poi.apache.org/spreadsheet/limitations.html

However it‘s possible to read and re-write files containing macros and apache poi will safely preserve the macros.

Here is an example:

String fileName = "C:\\new_file.xlsm";try{

Workbook workbook;

workbook= newXSSFWorkbook(

OPCPackage.open("resources/template_with_macro.xlsm")

);//DO STUF WITH WORKBOOK

FileOutputStream out= new FileOutputStream(newFile(fileName));

workbook.write(out);

out.close();

System.out.println("xlsm created successfully..");

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(InvalidFormatException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}

The file that is created will not give you an error

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值