如何使用Java使用表单控件创建Excel文件?

时间:2022-10-30 14:49:35

I have a Java application that uses the Apache POI library to build XLSX files. I have a customer who has an XLSX template with form controls (mostly list boxes and comobo boxes; not ActiveX, just regular form controls).

我有一个Java应用程序,它使用Apache POI库构建XLSX文件。我有一个客户,他有一个带有表单控件的XLSX模板(主要是列表框和comobo框;不是ActiveX,只是常规的表单控件。

We need to create XLSX files that replicate this template format; filling it in with pre-populated data. However, I cannot find a lot of information on how to create XLSX files with populated form controls from Java. The only post here I could find about it is here.

我们需要创建XLSX文件来复制这个模板格式;用预先填充的数据填充它。但是,我找不到很多关于如何使用Java填充的表单控件创建XLSX文件的信息。我能找到的关于它的唯一的帖子在这里。

We currently use the Apache POI library but I am open to alternate libraries that might be able to accomplish this easier, or even a mixed Java / VBA approach if that is the only way. Appreciate any help or insight, thanks in advance!

我们目前使用的是Apache POI库,但我对其他库开放,这些库可能能够更容易地完成这一任务,如果这是唯一的方法,甚至可以使用混合Java / VBA方法。感谢您的帮助和洞察力,感谢您的帮助!

1 个解决方案

#1


1  

You can create an Excel file template containing all of the form controls that you need, and store the actual Excel file inside your application jar file. Then from within your application you can open up the template file using POI, fill in the template with the data you require, and write out the new file to the local file system. For example:

您可以创建一个包含所需的所有表单控件的Excel文件模板,并将实际的Excel文件存储在应用程序jar文件中。然后,您可以在应用程序中使用POI打开模板文件,用所需的数据填充模板,并将新文件写入本地文件系统。例如:

outFile = new File("/myfiles", "table1.xlsx");
try (FileOutputStream fileOut = new FileOutputStream(outFile)) {
    InputStream is = getClass().getResourceAsStream("/com/myapp/templates/template1.xlsx");
    Workbook wb = new XSSFWorkbook(is);
    Sheet sheet = wb.getSheetAt(0);
    // Now populate the table here as your application requires
    wb.write(fileOut);
}

#1


1  

You can create an Excel file template containing all of the form controls that you need, and store the actual Excel file inside your application jar file. Then from within your application you can open up the template file using POI, fill in the template with the data you require, and write out the new file to the local file system. For example:

您可以创建一个包含所需的所有表单控件的Excel文件模板,并将实际的Excel文件存储在应用程序jar文件中。然后,您可以在应用程序中使用POI打开模板文件,用所需的数据填充模板,并将新文件写入本地文件系统。例如:

outFile = new File("/myfiles", "table1.xlsx");
try (FileOutputStream fileOut = new FileOutputStream(outFile)) {
    InputStream is = getClass().getResourceAsStream("/com/myapp/templates/template1.xlsx");
    Workbook wb = new XSSFWorkbook(is);
    Sheet sheet = wb.getSheetAt(0);
    // Now populate the table here as your application requires
    wb.write(fileOut);
}