使用struts2实现数据库数据导出成word文档(项目当中实际用的)

首先,需要准备一个Word(.doc或者.docx)文档的模版,把里面需要动态插入的数据写成以英文开头的字符串,例如:


第二步:把这个word模版另存为2003的xml文件,然后打开,把里面对应的英文字符串改成 ${英文字符串} 的形式(例如:${d1}),用于动态插入。

第三步:把修改好的xml文件的后缀名直接改成ftl。(这就是程序用的真正模版)

第四步,写代码:

package com.canglang.etc.gpsm.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import com.opensymphony.xwork2.ActionContext;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class ToWordDocumentAction {
    
       public void exportObj() { 
            Configuration configuration = new Configuration();  
            configuration.setDefaultEncoding("utf-8");  
            Map<String,Object> dataMap=new HashMap<String,Object>();  
            dataMap.put("xitongmingcheng", "对应数据");
            dataMap.put("xitongbianhao", 1);
            dataMap.put("yewumiaoshu", "对应数据");
            dataMap.put("fuwufanwei", "对应数据");
            dataMap.put("dengjipingcedanweimingcheng", "对应数据");
            dataMap.put("heshitouruyunxinshiyong", "对应数据");
            dataMap.put("shangjixitongmingcheng", "对应数据");
            dataMap.put("shangjixitongsuoshudanweimingcheng", "对应数据");
            configuration.setClassForTemplateLoading(this.getClass(), "/com/canglang/etc/template");  //FTL文件所存在的位置  
            Template t = null;  
            try {  
                t = configuration.getTemplate("workModel.ftl"); //文件名
                t.setEncoding("utf-8");  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
            String fileName = systemBasicInformation.getSystemName() + ".doc";  
              
            ActionContext ctx = ActionContext.getContext();  
            HttpServletResponse response = (HttpServletResponse) ctx  
                    .get("com.opensymphony.xwork2.dispatcher.HttpServletResponse");  
            response.setContentType("application/msword");  
            try {  
                response.addHeader("Content-Disposition", "attachment; filename="  
                        + URLEncoder.encode(fileName, "UTF-8"));  
      
                response.setCharacterEncoding("utf-8");  
                PrintWriter out = response.getWriter();  
                t.process(dataMap, out);  
                out.close();  
            } catch (UnsupportedEncodingException e) {  
                e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            } catch (TemplateException e) {  
                e.printStackTrace();  
            }  
      
        }
       
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值