write to xlsm (Excel 2007) using apache poi

后端 未结 2 619
滥情空心
滥情空心 2020-12-19 04:43

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 th

相关标签:
2条回答
  • 2020-12-19 04:56

    XLSM is Excel Spreadsheet with Macros, if you don't need macros in your sheet, you can simply use .xlsx as extension as well.

    You could also produce an .xls file (i.e. older format) via HSSFWorkbook, Excel should be able to open .xls just fine. Only limitation with this approach is that .xls is limited in the number of rows to 65k.

    0 讨论(0)
  • 2020-12-19 05:00

    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.

    0 讨论(0)
提交回复
热议问题