package com.esunny.zcq;
import com.esunny.zcq.entity.ReportDocElementEntity;
import com.esunny.zcq.entity.ReportDocExportEntity;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import sun.misc.BASE64Encoder;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DocumentHandlerTwo {
private Configuration configuration = null;
public DocumentHandlerTwo() {
configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");// 设置默认编码方式
}
/**
* !注意dataMap里存放的数据Key值要与模板中的参数相对应,图片为String类型的Base64编码
*
* @param dataMap 载入的数据文件
* @param templatePackagePath 模版文件包路径
* @param templateName 模版文件名称
* @param docPath 写出的doc文件路径
*/
public void createDoc(Map<String, Object> dataMap, String templatePackagePath, String templateName,
String docPath) {
// 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载,
// // 这里我们的模板是放在com.havenliu.document.template包下面
configuration.setClassForTemplateLoading(this.getClass(), templatePackagePath);
Template t = null;
try {
t = configuration.getTemplate(templateName, "utf-8");
} catch (IOException e) {
e.printStackTrace();
}
// 输出文档路径及名称
File outFile = new File(docPath);
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
} catch (Exception e1) {
e1.printStackTrace();
}
try {
t.process(dataMap, out);
out.flush();
out.close();
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private String getImageStr(String imgFile) {
InputStream in = null;
byte[] data = null;
try {
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
}
/**
* 测试
*/
public static void main(String[] args) {
DocumentHandlerTwo documentHandler = new DocumentHandlerTwo();
Map<String, Object> dataMap = new HashMap<String, Object>();
// dataMap.put("pic", documentHandler.getImageStr("D:\\avater.png"));
// dataMap.put("TitleLevelOne", "已婚");
// dataMap.put("TitleLevelTwo", "群众");
//组装数据
ReportDocExportEntity reportDocExportEntity = new ReportDocExportEntity();
reportDocExportEntity.setTitleLevelOne("一级标题测试");
List<ReportDocElementEntity> reportDocElementEntities = new ArrayList<>();
for (int i = 0; i < 3; i++) {
ReportDocElementEntity reportDocElementEntity = new ReportDocElementEntity();
reportDocElementEntity.setTitleLevelTwo("二级副标题序号" + i);
List<String> columnList = new ArrayList<>();
for (int j = 0; j < 4; j++) {
columnList.add("列项目" + i + "行" + j + "列");
}
reportDocElementEntity.setColumnList(columnList);
reportDocElementEntity.setColumnNum(columnList.size());
if (i == 0) {
reportDocElementEntity.setPicLevelTwo(documentHandler.getImageStr("D:\\avater.png"));
}
if (i == 1) {
reportDocElementEntity.setPicLevelTwo(documentHandler.getImageStr("D:\\shit.png"));
}
if (i == 2) {
reportDocElementEntity.setPicLevelTwo(documentHandler.getImageStr("D:\\shit.png"));
}
List<List<String>> entityList = new ArrayList<>();
//设置每一行的数据
for (int m = 0; m < 4; m++) {
List<String> lineData = new ArrayList<>();
lineData.add("data1" + "行:" + m);
lineData.add("data2" + "行:" + m);
lineData.add("data3" + "行:" + m);
lineData.add("data3" + "行:" + m);
entityList.add(lineData);
}
reportDocElementEntity.setEntityList(entityList);
Integer picNameSubstitute = i + 5;
reportDocElementEntity.setPicStringName("rId" + picNameSubstitute);
//加入列表中
reportDocElementEntities.add(reportDocElementEntity);
}
reportDocExportEntity.setReportElementsList(reportDocElementEntities);
reportDocExportEntity.setElementNum(reportDocElementEntities.size());
//构建reportDocExportEntity的relationShip数据列表
List<String> relationsStringList = new ArrayList<>();
Integer theme1Num = reportDocExportEntity.getElementNum() + 6;
String theme1 = "<Relationship Id=\"rId" + theme1Num + "\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme\"\n" +
" Target=\"theme/theme1.xml\"/>";
relationsStringList.add(theme1);
Integer fontTableNum = reportDocExportEntity.getElementNum() + 5;
String fontTable = "<Relationship Id=\"rId" + fontTableNum + "\"\n" +
" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable\"\n" +
" Target=\"fontTable.xml\"/>";
relationsStringList.add(fontTable);
for (int j = reportDocExportEntity.getElementNum(); j > 0; j--) {
int picNum = j + 4;
int picSerial = j;
String image = "<Relationship Id=\"rId" + picNum + "\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image\"\n" +
" Target=\"media/image" + picSerial + ".png\"/>";
relationsStringList.add(image);
}
reportDocExportEntity.setRelationshipsList(relationsStringList);
//设置原图片数据
StringBuilder picDataStringBuilder = new StringBuilder();
for (int k = 0; k < reportDocExportEntity.getElementNum(); k++) {
int picNum = k + 1;
ReportDocElementEntity reportDocElementEntity = reportDocExportEntity.getReportElementsList().get(k);
String picDataEntry = "<pkg:part pkg:name=\"/word/media/image"+ picNum +".png\" pkg:contentType=\"image/png\" pkg:compression=\"store\">\n" +
" <pkg:binaryData>" + reportDocElementEntity.getPicLevelTwo() +"</pkg:binaryData>\n" +
" </pkg:part>";
picDataStringBuilder.append(picDataEntry);
}
reportDocExportEntity.setPicDataList(picDataStringBuilder.toString());
dataMap.put("root", reportDocExportEntity);
documentHandler.createDoc(dataMap, "/com/template", "oyt_generete_new.ftl", "D:/outhello.doc");
System.out.println("生成完毕");
}
}
- 1
- 2
前往页