java导出word文档

4 篇文章 0 订阅
2 篇文章 0 订阅

        在项目中有的时候需要导出文件,一般情况下都是Excel文件。但是有时候也是需要导出word文件。今天我就根据以前的项目,修改的导出word文件的方法。

一、设置word文件模板

       新建一个word文档,设置好需要导出的文件的格式。将文件后缀修改为: .ftl;将需要填入数据的地方修改为:${station} 格式;

{ }中的字段不是乱设的,后面有用到。

二、获取数据后,将数据按模板的字段设置到map中

    Service代码:

public Map<String,Object> exportFile(String id){
        TroublesDao dao = new TroublesDaoImpl();
        TroublePrintingBean bean = dao.exportFile(id);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("station" ,bean.getStation_name());
        map.put("reporter" ,bean.getReporter());
        map.put("equipment" , bean.getEqu_name());
        map.put("phone" , bean.getPhone());
        map.put("report_time" , bean.getReport_time());
        map.put("happen_time" , bean.getTrouble_happen_time());
        map.put("title" , bean.getTheme());
        map.put("msg" , bean.getLarge()+"-"+bean.getSmall()); //故障点基本信息格式:故障大类 - 故障小类
        map.put("level" , bean.getTrouble_number());
        String LatestTime = "";//最后解决时间
        try {
            String report_time = bean.getReport_time();   //上报时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse(report_time); //将String时间格式转换成Date格式
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);//设置起时间
            cal.add(Calendar.DATE, 3);//时间增加3天;最后上报时间格式为:上报时间+故障点时效(72小时,即3天)
            LatestTime = sdf.format(cal.getTime());//最后解决时间
        } catch (ParseException e) {
            e.printStackTrace();
        }
        map.put("last_finish_time" , LatestTime);
        map.put("remark" , bean.getTrouble_describe());
        //下面两个字段虽然是空的,但是不能删。应为ftl模板文件中给了他们占位符,删除了会在导出文件时出异常。以后需要有数据时替换空字符串就好
        map.put("analysis" , "");//故障分析
        map.put("deal_way" , "");//解决方法
        return map;
    }

三、用IO流输出

     Controller代码:

/**
     * 导出
     * return 0:导出成功;1:导出异常
     */
    @Override
    @RequestMapping("/exportFile")
    @ResponseBody
    public String exportFile(HttpServletRequest request){
        String id = request.getParameter("id");
        Map<String,Object> map= new TroublesServiceImpl().exportFile(id);
        Configuration configuration = new Configuration();
        configuration.setDefaultEncoding("UTF-8");
       String path =  request.getSession().getServletContext().getRealPath("/files");//获取webroot文件下的files文件夹路径
        Template t = null;
        try {
            configuration.setDirectoryForTemplateLoading(new File(path));  //FTL文件所存在的位置
            t = configuration.getTemplate( File.separator + "trouble_model.ftl","UTF-8"); //文件名;File.separator:获取当前系统的路径斜杠
            File outFile = new File(path +File.separator + map.get("station")+"-"+map.get("title")+".doc");//设置导出的文件路径;导出的文件名为:油站名-故障主题
            FileOutputStream fos = new FileOutputStream(outFile);
            OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
            Writer out = new BufferedWriter(osw);
            t.process(map, out);
            if (fos != null){
                fos.close();
            }
            if(osw != null){
                osw.close();
            }
            if(out != null){
                out.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "1";
        }
        return "0";
    }

开发时遇到的BUG一:

        在导出文件后,打开文件时,报:文件被java …… 占用,无法操作;引起这种原因是输出流没有关闭,一直占着这个文件,所以无法打开。

       解决方法:关闭流;

开发时遇到的BUG二:

      导出文件后,打开文件时,报:根据结构,XML数据无效;

      

      解决方法:

      在下面的代码中,都要加上UTF-8;

configuration.setDefaultEncoding("UTF-8");
t = configuration.getTemplate( File.separator + "trouble_model.ftl","UTF-8");
OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值