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");
}
分享到:
相关推荐
本篇文章将聚焦于如何使用Java结合`RTFTemplate.dot`模板生成Word文件。 首先,`RTFTemplate.dot`是一个Microsoft Word的模板文件,它以RTF(Rich Text Format)格式存储,通常包含预定义的文本格式、样式和占位符...
通过rtftemplate,可以确保生成的RTF文件在Word中打开时保留原始模板的格式和内容。这对于需要在不支持RTF格式的环境中分享文档,或者希望用户在Word中进一步编辑文档的情况非常实用。 在实际应用中,rtftemplate-...
这可能涉及到编程语言(如Python、Java或C#)中的库和API,例如Microsoft的Open XML SDK或开源的Apache POI,它们可以处理RTF文件的生成和解析。 总的来说,"rtfTemplate"相关的知识涵盖了RTF文件格式的原理、模板...
RTFTemplate是其中一个专门处理RTF文档的工具,它允许开发者通过Java代码动态生成格式化的文本报告。本文将详细介绍RTFTemplate的初步使用,包括其工作原理、依赖库以及实际应用。 首先,我们来看一下RTFTemplate的...
RTFTemplate 是 RTF 引擎(RTF 到 RTF),它通过将模板 RTF(模型 RTF)与 JAVA 对象(上下文)合并来生成 RTF。 RTFTemplate 使用 Velocity 将模板与对象合并。 可以使用 MERGEFIELD 和 BOO 使用 MS Word 设计 RTF ...
1. `freemarker-2.3.8.jar` - 这是FreeMarker模板引擎的库,它是一个强大的、轻量级的、基于Java的模板语言,用于生成任何类型的文本输出,包括HTML、XML、Word文档等。FreeMarker与应用程序逻辑分离,使得模板设计...