SpringBoot+Poi-tl根据Word模板动态⽣成word(含动态⾏表格、合并单元格)
本编⽂章继⽂章之后
介绍Poi-tl导出word的延伸功能:
所需依赖以及word模板所属位置见,这⾥不再累赘。
直接介绍延伸功能的实现。
⼀、延伸功能
功能1:导出多个动态⾏表格(固定个数)
功能2:导出循环列表下的动态⾏表格(个数不固定)
功能3:导出合并单元格
功能4:导出循环列表下合并单元格
功能5:导出循环列表下合并单元格、外加⼀个动态⾏表格
⼆、功能实现
功能1:导出多个动态⾏表格(固定个数)
(1)新建⼀个word(orderD2.docx),编写word模板:
(2)在ExportWordController类中,编写相应的导出⽅法,供页⾯请求
/
**
* 销售订单信息导出word --- poi-tl(包含两个动态⾏表格)
* @throws IOException
*/
@RequestMapping("/exportDataWordD4")
public void exportDataWordD4(HttpServletRequest request,HttpServletResponse response)throws IOException{
try{
Map<String, Object> params =new HashMap<>();
// TODO 渲染其他类型的数据请参考官⽅⽂档
DecimalFormat df =new DecimalFormat(">#0.00");
Calendar now = Instance();
double money =0;//总⾦额
//组装表格列表数据
List<Map<String,Object>> typeList=new ArrayList<Map<String,Object>>();
for(int i =0; i <2; i++){
Map<String,Object> detailMap =new HashMap<String, Object>();
detailMap.put("index", i+1);//序号
detailMap.put("index", i+1);//序号
if(i ==0){
detailMap.put("sub_type","监督技术装备");//商品所属⼤类名称
}else if(i ==1){
detailMap.put("sub_type","⽕灾调查装备");//商品所属⼤类名称
}else if(i ==2){
detailMap.put("sub_type","⼯程验收装备");//商品所属⼤类名称
}
double saleprice=Double.valueOf(String.valueOf(100+i));
Integer buy_num=Integer.valueOf(String.valueOf(3+i));
String buy_price=df.format(saleprice*buy_num);
detailMap.put("buy_price", buy_price);//所属⼤类总价格
money=money+Double.valueOf(buy_price);
typeList.add(detailMap);
}
//组装表格列表数据
List<Map<String,Object>> detailList=new ArrayList<Map<String,Object>>();
for(int i =0; i <3; i++){
Map<String,Object> detailMap =new HashMap<String, Object>();
detailMap.put("index", i+1);//序号
if(i ==0|| i ==1){
detailMap.put("product_type","⼆级分类1");//商品⼆级分类
}else{
detailMap.put("product_type","⼆级分类2");//商品⼆级分类
}
detailMap.put("title","商品"+i);//商品名称
detailMap.put("product_description","套");//商品规格
detailMap.put("buy_num",3+i);//销售数量
detailMap.put("saleprice",100+i);//销售价格
detailMap.put("technical_parameter","技术参数"+i);//技术参数
detailList.add(detailMap);
}
//总⾦额
String order_money=String.valueOf(money);
//⾦额中⽂⼤写
String money_total = MoneyUtils.change(money);
//word模板地址获取⽅式⼀:缺点---打jar包获取不到该路径
//  String DefaultClassLoader().getResource("").getPath()+"static/template/";
/
/  String resource =basePath+"orderD2.docx";//word模板地址
//word模板地址获取⽅式⼆:优点---相⽐上⼀种⽅式,这种⽅法不会在linux或者jar上失效
ClassPathResource classPathResource =new ClassPathResource("static/template/orderD2.docx");  String resource = URL().getPath();
//渲染表格动态⾏
HackLoopTableRenderPolicy  policy =new HackLoopTableRenderPolicy();
Configure config = wBuilder()
.bind("typeList", policy).bind("detailList", policy).build();
XWPFTemplate template = XWPFTemplatepile(resource, config).render(
new HashMap<String, Object>(){{
put("typeList", typeList);
put("detailList",detailList);
put("order_number","2356346346645");
put("y", (Calendar.YEAR));//当前年
put("m",((Calendar.MONTH)+1));//当前⽉
put("d", (Calendar.DAY_OF_MONTH));//当前⽇
put("order_money",order_money);//总⾦额
put("money_total",money_total);//⾦额中⽂⼤写
}}
);
//=================⽣成⽂件保存在本地D盘某⽬录下=================
String temDir="D:/mimi/"+File.separator+"file/word/";;//⽣成临时⽂件存放地址
//⽣成⽂件名
Long time =new Date().getTime();
// ⽣成的word格式
// ⽣成的word格式
String formatSuffix =".docx";
// 拼接后的⽂件名
String fileName = time + formatSuffix;//⽂件名带后缀
FileOutputStream fos =new FileOutputStream(temDir+fileName);
template.write(fos);
//=================⽣成word到设置浏览默认下载地址=================
// 设置强制下载不打开
response.setContentType("application/force-download");
/
/ 设置⽂件名
response.addHeader("Content-Disposition","attachment;fileName="+ fileName);
OutputStream out = OutputStream();
template.write(out);
out.flush();
out.close();
template.close();
}catch(Exception e){
e.printStackTrace();
}
}
(3)页⾯编写调⽤⽅法
<div class="m2" >使⽤<span class="s1">POI-tl</span>根据word模板动态⽣成word(包含两个动态⾏表格)</div>
<a href="#"class="easyui-linkbutton"onclick="doExportWordD4();"data-options="iconCls:'icon-save'">导出word(包含两个动态⾏表格)</a>
//⽅式⼀导出word(包含两个动态⾏表格)
function doExportWordD4(){
window.location.href="<%=basePath%>/auth/exportWord/exportDataWordD4";
}
(4)导出结果:
功能2:导出循环列表下的动态⾏表格(个数不固定)
如果列表的每⼀项不是简单的⽂本,⽽是包含很多⽂档内容,或者多级列表该怎么⽣成? 区块对的循环功能可以很好的循环列表,并且⽀持编号有序。
(1)新建⼀个word(order2.docx),编写word模板:
(2)在ExportWordController类中,编写相应的导出⽅法,供页⾯请求
/**
* 销售订单信息导出word --- poi-tl(包含动态⾏表格、循环列表中的动态⾏表格)
* @throws IOException
*/
@RequestMapping("/exportDataWord4")
public void exportDataWord4(HttpServletRequest request,HttpServletResponse response)throws IOException{ try{
Map<String, Object> params =new HashMap<>();
// TODO 渲染其他类型的数据请参考官⽅⽂档
DecimalFormat df =new DecimalFormat(">#0.00");
Calendar now = Instance();
double money =0;//总⾦额
//组装表格列表数据springboot中文
List<Map<String,Object>> typeList=new ArrayList<Map<String,Object>>();
for(int i =0; i <2; i++){
Map<String,Object> detailMap =new HashMap<String, Object>();
detailMap.put("index", i+1);//序号
if(i ==0){
detailMap.put("sub_type","监督技术装备");//商品所属⼤类名称
}else if(i ==1){
detailMap.put("sub_type","⽕灾调查装备");//商品所属⼤类名称
}else if(i ==2){
detailMap.put("sub_type","⼯程验收装备");//商品所属⼤类名称
}
double saleprice=Double.valueOf(String.valueOf(100+i));
Integer buy_num=Integer.valueOf(String.valueOf(3+i));
String buy_price=df.format(saleprice*buy_num);
detailMap.put("buy_price", buy_price);//所属⼤类总价格
money=money+Double.valueOf(buy_price);
typeList.add(detailMap);
}
//组装表格列表数据
List<Map<String,Object>> detailList=new ArrayList<Map<String,Object>>();
for(int i =0; i <3; i++){
Map<String,Object> detailMap =new HashMap<String, Object>();
detailMap.put("index", i+1);//序号
if(i ==0|| i ==1){
detailMap.put("product_type","⼆级分类1");//商品⼆级分类
}else{
detailMap.put("product_type","⼆级分类2");//商品⼆级分类
}
detailMap.put("title","商品"+i);//商品名称
detailMap.put("title","商品"+i);//商品名称
detailMap.put("product_description","套");//商品规格
detailMap.put("buy_num",3+i);//销售数量
detailMap.put("saleprice",100+i);//销售价格
detailMap.put("technical_parameter","技术参数"+i);//技术参数
detailList.add(detailMap);
}
List<Map<String,Object>> tList=new ArrayList<Map<String,Object>>();
Map<String,Object> tMap =new HashMap<String, Object>();
tMap.put("index",1);
tMap.put("sub_type","监督技术装备");
tMap.put("detailList", detailList);
tMap.put("buy_price",100);
tList.add(tMap);
tMap =new HashMap<String, Object>();
tMap.put("index",2);
tMap.put("sub_type","⽕灾调查装备");
tMap.put("detailList", detailList);
tMap.put("buy_price",200);
tList.add(tMap);
tMap =new HashMap<String, Object>();
tMap.put("index",3);
tMap.put("sub_type","⼯程验收装备");
tMap.put("detailList", detailList);
tMap.put("buy_price",300);
tList.add(tMap);
//总⾦额
String order_money=String.valueOf(money);
//⾦额中⽂⼤写
String money_total = MoneyUtils.change(money);
//word模板地址获取⽅式⼀:缺点---打jar包获取不到该路径
//  String DefaultClassLoader().getResource("").getPath()+"static/template/"; //  String resource =basePath+"order2.docx";//word模板地址
//word模板地址获取⽅式⼆:优点---相⽐上⼀种⽅式,这种⽅法不会在linux或者jar上失效
ClassPathResource classPathResource =new ClassPathResource("static/template/order2.docx");  String resource = URL().getPath();
//渲染表格动态⾏
HackLoopTableRenderPolicy  policy =new HackLoopTableRenderPolicy();
Configure config = wBuilder()
.bind("typeList", policy).bind("detailList", policy).build();
XWPFTemplate template = XWPFTemplatepile(resource, config).render(
new HashMap<String, Object>(){{
put("typeList", typeList);
put("typeProducts",tList);
put("order_number","2356346346645");
put("y", (Calendar.YEAR));//当前年
put("m",((Calendar.MONTH)+1));//当前⽉
put("d", (Calendar.DAY_OF_MONTH));//当前⽇
put("order_money",order_money);//总⾦额
put("money_total",money_total);//⾦额中⽂⼤写
}}
);
/
/=================⽣成⽂件保存在本地D盘某⽬录下=================
String temDir="D:/mimi/"+File.separator+"file/word/";;//⽣成临时⽂件存放地址
//⽣成⽂件名
Long time =new Date().getTime();
// ⽣成的word格式

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。

发表评论