1、根据需求创建word模板(书签)
2、后台代码
public void generaFile(String dataId) {
Document document = null;
try {
// 创建临时文件作
String tempFile = "";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
String fileName = "zlmbjgzjh:"+dateFormat.format(new Date());
String filePath = AppUtil.getRealPath("/template/") + fileName
+ ".doc";
FileOperator.createFile(filePath);//创建文件夹
tempFile = AppUtil.getRealPath("/template/doc/zlmbjgzjh.doc");//得到模板
// 开始写入数据到部门质量目标计划
document = new Document(tempFile);
//部门分解主表信息
List<Map<String, Object>> list = qoalJobDao
.getDataInfo(dataId);
Date date = new Date();
if (BMFJ.size() > 0) {
//主表替换数据
replaceMajorFormBookMark(document,list);
}
// 生成Word
document.save(filePath);
} catch (Exception e) {
e.printStackTrace();
}
}
private void replaceMajorFormBookMark(Document document,
List<Map<String, Object>> list) {
try {
Iterator<Bookmark> bookmarks = document.getRange().getBookmarks()
.iterator();
while (bookmarks.hasNext()) {
Bookmark bookmark = bookmarks.next();
String bookName = bookmark.getName();
if (bookName.startsWith("PO_FORM")) {
String fieldName = "";
// 年度
if (bookName.contains("PO_FORM_ND")) {
String fieldValue = CommonTools.Obj2String(bMFJ.get(0)
.get("F_nd"));
fieldValue = StringUtil.isNotEmpty(fieldValue) ? fieldValue.substring(0, 4) : "";
bookmark.setText(fieldValue);
}
//部门
if (bookName.contains("PO_FORM_DEPT")) {
String fieldValue = CommonTools.Obj2String(bMFJ.get(0)
.get("F_bzbm"));
bookmark.setText(fieldValue);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}