一:用freemaker生成word
1、用word 编辑好模板
A、普通字符串替换为${String}
如图:
B、表格循环用标签
<#list list as s>
上报人:${s.reportName} , 性别:${user.sex}
</#list>
2、将word格式另存为xml3、将xml模板文件后缀改成ftl
4、java代码
public class WordUtil {
/**
* @Desc:生成word文件
* @Author:ZQQ
* @Date:2017-12-06
* @param dataMap word中需要展示的动态数据,用map集合来保存
* @param templateName word模板名称,例如:test.ftl
* @param filePath 文件生成的目标路径,例如:桌面
*/
public static void createWord(Map dataMap,String templateName,String filePath,String fileName){
try{
//创建配置实例
Configuration configuration = new Configuration();
//设置编码
configuration.setDefaultEncoding("UTF-8");
//ftl模板文件统一放至 /com/sinosoft/yjcz/web/template/包下面
configuration.setClassForTemplateLoading(WordUtil.class,"/com/sinosoft/yjcz/web/template/");
//获取模板
Template template = configuration.getTemplate(templateName);
//输出文件
File outFile = new File(filePath+File.separator+fileName);
//如果输出目标文件夹不存在,则创建
if (!outFile.getParentFile().exists()){
outFile.getParentFile().mkdirs();
}
//将模板和数据模型合并生成文件
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"UTF-8"));
//生成文件
template.process(dataMap, out);
//关闭流
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private String filePath; //文件路径
private String fileName; //文件名称
/**
* 生成word文档
*〈简述〉
*〈详细描述〉
* @author zqq
* @param yjEreport
* @param yjReportIssued
* @return
*/
private String createWord(YjEreportEntity yjEreport,YjReportIssuedEntity yjReportIssued){
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String format = sdf.format(new Date());
/** 用于组装word页面需要的数据 */
Map<String, Object> dataMap = new HashMap<String, Object>();
//查询上报单位
String reportUnit = yjEreportService.getUnitById(yjReportIssued.getUnitId());
log.info("上报单位为==="+reportUnit);
//设置文本内容
String date =sdf.format(yjEreport.getPlaceTime());
String year =date.substring(0,4)+"年";
String month =date.substring(4,6)+"月";
String day =date.substring(6,8)+"日";
//查询上报相关人员
String memberId = yjReportIssued.getMemberId();
List<YjUnitMemberEntity> list = yjReportIssuedServiceI.findHql("From YjUnitMemberEntity as ch where ch.id = ?" , memberId);
//查询联系人和联系方式
String memberName= "";
String rePhone ="";
for (YjUnitMemberEntity yjUnitMemberEntity : list) {
memberName = list.get(0).getMemberName();
rePhone = list.get(0).getMemberPhone();
log.info("上报联系人和联系方式为"+memberName+"===="+rePhone);
}
date = year + month + day;
//正文内容
String content =date+yjEreport.getRemark();
// 组装数据 添加标题 上报单位 正文 日期 联系人 联系方式
dataMap.put("title", yjEreport.getTitle());
dataMap.put("reportUnit", reportUnit);
dataMap.put("date", date);
dataMap.put("content", content);
dataMap.put("memberName", memberName);
dataMap.put("rePhone", rePhone);
//文件路径 下载到桌面
String filePath = FileSystemView.getFileSystemView().getHomeDirectory().getPath();//这便是读取桌面路径的方法了
//文件名称
String fileName = yjEreport.getTitle()+format+".doc";
/** 生成word */
WordUtil.createWord(dataMap, "news.ftl", filePath, fileName);
return "createWordSuccess";
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
二:用POI生成word
/**
*
*〈简述〉 下载word文档
*〈详细描述〉
* @author zqq
* @param yjEreport
* @throws Exception
*/
public void exportSimpleWord(YjEreportEntity yjEreport,YjReportIssuedEntity yjReportIssued) throws Exception{
//Blank Document
XWPFDocument document= new XWPFDocument();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String format = sdf.format(new Date());
String path = yjEreport.getTitle()+format;
//Write the Document in file system
FileOutputStream out = new FileOutputStream(new File(path+".doc"));
//添加标题
XWPFParagraph titleParagraph = document.createParagraph();
//设置段落居中
titleParagraph.setAlignment(ParagraphAlignment.CENTER);
XWPFRun titleParagraphRun = titleParagraph.createRun();
titleParagraphRun.setText("关于"+yjEreport.getTitle()+"的报告");
titleParagraphRun.setFontSize(22);
titleParagraphRun.setFontFamily("宋体");
//上报单位
String unitId = yjReportIssued.getUnitId();
//查询上报单位
String reportUnit = yjEreportService.getUnitById(unitId);
log.info("上报单位为==="+reportUnit);
//查询上报相关人员
String memberId = yjReportIssued.getMemberId();
List<YjUnitMemberEntity> list = yjReportIssuedServiceI.findHql("From YjUnitMemberEntity as ch where ch.id = ?" , memberId);
//查询联系人和联系方式
String memberName= "";
String rePhone ="";
for (YjUnitMemberEntity yjUnitMemberEntity : list) {
memberName = list.get(0).getMemberName();
rePhone = list.get(0).getMemberPhone();
log.info("上报联系人和联系方式为"+memberName+"===="+rePhone);
}
//标题下的上报单位
XWPFParagraph titleParagraphFu = document.createParagraph();
XWPFRun run2 = titleParagraphFu.createRun();
run2.setText(reportUnit+":"+"\r");
run2.setFontFamily("仿宋");
run2.setFontSize(16);
//段落 正文
XWPFParagraph firstParagraph = document.createParagraph();
//设置文本内容
String date =sdf.format(yjEreport.getPlaceTime());
String year =date.substring(0,4)+"年";
String month =date.substring(4,6)+"月";
String day =date.substring(6,8)+"日";
date = year + month + day;
//正文内容
String content =date+yjEreport.getRemark();
//首行缩进
firstParagraph.setIndentationFirstLine(800);
XWPFRun r0=firstParagraph.createRun();//p1.createRun()将一个新运行追加到这一段
r0.setText(content);
r0.setFontFamily("仿宋");
r0.setFontSize(16);
XWPFParagraph firstParagraph1 = document.createParagraph();
//向右缩进
firstParagraph1.setIndentationFirstLine(6200);
XWPFRun r1=firstParagraph1.createRun();//p1.createRun()将一个新运行追加到这一段
//年月日
r1.setFontFamily("仿宋");
r1.setFontSize(16);
r1.setText("北京市外办");
XWPFParagraph firstParagraph2 = document.createParagraph();
//向右缩进
firstParagraph2.setAlignment(ParagraphAlignment.RIGHT);
XWPFRun r2=firstParagraph2.createRun();//p1.createRun()将一个新运行追加到这一段
r2.setText(date);
r2.setFontFamily("仿宋");
r2.setFontSize(16);
XWPFParagraph firstParagraph3 = document.createParagraph();
XWPFRun r3=firstParagraph3.createRun();//p1.createRun()将一个新运行追加到这一段
//首行缩进
firstParagraph3.setIndentationFirstLine(800);
//联系方式
String name ="(联系人:" +memberName+";"+"联系方式:"+rePhone+")";
r3.setText(name);
r3.setFontFamily("仿宋");
r3.setFontSize(16);
document.write(out);
out.close();
log.info("create_table document written success.");
}