问题描述
我已经编写了用于编写xlsm(Excel 2007)的java文件。使用Apache POI库,编写xlsx文件是成功的。并且写xlsm文件是成功的。但是,由于打开xlsm文件时出错,我无法打开xlsm文件。
使用Apache POI Library编写xlsm文件是否可行?
如果可以写xlsm,请提供指导如何使用Apache poi库编写xlsm文件。
XSSFWorkbook工作簿=新XSSFWorkbook();
XSSFSheet sheet = workBook.createSheet(Related_SRC);
String realName =Test.xlsm;
文件文件=新文件(C:\\\sdpApp,realName);
尝试{
FileOutputStream fileOutput = new FileOutputStream(file);
workBook.write(fileOutput);
fileOutput.close();
} catch(Exception e){
// TODO:handle exception
e.printStackTrace();
}
谢谢
根据Apache POI的文档,不可能创建宏:
但是,可以读取并重写包含宏和apache的文件poi将安全地保存宏。
这是一个例子:
String fileName =C:\\\\
ew_file.xlsm;
尝试{
工作簿工作簿;
workbook = new XSSFWorkbook(
OPCPackage.open(resources / template_with_macro.xlsm)
);
//使用WORKBOOK STUF
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();
}
创建的文件不会给您一个错误。
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.
这篇关于使用apache poi写入xlsm(Excel 2007)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!