|
|
@@ -1,16 +1,30 @@
|
|
|
package com.common.workflow.service.util;
|
|
|
|
|
|
+import com.common.workflow.service.dto.CustomXWPFDocument;
|
|
|
+import com.common.workflow.service.dto.OfficeUtil;
|
|
|
+import com.common.workflow.service.dto.SegmentData;
|
|
|
+import com.common.workflow.service.dto.StoryData;
|
|
|
import com.deepoove.poi.XWPFTemplate;
|
|
|
import com.deepoove.poi.config.Configure;
|
|
|
+import com.deepoove.poi.data.DocxRenderData;
|
|
|
+import org.apache.poi.poifs.filesystem.DirectoryEntry;
|
|
|
+import org.apache.poi.poifs.filesystem.DocumentEntry;
|
|
|
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
|
|
+import org.jsoup.Jsoup;
|
|
|
+import org.jsoup.nodes.Document;
|
|
|
+import org.jsoup.nodes.Element;
|
|
|
+import org.jsoup.select.Elements;
|
|
|
+import org.springframework.util.ResourceUtils;
|
|
|
+import org.springframework.web.util.HtmlUtils;
|
|
|
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
-import java.io.File;
|
|
|
+import java.io.*;
|
|
|
import java.lang.reflect.Field;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* Created by gyue on 2019-03-18.
|
|
|
*/
|
|
|
@@ -48,6 +62,113 @@ public class WordTemplate {
|
|
|
return template;
|
|
|
}
|
|
|
|
|
|
+ public XWPFTemplate exportDocWordTemplate (ArrayList<String> contentList, String recordName,String templateUrl, String fileName,
|
|
|
+ String detailTemplateUrl, String detailFileName) throws Exception {
|
|
|
+
|
|
|
+ String fullFileName = FileUtils.getExcelTemplatePath() + "/" + fileName;
|
|
|
+ File file = new File(fullFileName);
|
|
|
+ if (file.exists()) {
|
|
|
+ file.delete(); //清除历史文件
|
|
|
+ }
|
|
|
+ fullFileName = FileUtils.downLoadFromUrl(templateUrl, fileName, FileUtils.getExcelTemplatePath());
|
|
|
+
|
|
|
+ String detailfullFileName = FileUtils.getExcelTemplatePath() + "/" + detailFileName;
|
|
|
+ File detailfile = new File(detailfullFileName);
|
|
|
+ if (detailfile.exists()) {
|
|
|
+ detailfile.delete(); //清除历史文件
|
|
|
+ }
|
|
|
+ detailfullFileName = FileUtils.downLoadFromUrl(detailTemplateUrl, detailFileName, FileUtils.getExcelTemplatePath());
|
|
|
+
|
|
|
+ StoryData data = new StoryData();
|
|
|
+ data.setStoryName(recordName);
|
|
|
+ List<SegmentData> segments = new ArrayList<SegmentData>();
|
|
|
+
|
|
|
+ for (String content : contentList) {
|
|
|
+ SegmentData s1 = new SegmentData();
|
|
|
+ content = HtmlUtils.htmlUnescape(content);
|
|
|
+ s1.setContent(content);
|
|
|
+ segments.add(s1);
|
|
|
+ }
|
|
|
+
|
|
|
+ DocxRenderData segment = new DocxRenderData(new File(detailfullFileName), segments );
|
|
|
+
|
|
|
+ data.setSegment(segment);
|
|
|
+
|
|
|
+ XWPFTemplate template = XWPFTemplate.compile(fullFileName).render(data);
|
|
|
+
|
|
|
+ return template;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 导出富文本框
|
|
|
+ public XWPFTemplate exportDocWordTemplate_1 (ArrayList<String> contentList, String recordName,String templateUrl, String fileName,
|
|
|
+ String detailTemplateUrl, String detailFileName) throws Exception {
|
|
|
+ Map<String, Object> param = new HashMap<String, Object>();
|
|
|
+ String fullFileName = FileUtils.getExcelTemplatePath() + "/" + fileName;
|
|
|
+ File file = new File(fullFileName);
|
|
|
+ if (file.exists()) {
|
|
|
+ file.delete(); //清除历史文件
|
|
|
+ }
|
|
|
+ fullFileName = FileUtils.downLoadFromUrl(templateUrl, fileName, FileUtils.getExcelTemplatePath());
|
|
|
+ String content = "";
|
|
|
+ for (String str : contentList) {
|
|
|
+ content += str;
|
|
|
+ }
|
|
|
+// content = HtmlUtils.htmlUnescape(contentList.get(5));
|
|
|
+// List<HashMap<String, String>> imgs = getImgStr(content);
|
|
|
+// int count = 0;
|
|
|
+// for (HashMap<String, String> img : imgs) {
|
|
|
+// count++;
|
|
|
+// //处理替换以“/>”结尾的img标签
|
|
|
+// content = content.replace(img.get("img"), "${imgReplace" + count + "}");
|
|
|
+// //处理替换以“>”结尾的img标签
|
|
|
+// content = content.replace(img.get("img1"), "${imgReplace" + count + "}");
|
|
|
+// Map<String, Object> header = new HashMap<String, Object>();
|
|
|
+//
|
|
|
+// try {
|
|
|
+// File filePath = new File(ResourceUtils.getURL("classpath:").getPath());
|
|
|
+// String imagePath = filePath.getAbsolutePath() + "\\static\\";
|
|
|
+// imagePath += img.get("src").replaceAll("/", "\\\\");
|
|
|
+// //如果没有宽高属性,默认设置为400*300
|
|
|
+// if(img.get("width") == null || img.get("height") == null) {
|
|
|
+// header.put("width", 400);
|
|
|
+// header.put("height", 300);
|
|
|
+// }else {
|
|
|
+// header.put("width", (int) (Double.parseDouble("400")));
|
|
|
+// header.put("height", (int) (Double.parseDouble("300")));
|
|
|
+// }
|
|
|
+// header.put("type", "jpg");
|
|
|
+// header.put("content", OfficeUtil.inputStream2ByteArray(new FileInputStream(imagePath), true));
|
|
|
+// } catch (FileNotFoundException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// param.put("${imgReplace" + count + "}", header);
|
|
|
+// }
|
|
|
+ try {
|
|
|
+ // 生成doc格式的word文档,需要手动改为docx
|
|
|
+ byte by[] = content.getBytes("UTF-8");
|
|
|
+ ByteArrayInputStream bais = new ByteArrayInputStream(by);
|
|
|
+ POIFSFileSystem poifs = new POIFSFileSystem();
|
|
|
+ DirectoryEntry directory = poifs.getRoot();
|
|
|
+// DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
|
|
|
+// FileOutputStream ostream = new FileOutputStream("D:\\excel_templates\\temp.doc");
|
|
|
+// poifs.writeFilesystem(ostream);
|
|
|
+// bais.close();
|
|
|
+// ostream.close();
|
|
|
+ XWPFTemplate template = XWPFTemplate.compile(fullFileName).render(bais);
|
|
|
+ return template;
|
|
|
+
|
|
|
+// // 临时文件(手动改好的docx文件)
|
|
|
+// CustomXWPFDocument doc = OfficeUtil.generateWord(param, "D:\\excel_templates\\temp.docx");
|
|
|
+// //最终生成的带图片的word文件
|
|
|
+// FileOutputStream fopts = new FileOutputStream("D:\\excel_templates\\final.docx");
|
|
|
+// doc.write(fopts);
|
|
|
+// fopts.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public Map<String, String> objectToMap(Object obj) throws IllegalAccessException {
|
|
|
Map<String, String> map = new HashMap<>();
|
|
|
Class<?> clazz = obj.getClass();
|
|
|
@@ -83,4 +204,28 @@ public class WordTemplate {
|
|
|
}
|
|
|
return map;
|
|
|
}
|
|
|
+
|
|
|
+ //获取html中的图片元素信息
|
|
|
+ public static List<HashMap<String, String>> getImgStr(String htmlStr) {
|
|
|
+ List<HashMap<String, String>> pics = new ArrayList<HashMap<String, String>>();
|
|
|
+
|
|
|
+ Document doc = Jsoup.parse(htmlStr);
|
|
|
+ Elements imgs = doc.select("img");
|
|
|
+ for (Element img : imgs) {
|
|
|
+ HashMap<String, String> map = new HashMap<String, String>();
|
|
|
+ if(!"".equals(img.attr("width"))) {
|
|
|
+ map.put("width", img.attr("width").substring(0, img.attr("width").length() - 2));
|
|
|
+ }
|
|
|
+ if(!"".equals(img.attr("height"))) {
|
|
|
+ map.put("height", img.attr("height").substring(0, img.attr("height").length() - 2));
|
|
|
+ }
|
|
|
+ map.put("img", img.toString().substring(0, img.toString().length() - 1) + "/>");
|
|
|
+ map.put("img1", img.toString());
|
|
|
+ map.put("src", img.attr("src"));
|
|
|
+ pics.add(map);
|
|
|
+ }
|
|
|
+ return pics;
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
+
|