java根据模板动态生成word文档带表格

废话不多说上代码

    
   
      org.springframework.boot
      spring-boot-starter-web
   

   
      org.springframework.boot
      spring-boot-starter-test
      test
   

   
   
      org.springframework.boot
      spring-boot-devtools
      true
      true
   
   
   
      javax.servlet
      javax.servlet-api
   
   
      org.apache.tomcat.embed
      tomcat-embed-jasper
   
   
      javax.servlet
      jstl
   
   
   
   
   
       com.deepoove
      poi-tl
      1.7.3
   
   
       
           org.apache.poi
           poi-ooxml
           4.1.2
       
       
           org.apache.poi
           poi-ooxml-schemas
           4.1.2
       
       
           org.apache.poi
           poi
           4.1.2
       
   
    
           cn.afterturn
           easypoi-base
           3.0.3
       
       
           cn.afterturn
           easypoi-web
           3.0.3
       
       
           cn.afterturn
           easypoi-annotation
           3.0.3
       
 
  
package com.example.word.controller;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.policy.HackLoopTableRenderPolicy;
import com.example.word.common.MoneyUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.*;

public class testClas {

  public static final String filePath = "/static/template/";

    public static void exportDataWordD4(){
            Map params = new HashMap<>();
            // TODO 渲染其他类型的数据请参考官方文档
            DecimalFormat df = new DecimalFormat("######0.00");
            Calendar now = Calendar.getInstance();
            double money = 0;//总金额
            //组装表格列表数据
            List> typeList=new ArrayList>();
            for (int i = 0; i < 2; i++) {
                Map detailMap = new HashMap();
                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> detailList=new ArrayList>();
            for (int i = 0; i < 3; i++) {
                Map detailMap = new HashMap();
                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);
            HashMap datamap = new HashMap<>();
            datamap.put("typeList", typeList);
            datamap.put("detailList",detailList);
            datamap.put("order_number", "2356346346645");
            datamap.put("y", now.get(Calendar.YEAR));//当前年
            datamap.put("m", (now.get(Calendar.MONTH) + 1));//当前月
            datamap.put("d", now.get(Calendar.DAY_OF_MONTH));//当前日
            datamap.put("order_money",order_money);//总金额
            datamap.put("money_total",money_total);//金额中文大写
        //渲染表格  动态行
        HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
        Configure config = Configure.newBuilder()
                .bind("typeList", policy)
                .bind("detailList", policy).build();


        String tempFileName = "orderD2.docx";

        String temDir="C:/test/"+ File.separator+"file/word/"; ;//生成临时文件存放地址
        String getinFileName =   filePath + tempFileName;
        //获取模板路径
        testClas a = new testClas();
        InputStream getin = a.geresource(getinFileName);
        //生成world
        makeword(getin,config,datamap,temDir);
    }

    public static void makeword(InputStream resource,
                                     Configure config,
                                     HashMap hashMap,
                                        String temDir){
        try {

           XWPFTemplate template = XWPFTemplate.compile(resource, config).render(hashMap);
            //生成文件名
            Long time = new Date().getTime();
            // 生成的word格式
            String formatSuffix = ".docx";
            // 拼接后的文件名
            String fileName = time + formatSuffix;//文件名  带后缀
            FileOutputStream fos = new FileOutputStream(temDir+fileName);
            template.write(fos);
            template.close();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
    public InputStream geresource(String filePath){
        InputStream resourceAsStream = this.getClass().getResourceAsStream(filePath);
        return resourceAsStream;
    }


    public static void main(String[] args) {
        exportDataWordD4();
    }


}
    public static void main(String[] args) {
        exportDataWordD4();
    }


}

java根据模板动态生成word文档带表格_第1张图片

java根据模板动态生成word文档带表格_第2张图片

java根据模板动态生成word文档带表格_第3张图片

你可能感兴趣的:(java,word,开发语言)