SpringBoot+Poi-tl根据Word模板动态生成word(含动态行表格、合并单元格)

本编文章继SpringBoot+Poi-tl根据Word模板动态生成word(含动态行表格)文章之后
介绍Poi-tl导出word的延伸功能:

所需依赖以及word模板所属位置 见 SpringBoot+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 = Calendar.getInstance(); 
			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);//序号
				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 basePath=ClassUtils.getDefaultClassLoader().getResource("").getPath()+"static/template/";
//			String resource =basePath+"orderD2.docx";//word模板地址
	        //word模板地址获取方式二:优点---相比上一种方式,这种方法不会在linux或者jar上失效
	      	ClassPathResource classPathResource = new ClassPathResource("static/template/orderD2.docx");
			String resource = classPathResource.getURL().getPath();
			//渲染表格  动态行
			HackLoopTableRenderPolicy  policy = new HackLoopTableRenderPolicy();
			Configure config = Configure.newBuilder()
					.bind("typeList", policy).bind("detailList", policy).build();
			
			XWPFTemplate template = XWPFTemplate.compile(resource, config).render(
					new HashMap<String, Object>() {
   {
   
						put("typeList", typeList);
						put("detailList",detailList);
						put("order_number", "2356346346645");
						put("y", now.get(Calendar.YEAR));//当前年
						put("m", (now.get(Calendar.MONTH) + 1));//当前月
						put("d", now.get(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格式
			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 = response.getOutputStream();
			template.write(out);
			out.flush();
			out.close();
			template.close();
		} catch (Exception e) {
   
			e.printStackTrace();
		}

}
(3)页面编写调用方法
<div class="m2" style="margin-top: 30px;">使用<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 = Calendar.getInstance(); 
			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);//序号
				if(i == 0){
   
					detailMap.put("sub_type", "监督技术装备");//商品所属大类名称
				}else if(i == 1){
   
					detailMap.put("sub_type", "火灾调查装备");//商品所属大类名称
				}else if(i == 2){
   
					detailMap.put("sub_type"
  • 24
    点赞
  • 134
    收藏
    觉得还不错? 一键收藏
  • 20
    评论
在Spring Boot中使用poi-tl库来导出带有合并列的Word表格并下载,您可以按照以下步骤操作: 1. 首先,确保您的Spring Boot项目中已经添加poi-tl的依赖。您可以在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.deepoove</groupId> <artifactId>poi-tl</artifactId> <version>1.6.0</version> </dependency> ``` 2. 创建一个Controller来处理导出请求。例如,创建一个名为WordExportController的类,并添加一个处理导出请求的方法。 ```java import com.deepoove.poi.XWPFTemplate; import com.deepoove.poi.data.*; import com.deepoove.poi.util.BytePictureUtils; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableRow; import org.springframework.core.io.InputStreamResource; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; @Controller public class WordExportController { @GetMapping("/export") public ResponseEntity<InputStreamResource> exportWord() throws IOException { // 创建一个数据模型 List<List<String>> tableData = new ArrayList<>(); tableData.add(createRow("Merged Cells", "Cell 3")); tableData.add(createRow("Cell 4", "Cell 6")); // 使用poi-tl的XWPFTemplate来生成Word文档 XWPFTemplate template = XWPFTemplate.compile("templates/template.docx").render( new DataTable(tableData) .setHeader(createRow("Header 1", "Header 2")) .setCellWidth(2000) // 设置单元格宽度 .setHeaderCellStyle(new CellStyle().setBold(true).setColor("FFFFFF").setBgColor("336699")) .setOddRowCellStyle(new CellStyle().setColor("FFFFFF").setBgColor("99CCFF")) .setEvenRowCellStyle(new CellStyle().setColor("FFFFFF").setBgColor("CCEEFF")) ); // 将生成Word文档转换为字节数组 ByteArrayOutputStream out = new ByteArrayOutputStream(); template.write(out); byte[] documentBytes = out.toByteArray(); // 设置下载响应的头信息 HttpHeaders headers = new HttpHeaders(); headers.setContentDispositionFormData("attachment", "merged_table.docx"); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); // 创建一个包Word文档字节数组的InputStreamResource InputStreamResource resource = new InputStreamResource(new ByteArrayInputStream(documentBytes)); // 返回响应实体 return ResponseEntity.ok() .headers(headers) .body(resource); } private List<String> createRow(String cell1, String cell2) { List<String> row = new ArrayList<>(); row.add(cell1); row.add(cell2); return row; } } ``` 3. 在resources目录下创建一个名为template.docx的Word模板文件。在模板文件中,您可以根据自己的需求设置表格样式和内容。 4. 启动您的Spring Boot应用程序,并访问导出请求的URL(例如:http://localhost:8080/export)。将会自动下载名为merged_table.docx的Word文档,其中包合并列的表格。 请确保按照您的需求修改代码,并根据模板文件的位置进相应的调整。 希望对您有所帮助!如果您有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值