jxls专题之多Sheet页回填
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">版本信息:Java8、jxls 2.14.0</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">本文介绍下<span style="color: black;">根据</span>模板生成多Sheet页的内容。<span style="color: black;">运用</span>到的jxls命令是 jx:each ,看名<span style="color: black;">叫作</span>跟java的for-each的each是有点那味了。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Sheet页内的多行数据回填和按模板回填多Sheet页用的都是 jx:each 命令,<span style="color: black;">经过</span>命令内<span style="color: black;">区别</span>的参数指定数据,配置多Sheet页<span style="color: black;">重点</span>指定 multisheet 的属性。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">示例</p>jx:each(items=<span style="color: black;">"sheets"</span>, <span style="color: black;">var</span>=<span style="color: black;">"sheet"</span>, multisheet=<span style="color: black;">"sheetNames"</span>, lastCell=<span style="color: black;">"H4"</span>)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">需求场景:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">导出各个班级的学生的考试成绩,<span style="color: black;">包括</span>班级、班<span style="color: black;">专家</span>、学生、学生各科成绩。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">jxls excel模版:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-axegupay5k/1704a5d64c1b425cb7f7784ed0d4cbcd~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723889708&x-signature=8NHbUDoK%2FHLneBRhziLDEWHOWM0%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">多sheet页到处模板</p>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">模版文件有两个页循环、行循环两个批注:</p><span style="color: black;">jx</span>:area(lastCell=<span style="color: black;">"H4"</span>)
<span style="color: black;">jx</span>:each(items=<span style="color: black;">"sheets"</span>, var=<span style="color: black;">"sheet"</span>, multisheet=<span style="color: black;">"sheetNames"</span>, lastCell=<span style="color: black;">"H4"</span>)jx:each(items=”sheet.students”<span style="color: black;">var</span>=”student” lastCell=”H4”) <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">重视</span>不要忘记添加 jx:area(lastCell="H4") ,要先指定模版范围才<span style="color: black;">能够</span>正确解析!</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">直接导入测试数据,测试的数据样例放在文末,避免栈太多篇幅,核心代码是:</p><span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">static</span> <span style="color: black;">void</span> <span style="color: black;">main</span>(<span style="color: black;">String[] args</span>)</span> {
<span style="color: black;">try</span> (InputStream <span style="color: black;">in</span> = <span style="color: black;">new</span> FileInputStream(<span style="color: black;">"/yourPath/multiSheet.xlsx"</span>);
FileOutputStream <span style="color: black;">out</span> = <span style="color: black;">new</span> FileOutputStream(<span style="color: black;">"/yourPath/多页导出.xlsx"</span>)) {
Context context =<span style="color: black;">new</span> Context();
context.putVar(<span style="color: black;">"sheetNames"</span>, Arrays.asList(<span style="color: black;">"一班"</span>, <span style="color: black;">"二班"</span>));
context.putVar(<span style="color: black;">"sheets"</span>, Arrays.asList(getClassOne(), getClassTwo()));
JxlsHelper.getInstance().processTemplate(<span style="color: black;">in</span>, <span style="color: black;">out</span>, context);
} <span style="color: black;">catch</span> (Exception e) {
<span style="color: black;">throw</span> <span style="color: black;">new</span>RuntimeException(e);
}
}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Context<span style="color: black;">经过</span>putVar设置的key为 sheetNames 和 sheets ,与模版配置中所<span style="color: black;">选取</span>的key对应:</p>jx:each(items=<span style="color: black;">"sheets"</span>, <span style="color: black;">var</span>=<span style="color: black;">"sheet"</span>, multisheet=<span style="color: black;">"sheetNames"</span>, lastCell=<span style="color: black;">"H4"</span>)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">查看导出的效果:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/4a1ad4eb8ddd46c2851c9a3c7ea8ff21~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723889708&x-signature=sQYK%2Fod01VsTxnhRlK%2FJ3rO6ik4%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">一班成绩</p>
</div>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/8312629b096a43c1b86d9dcbeecff9ed~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723889708&x-signature=Z3M89V0MPLUz8y1o3wMCDl4MTl8%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">二班成绩</p>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">必须</span><span style="color: black;">重视</span>的是,多页的配置<span style="color: black;">通常</span><span style="color: black;">意见</span>和 area指定放在同一处,都放在sheet的<span style="color: black;">第1</span>个单元格(A1)上,避免<span style="color: black;">显现</span>不符合预期的导出文件。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">如示例,把多页的配置放到A3位置:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/bbe7127aed7644a08f439d4a0ca9f7ae~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723889708&x-signature=95RZWRYzdWarkizg%2FuwpCvm%2BIjw%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">看下导出来的数据:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/7489c697d7574bbb8c45d87120d28266~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723889708&x-signature=P0HywNeWCLVyAevGhr%2FG6Do9KdY%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">能够</span>看到前两行<span style="color: black;">包含</span>固定数据和配置数据都被清空了,说明默认<span style="color: black;">状况</span>下,多sheet页是从多sheet页配置的单元格的那一行<span style="color: black;">起始</span>生效的,之前行的模版数据和配置将会被清空,不会有数据。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">完整测试代码:</p><span style="color: black;">public</span> <span style="color: black;">class</span> <span style="color: black;">MultiSheetDemo</span> {
<span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">static</span> <span style="color: black;">void</span> <span style="color: black;">main</span>(<span style="color: black;">String[] args</span>)</span> {
<span style="color: black;">try</span> (InputStream <span style="color: black;">in</span> = <span style="color: black;">new</span> FileInputStream(<span style="color: black;">"/yourPath/multiSheet.xlsx"</span>);
FileOutputStream <span style="color: black;">out</span> = <span style="color: black;">new</span> FileOutputStream(<span style="color: black;">"/yourPath/多页导出.xlsx"</span>)) {
Context context = <span style="color: black;">new</span> Context();
context.putVar(<span style="color: black;">"sheetNames"</span>, Arrays.asList(<span style="color: black;">"一班"</span>, <span style="color: black;">"二班"</span>));
context.putVar(<span style="color: black;">"sheets"</span>, Arrays.asList(getClassOne(), getClassTwo()));
JxlsHelper.getInstance().processTemplate(<span style="color: black;">in</span>, <span style="color: black;">out</span>, context);
} <span style="color: black;">catch</span> (Exception e) {
<span style="color: black;">throw</span> <span style="color: black;">new</span> RuntimeException(e);
}
}
<span style="color: black;"><span style="color: black;">private</span> <span style="color: black;">static</span> Class <span style="color: black;">getClassOne</span>()</span> {
Class bClass = <span style="color: black;">new</span>Class();
bClass.setClassName(<span style="color: black;">"三年一班"</span>);
bClass.setMaster(<span style="color: black;">"一班张老师"</span>);
Student studentB = <span style="color: black;">new</span> Student();
studentB.setName(<span style="color: black;">"一班狗蛋"</span>);
studentB.setSex(<span style="color: black;">"男"</span>);
studentB.setA(<span style="color: black;">33</span>);
studentB.setB(<span style="color: black;">20</span>);
studentB.setC(<span style="color: black;">10</span>);
studentB.setD(<span style="color: black;">5</span>);
studentB.setE(<span style="color: black;">8</span>);
studentB.setF(<span style="color: black;">20</span>);
Student studentB1 =<span style="color: black;">new</span> Student();
studentB1.setName(<span style="color: black;">"一班铁柱"</span>);
studentB1.setSex(<span style="color: black;">"男"</span>);
studentB1.setA(<span style="color: black;">10</span>);
studentB1.setB(<span style="color: black;">22</span>);
studentB1.setC(<span style="color: black;">31</span>);
studentB1.setD(<span style="color: black;">21</span>);
studentB1.setE(<span style="color: black;">3</span>);
studentB1.setF(<span style="color: black;">45</span>);
bClass.setStudents(Arrays.asList(studentB,studentB1));
<span style="color: black;">return</span>bClass;
}<span style="color: black;"><span style="color: black;">private</span> <span style="color: black;">static</span> Class <span style="color: black;">getClassTwo</span>()</span> {
Class aClass = <span style="color: black;">new</span> Class();
aClass.setClassName(<span style="color: black;">"三年二班"</span>);
aClass.setMaster(<span style="color: black;">"二班李老师"</span>);
Student studentA =<span style="color: black;">new</span> Student();
studentA.setName(<span style="color: black;">"二班小明"</span>);
studentA.setSex(<span style="color: black;">"男"</span>);
studentA.setA(<span style="color: black;">100</span>);
studentA.setB(<span style="color: black;">100</span>);
studentA.setC(<span style="color: black;">90</span>);
studentA.setD(<span style="color: black;">80</span>);
studentA.setE(<span style="color: black;">95</span>);
studentA.setF(<span style="color: black;">100</span>);
Student studentA1 = <span style="color: black;">new</span> Student();
studentA1.setName(<span style="color: black;">"二班李华"</span>);
studentA1.setSex(<span style="color: black;">"男"</span>);
studentA1.setA(<span style="color: black;">100</span>);
studentA1.setB(<span style="color: black;">100</span>);
studentA1.setC(<span style="color: black;">100</span>);
studentA1.setD(<span style="color: black;">100</span>);
studentA1.setE(<span style="color: black;">95</span>);
studentA1.setF(<span style="color: black;">100</span>);
aClass.setStudents(Arrays.asList(studentA, studentA1));
<span style="color: black;">return</span> aClass;
}
@Data
<span style="color: black;">public</span> <span style="color: black;">static</span> <span style="color: black;">class</span> <span style="color: black;">Class</span>{
<span style="color: black;">private</span> String className;
<span style="color: black;">private</span> String master;
<span style="color: black;">private</span> List<Student> students;
}
@Data
<span style="color: black;">public</span> <span style="color: black;">static</span> <span style="color: black;">class</span> <span style="color: black;">Student</span>{
<span style="color: black;">private</span> String name;
<span style="color: black;">private</span> String sex;
<span style="color: black;">private</span> <span style="color: black;">int</span> a;
<span style="color: black;">private</span> <span style="color: black;">int</span> b;
<span style="color: black;">private</span> <span style="color: black;">int</span> c;
<span style="color: black;">private</span> <span style="color: black;">int</span> d;
<span style="color: black;">private</span> <span style="color: black;">int</span> e;
<span style="color: black;">private</span> <span style="color: black;">int</span> f;
}
}
期待楼主的下一次分享!” 谷歌外贸网站优化技术。 外链发布社区 http://www.fok120.com/
页:
[1]