本文介绍了使用 apache poi 写入 xlsm (Excel 2007)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了用于编写 xlsm(Excel 2007)的 java 文件.

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

使用Apache POI库,写入xlsx文件成功.并且写入 xlsm 文件是成功的.但是因为打开xlsm文件时出错,我无法打开xlsm文件.

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.

使用 Apache POI 库编写 xlsm 文件是否可行?

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

如果可以写xlsm,请提供指导线How 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();
    }

谢谢

推荐答案

根据 Apache POI 的文档,无法创建宏:http://poi.apache.org/spreadsheet/limitations.html

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

但是,可以读取和重写包含宏的文件,并且 apache poi 将安全地保留宏.

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

这是一个例子:

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.

这篇关于使用 apache poi 写入 xlsm (Excel 2007)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 15:52