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");
}