RtfTemplate实现Java生成word

Struts实现生成word的方法Action代码

实现步骤:RTFTemplate.dot生成rtf模板 ---建立*fields.xml文件并写入相应的属性---在生成rtf中插入属性标识---调用action生成word.

/**
     * 生成RTF文档
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @throws Exception
     */
    public ActionForward generateRTF(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 收集数据
        String cID = request.getParameter("cID");
        Weightlist weightlist = weightlistManager.get(cID);
        GetEng getEng = new GetEng();
        DateUtil dateUtil = new DateUtil();
        DecimalFormat   df2   =   new   DecimalFormat("#0.##");
        DecimalFormat   df1   =   new   DecimalFormat("#0");
        DecimalFormat   df3   =   new   DecimalFormat("#0.00#");
        SimpleDateFormat timedf =new SimpleDateFormat("yyyy-MM-dd"); 
        String eng = getEng.getEng(Integer.valueOf(weightlist.getTboxes()));
        List<Weightlistdetail> list = (List<Weightlistdetail>)weightlistdetailManager.find("from Weightlistdetail w where w.weightlist.id=? order by w.sortno",cID);
        Map<String, Object> contextMap = new HashMap<String, Object>();
       
        String tboxes = "";
        if("1".equals(weightlist.getIspallet())){
            tboxes =  df1.format(Float.parseFloat(weightlist.getTboxes())) + "PALLETS";
        }else{
            tboxes =  df1.format(Float.parseFloat(weightlist.getTboxes())) + "CTNS";
        }
       
        String tgross =  df2.format(Float.parseFloat(weightlist.getTgross())) + "KGS";
        String tnet =  df2.format(Float.parseFloat(weightlist.getTnet())) + "KGS";
       
        if("1".equals(weightlist.getIspallet())){
            contextMap.put("pallet", "PALLETS");
        }else{
            contextMap.put("pallet", "CTNS");
        }
       
        contextMap.put("icode", weightlist.getIcode());
        contextMap.put("buyer", weightlist.getBuyer());
        contextMap.put("rcode", weightlist.getRcode());
        contextMap.put("tboxes", tboxes);
        contextMap.put("tgross", tgross);
        contextMap.put("tnet", tnet);
        String date =  "";
        if(!"".equals(weightlist.getDate())&&weightlist.getDate()!=null){
            date = dateUtil.getUSAdate(timedf.format(weightlist.getDate()));
        }
        contextMap.put("date", date);
        contextMap.put("eng", eng);
        contextMap.put("sng", weightlist.getTboxes());
      
        //WORD中插入图片
        Project project = new Project("Jakarta Velocity");
        String image = "";
        try {
            if("Y".equals(weightlist.getIsstamp())){//是否生成章印
                if("lj".equals(weightlist.getCompany())){//选择章印
                    image = request.getSession().getServletContext().getRealPath("\\styles\\images\\lj_stamp.png");
                }else{
                    image = request.getSession().getServletContext().getRealPath("\\styles\\images\\lg_stamp.png");
                }
                final InputStream in = new FileInputStream(image);
                project.setLogo(in);
            }
         } catch (Exception e) {
             System.out.println(e.getStackTrace());
         }
        contextMap.put("project", project);
       
        List w = new ArrayList();
        String type = "";
        for(Weightlistdetail wd : list){
            WeightlistdetailVO wVO = new WeightlistdetailVO();
                wVO.setId(wd.getId());
                wVO.setBgross(df2.format(Float.parseFloat(wd.getBgross())));
                wVO.setBnet(df2.format(Float.parseFloat(wd.getBnet())));
                wVO.setBox(df2.format(wd.getBox()));
                wVO.setBoxpcs(df2.format(wd.getBoxpcs()));
                wVO.setHeight("");
                wVO.setLength("");
                wVO.setName(wd.getName());
                wVO.setTbgross((wd.getTgross()!=null?wd.getTgross():0).toString());
                wVO.setTbnet((wd.getTnet()!=null?wd.getTnet():0).toString());
                wVO.setType("");
                wVO.setVolume("");
                wVO.setWidth("");
                w.add(wVO);
        }
        contextMap.put("w", w);
        //文档目
        String rtfSource = request.getSession().getServletContext().getRealPath("\\word\\weight.rtf") ;
        String rtfTargetPath = request.getSession().getServletContext().getRealPath("\\word") ;;
        String rtfname = "\\weight_" + DateFormatUtils.formatDateTime(new Date(), "yyyyMMddHHmmssSSS") + ".rtf";
        String rtfTarget = rtfTargetPath + rtfname;


        // 生成RTF
        RTFGenerator generator = new RTFGenerator();
        generator.setContextMap(contextMap);
        generator.run(rtfSource, rtfTarget);


        // 下载生成的rtf
        request.setAttribute("filePath", rtfTarget);
        request.setAttribute("fileName", "weightlist.doc");
        request.setAttribute("contentType", ".doc");
        return new ActionForward("/download.do");
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值