生成word的两种方法 freemaker和POI
一:用freemaker生成word
1、用word 编辑好模板
A、普通字符串替换为${String}
如图:
B、表格循环用标签
<#list list as s>
上报人:${s.reportName} , 性别:${user.sex}
</#list>
2、将word格式另存为xml
3、将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.");
}
生成word的两种方法 freemaker和POI相关推荐
- 在word文档中如何自动生成目录,两种方法制作目录,总有一种适合你
在word文档中如何自动生成目录,两种方法制作目录,总有一种适合你 目录 在word文档中如何自动生成目录,两种方法制作目录,总有一种适合你 1.文章中的标题较多,每个单独调整格式太费劲,这里我们用一 ...
- 用word2007自动生成目录的两种方法
用word2007自动生成目录的两种方法 看到很多人还在手工编写word的目录,感觉很浪费时间,下面跟大家分享一下,在word2007及word ...
- C++生成GUID的两种方法
C++生成GUID的两种方法 C++生成GUID的两种方法 使用CoCreateGuid函数 使用Boost库 C++生成GUID的两种方法 GUID是软件开发中常用的组件,用于生成唯一的对象,在C# ...
- php 生成excel表格,PHP 生成Excel表格两种方法
1.使用PHP header()函数 (向浏览器输出Excel,即通过浏览器方式,下载生成的excel) 实例: /** * PHP 使用heade()函数生成excel表格 ExcelName为 ...
- python使用源文件_使用Python生成源文件的两种方法
利用Python的字符串处理模块,开发人员可以编写脚本用来生成那些格式相同的C.C++.JAVA源程序.头文件和测试文件,从而避免大量的重复工作.本文概述两种利用Python string类生成jav ...
- python pdf转word的两种方法
第一种方法:多线程处理 优点:可以同时处理多个文件,速度非常快. 缺点:只能识别文字信息,对于图片等非文本内容无法识别:另外,pdf文档中避免出现 '\n' 等转义字符,否则可能会出现一些识别歧义. ...
- hive sql自动生成id的两种方法随机id和自增id
文章目录 1.生成自增id:row number() over(order by) 2.随机不重复id: regexp_replace(reflect('java.util.UUID','random ...
- html文件从手机里打开方式,在手机上打开word的两种方法
在手机上看word文档其实非常简单,你不仅可以看,还可以修改和写word文档,那么下面就由学习啦小编给大家分享下在手机上打开word的技巧,希望能帮助到您. 在手机上打开word方法一 1进入百度应用 ...
- 自动生成testbench的两种方法
方法一: Quartus,Processing->Start->Start Teat Bench Template Write, 选择了之后会在工程的simulation/modelsim ...
最新文章
- 计算机视觉算法与应用清华大学,计算机视觉-清华大学.ppt
- Solr集群搭建,zookeeper集群搭建,Solr分片管理,Solr集群下的DataImport,分词配置。
- POJ 3225 线段树+lazy标记
- Delphi面向对象学习随笔七:COM
- 流程图伪代码计算机语言,流程图与伪代码 PPT课件
- .net winfrom 定义全局快捷键!
- Oracle 10.2.0.4 升级到 10.2.0.5
- Winpcap笔记4之不用回调函数捕获数据包
- jQuery源码分析一
- 基于AT89C51的多层电梯控制系统
- java类加载器说法正确的_Java中的类加载器
- 租用游艇,Clear And Present Danger S,Heat Wave G,单源最短路径(弱化版)
- 4.1图像分割之区域生长法
- c语言已知祖父年龄70,实验三循环结构程序设计.doc
- 获取微信用户openid
- 手机/平板上如何进行网页过滤
- [原创].NET中防止Access数据库下载
- 淘宝运营 钻展、智钻的定义 优势所在
- .gitignore文件 —— git忽略文件
- Elasticsearch-7.8.0源码编译