java生成不规则的word文档导出

1、word内容如下

 

2、 将该word文件另存为xml格式

3、将xml文件的扩展名直接改为ftl 

4、对于表格的循环 搜索<w:tr> 跳过表头的,找到对应数据的<w:tr>

5、用<#list flowList as flow> </#list>标签将数据对应的 w:tr 标签包围起来,对应数据改成实体的相应字段,如 ${flow.timeSlot}

<#list flowList as flow>
<w:tr><w:tblPrEx><w:shd w:val="clear" w:color="auto" w:fill="auto"/><w:tblCellMar><w:top w:w="0" w:type="dxa"/><w:left w:w="0" w:type="dxa"/><w:bottom w:w="0" w:type="dxa"/><w:right w:w="0" w:type="dxa"/></w:tblCellMar></w:tblPrEx><w:trPr><w:trHeight w:val="321" w:hRule="atLeast"/></w:trPr><w:tc><w:tcPr><w:tcW w:w="1282" w:type="dxa"/><w:tcBorders><w:top w:val="single" w:color="000000" w:sz="4" w:space="0"/><w:left w:val="single" w:color="000000" w:sz="4" w:space="0"/><w:bottom w:val="single" w:color="000000" w:sz="4" w:space="0"/><w:right w:val="single" w:color="000000" w:sz="4" w:space="0"/></w:tcBorders><w:shd w:val="clear" w:color="auto" w:fill="auto"/><w:noWrap/><w:tcMar><w:top w:w="15" w:type="dxa"/><w:left w:w="15" w:type="dxa"/><w:right w:w="15" w:type="dxa"/></w:tcMar><w:vAlign w:val="center"/></w:tcPr><w:p><w:pPr><w:bidi w:val="0"/><w:rPr><w:rFonts w:hint="default" w:eastAsiaTheme="minorEastAsia"/><w:lang w:val="en-US" w:eastAsia="zh-CN"/></w:rPr></w:pPr><w:r><w:t> </w:t></w:r><w:r><w:rPr><w:rFonts w:hint="eastAsia"/><w:lang w:val="en-US" w:eastAsia="zh-CN"/></w:rPr><w:t>${flow.timeSlot}</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1853" w:type="dxa"/><w:tcBorders><w:top w:val="single" w:color="000000" w:sz="4" w:space="0"/><w:left w:val="single" w:color="000000" w:sz="4" w:space="0"/><w:bottom w:val="single" w:color="000000" w:sz="4" w:space="0"/><w:right w:val="single" w:color="000000" w:sz="4" w:space="0"/></w:tcBorders><w:shd w:val="clear" w:color="auto" w:fill="auto"/><w:tcMar><w:top w:w="15" w:type="dxa"/><w:left w:w="15" w:type="dxa"/><w:right w:w="15" w:type="dxa"/></w:tcMar><w:vAlign w:val="center"/></w:tcPr><w:p><w:pPr><w:bidi w:val="0"/><w:rPr><w:rFonts w:hint="eastAsia"/></w:rPr></w:pPr><w:r><w:t>${flow.inCnt}</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1700" w:type="dxa"/><w:tcBorders><w:top w:val="single" w:color="000000" w:sz="4" w:space="0"/><w:left w:val="single" w:color="000000" w:sz="4" w:space="0"/><w:bottom w:val="single" w:color="000000" w:sz="4" w:space="0"/><w:right w:val="single" w:color="000000" w:sz="4" w:space="0"/></w:tcBorders><w:shd w:val="clear" w:color="auto" w:fill="auto"/><w:tcMar><w:top w:w="15" w:type="dxa"/><w:left w:w="15" w:type="dxa"/><w:right w:w="15" w:type="dxa"/></w:tcMar><w:vAlign w:val="center"/></w:tcPr><w:p><w:pPr><w:bidi w:val="0"/><w:rPr><w:rFonts w:hint="default" w:eastAsiaTheme="minorEastAsia"/><w:lang w:val="en-US" w:eastAsia="zh-CN"/></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="eastAsia"/><w:lang w:val="en-US" w:eastAsia="zh-CN"/></w:rPr><w:t>10</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="4960" w:type="dxa"/><w:tcBorders><w:top w:val="single" w:color="000000" w:sz="4" w:space="0"/><w:left w:val="single" w:color="000000" w:sz="4" w:space="0"/><w:bottom w:val="single" w:color="000000" w:sz="4" w:space="0"/><w:right w:val="single" w:color="000000" w:sz="4" w:space="0"/></w:tcBorders><w:shd w:val="clear" w:color="auto" w:fill="auto"/><w:tcMar><w:top w:w="15" w:type="dxa"/><w:left w:w="15" w:type="dxa"/><w:right w:w="15" w:type="dxa"/></w:tcMar><w:vAlign w:val="center"/></w:tcPr><w:p><w:pPr><w:bidi w:val="0"/><w:rPr><w:rFonts w:hint="default" w:eastAsiaTheme="minorEastAsia"/><w:lang w:val="en-US" w:eastAsia="zh-CN"/></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="eastAsia"/><w:lang w:val="en-US" w:eastAsia="zh-CN"/></w:rPr><w:t>${flow.surplusCnt}</w:t></w:r></w:p></w:tc></w:tr>
</#list>

 

6、pom引入freemarker

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-freemarker</artifactId>
            </dependency>

7、代码如下

 public static void main(String[] args) throws Exception {

        Map<String,Object> dataMap = new HashMap();
        List<PassengerFlow> list = new ArrayList<PassengerFlow>();
        for(int i=0;i<10;i++){
            PassengerFlow flow = new PassengerFlow();
            flow.setInCnt(100*i+100);
            flow.setSurplusCnt(86*(i+1));
            flow.setTimeSlot(i+11+"时间段");
            list.add(flow);
        }
        dataMap.put("flowList", list);
        dataMap.put("a","aa");
        dataMap.put("b","BB");
        dataMap.put("c",100);
        dataMap.put("d",1100);


        Configuration configuration = new Configuration();
        configuration.setDefaultEncoding("utf-8");
        configuration.setDirectoryForTemplateLoading(new File("d:/"));
        File outFile = new File("D:/test.doc");
        Template t =  configuration.getTemplate("word测试.ftl","utf-8");
        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"),10240);
        t.process(dataMap, out);
        out.close();
}

 

8、javaWeb中利用response导出 (注意编码问题,防止中文乱码) 

    @RequestMapping("/test1")
    public void test1(HttpServletRequest request, HttpServletResponse response) throws Exception {

        Map<String, Object> dataMap = new HashMap();
        List<PassengerFlow> list = new ArrayList<PassengerFlow>();
        for (int i = 0; i < 10; i++) {
            PassengerFlow flow = new PassengerFlow();
            flow.setInCnt(100 * i + 100);
            flow.setSurplusCnt(86 * (i + 1));
            flow.setTimeSlot(i + 11 + "时间段");
            list.add(flow);
        }
        dataMap.put("flowList", list);
        dataMap.put("a", "aa");
        dataMap.put("b", "BB");
        dataMap.put("c", 100);
        dataMap.put("d", 1100);


        Configuration configuration = new Configuration();
        configuration.setDefaultEncoding("utf-8");
        configuration.setDirectoryForTemplateLoading(new File("d:/"));//指定ftl所在目录,根据自己的改
        response.setContentType("application/msword");
        response.setHeader("Content-Disposition", "attachment;filename=\"" + new String("文件名.doc".getBytes("GBK"), "iso8859-1") + "\"");
        response.setCharacterEncoding("utf-8");//此句非常关键,不然word文档全是乱码
        PrintWriter out = response.getWriter();
        Template t = configuration.getTemplate("word测试.ftl", "utf-8");//以utf-8的编码读取ftl文件
        t.process(dataMap, out);
        out.close();

    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

非ban必选

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值