码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-11-08 00:57:20      阅读:1058      评论:0      收藏:0      [点我收藏+]

标签:reserve   ror   filename   pap   xlsx   ons   file   jar   overflow   

write to xlsm (Excel 2007) using apache poi

 POI的下载(记得把其中的jar包全部加到工程里哦)http://mirror.bit.edu.cn/apache/poi/

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 = new XSSFWorkbook();
XSSFSheet sheet = workBook.createSheet("Related_SRC");
String realName = "Test.xlsm";
File file = new File("C:\\sdpApp", realName);
try {
    FileOutputStream fileOutput = new FileOutputStream(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 = new XSSFWorkbook(
        OPCPackage.open("resources/template_with_macro.xlsm")
    );

    //DO STUF WITH WORKBOOK

    FileOutputStream out = new FileOutputStream(new File(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

 

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

标签:reserve   ror   filename   pap   xlsx   ons   file   jar   overflow   

原文地址:http://www.cnblogs.com/lsgwr/p/6041013.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!