Java动态生成含有excel的word表单

生成这样一个word表单

 

文档另存为为word.xml格式:

 

在线格式转化:https://tool.oschina.net/codeformat/xml  地址

在需要遍历的地方 :

<#list taskList as list> </#list>  #list 是遍历标识    taskList  是传值的集合数据名称

<!-- 这是判断数据来展示是否 --><#if list_index==0> </#if>  用if判断

 

复制代码
<#list taskList as list>
<!-- 这是判断数据来展示是否 -->
<#if list_index==0>
<w:t>作业活动</w:t>
</#if>

<w:t>${list_index + 1}</w:t>



<w:tc>
<w:tcPr>
<w:tcW w:w="1305" w:type="dxa"/>
${list.color}
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p>
<w:pPr>
<w:widowControl/>
<w:jc w:val="center"/>
<w:textAlignment w:val="center"/>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312"/>
<w:color w:val="000000"/>
<w:kern w:val="0"/>
<w:szCs w:val="21"/>
<w:highlight w:val="yellow"/>
</w:rPr>
</w:pPr>
</w:p>
</w:tc>

</#list>
复制代码

 

java代码:

 

 

 

复制代码
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.*;
import java.util.*;


public class WordUtil {

private Configuration configuration = null;

public WordUtil(){
configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
}

  // 获取数据源
  public static Map<String, Object> getData() { Map<String, Object> dataMap = new HashMap<>(); // word3测试数据 List<Map<String,Object>> list = new ArrayList<Map<String,Object>>(); Map<String,Object> map1 = new HashMap<>(); map1.put("id",1); map1.put("name","磨具搬运"); map1.put("danger","行车吊运模具。"); map1.put("event","起重伤害"); map1.put("possible",3); map1.put("rate",6); map1.put("serious",7); map1.put("risk",126); map1.put("grade","一般风险"); map1.put("color","<w:shd w:val=\"clear\" w:color=\"auto\" w:fill=\"0000FF\"/>"); //<w:shd w:val="clear" w:color="auto" w:fill="0000FF"/> //<#elseif list.color=="2"> //<w:shd w:val="clear" w:color="auto" w:fill="FFFF00"/> //<#elseif list.color=="3"> //<w:shd w:val="clear" w:color="auto" w:fill="FF6100"/> //<#elseif list.color=="4"> //<w:shd w:val="clear" w:color="auto" w:fill="FF0000"/> Map<String,Object> map3 = new HashMap<>(); map3.put("id",2); map3.put("name","运输车"); map3.put("danger","行车吊运模具。"); map3.put("event","起重伤害"); map3.put("possible",3); map3.put("rate",6); map3.put("serious",7); map3.put("risk",126); map3.put("grade","较大风险"); map3.put("color","<w:shd w:val=\"clear\" w:color=\"auto\" w:fill=\"FF6100\"/>"); list.add(map1); list.add(map3); List<Map<String,Object>> list2= new ArrayList<Map<String,Object>>(); Map<String,Object> map2 = new HashMap<>(); map2.put("id",1); map2.put("name","磨具搬运"); map2.put("danger","行车吊运模具。"); map2.put("event","起重伤害"); map2.put("possible",3); map2.put("rate",6); map2.put("serious",7); map2.put("risk",126); map2.put("grade","较大风险"); map2.put("color","<w:shd w:val=\"clear\" w:color=\"auto\" w:fill=\"FFFF00\"/>"); list2.add(map2); // word4 测试 List<Map<String,Object>> list3 = new ArrayList<Map<String,Object>>(); List<Map<String,Object>> list4 = new ArrayList<Map<String,Object>>(); Map<String,Object> map4 = new HashMap<>(); map4.put("id",1); map4.put("name","运输车000"); map4.put("danger","00行车吊运模具。"); map4.put("eventType","11起重伤害"); map4.put("technology","工程技术1111"); map4.put("managementMeasures","1.定期检查;2.定期更新"); map4.put("controlPersonName","车间主任"); map4.put("dg1",""); map4.put("dg2","√"); map4.put("dg3","√"); map4.put("dg4","√"); map4.put("riskLevel","较大风险"); map4.put("color","<w:shd w:val=\"clear\" w:color=\"auto\" w:fill=\"FF6100\"/>"); Map<String,Object> map5 = new HashMap<>(); map5.put("id",2); map5.put("name","运输车111"); map5.put("danger","行车吊运模具。"); map5.put("eventType","起重伤害"); map5.put("technology","工程技术1111"); map5.put("managementMeasures","1.定期检查;2.定期更新"); map5.put("controlPersonName","公司领导"); map5.put("dg1","√"); map5.put("dg2","√"); map5.put("dg3","√"); map5.put("dg4","√"); map5.put("riskLevel","重大风险"); map5.put("color","<w:shd w:val=\"clear\" w:color=\"auto\" w:fill=\"FF0000\"/>"); list3.add(map4);list3.add(map5); Map<String,Object> map6 = new HashMap<>(); map6.put("id",1); map6.put("name",""); map6.put("danger",""); map6.put("eventType",""); map6.put("technology",""); map6.put("managementMeasures",""); map6.put("controlPersonName",""); map6.put("dg1",""); map6.put("dg2",""); map6.put("dg3",""); map6.put("dg4",""); map6.put("controlPersonLevel",""); map6.put("riskLevel",""); map6.put("color",""); list4.add(map6); dataMap.put("taskList", list3); dataMap.put("equipments", list4); return dataMap; }

  // 生成word
  // templateName 模板名称
  // filePath 文件地址
  // name 生成文件名称
  public static void createWord(String templateName, String filePath, String name){
        Map<String,Object> dataMap = getData();
//创建配置实例
Configuration configuration = new Configuration();
//设置编码
configuration.setDefaultEncoding("UTF-8");
configuration.setClassForTemplateLoading(WordUtil.class, "/word");//模板文件所在路径
Template t=null;
try {
t = configuration.getTemplate(templateName); //获取模板文件
} catch (IOException e) {
e.printStackTrace();
}
File outFile = new File("D:/outFile/new/"+ name + Math.random()*1000 +".doc"); //导出文件 防止知名称重复 加上uuid区分一下
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}

try {
t.process(dataMap, out); //将填充数据填入模板文件并输出到目标文件
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
 
复制代码

 

用到的jar包:

 <!--freemarker-->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.31</version>
        </dependency>

 

posted @   Tramp_Boy  阅读(387)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
点击右上角即可分享
微信分享提示