java 如何编写 jXLS 模板以及在哪里编写它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10893121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:00:27  来源:igfitidea点击:

How to write a jXLS template and where to write it

javaexceljxls

提问by Raaz

I'm trying to use jXLS to export data from a list to an Excel sheet. I need to create an Excel template using jXLS and print out a list of data using that template. I have a Bean class called Department and I need to use a forEach statement to loop through the list and write data to the Excel sheet.

我正在尝试使用 jXLS 将数据从列表导出到 Excel 工作表。我需要使用 jXLS 创建一个 Excel 模板并使用该模板打印出数据列表。我有一个名为 Department 的 Bean 类,我需要使用 forEach 语句遍历列表并将数据写入 Excel 工作表。

Can someone please tell me how and where I can write my Excel template? I know my code inside should look something like this -

有人可以告诉我如何以及在哪里可以编写我的 Excel 模板吗?我知道我里面的代码应该是这样的 -

            <jx:forEach items="${departments}" var="department">
                ${department.name} | ${department.chief}
            </jx:forEach>

采纳答案by Sanjay Bharwani

You need to create an Excel Template file where in you define your basic structure which you need to repeat for number of objects in the collection.

您需要创建一个 Excel 模板文件,在其中定义您需要为集合中的多个对象重复的基本结构。

The code

代码

<jx:forEach items="${departments}" var="department">
                ${department.name} | ${department.chief}
            </jx:forEach>

will go in that template excel.

将进入该模板excel。

Then you need to use JXLS API in java code to generated the excel from this template.

然后你需要在java代码中使用JXLS API从这个模板生成excel。

Map contextBeans = new HashMap();
contextBeans.put("departments", departmentList);
xlsTransformer.transformXLS(xlsTemplateFileURL.getPath(), contextBeans, reportFileURL.getPath());

This code will create the excel file out of the template file populated with the collection loaded in contextBeans Map.

此代码将根据使用 contextBeans Map 中加载的集合填充的模板文件创建 excel 文件。

回答by sayannayas

Syntactically jXLS is very similar to JSTL. in your case all you need is an Excel template that will have columns filled with jXLS notation like

jXLS 在语法上与 JSTL 非常相似。在您的情况下,您只需要一个 Excel 模板,其中的列将填充 jXLS 表示法,例如

   cola              col b
1  {department.name}  {department.chief}

and in Java you need a HashMap that will have an ArrayList of all of your department beans.

而在 Java 中,您需要一个 HashMap,它将包含您所有部门 bean 的 ArrayList。

回答by Susie

you can past them in your Excel template's sheet,any row is ok.Maybe you should look examples in http://jxls.sourceforge.net/first.

您可以将它们粘贴到 Excel 模板的工作表中,任何行都可以。也许您应该首先查看http://jxls.sourceforge.net/ 中的示例。