|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+package com.ruoyi.common.utils;
|
|
|
2
|
+
|
|
|
3
|
+
|
|
|
4
|
+import com.deepoove.poi.XWPFTemplate;
|
|
|
5
|
+import com.deepoove.poi.config.Configure;
|
|
|
6
|
+import com.deepoove.poi.data.*;
|
|
|
7
|
+import com.deepoove.poi.data.style.ParagraphStyle;
|
|
|
8
|
+import com.deepoove.poi.data.style.Style;
|
|
|
9
|
+import com.deepoove.poi.util.PoitlIOUtils;
|
|
|
10
|
+import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
|
|
|
11
|
+
|
|
|
12
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
13
|
+import java.io.BufferedOutputStream;
|
|
|
14
|
+import java.io.FileOutputStream;
|
|
|
15
|
+import java.io.IOException;
|
|
|
16
|
+import java.io.OutputStream;
|
|
|
17
|
+import java.util.ArrayList;
|
|
|
18
|
+import java.util.List;
|
|
|
19
|
+import java.util.Map;
|
|
|
20
|
+
|
|
|
21
|
+/**
|
|
|
22
|
+ * @Author: ymbgy
|
|
|
23
|
+ * @Date: 2022-09-21 13:26
|
|
|
24
|
+ */
|
|
|
25
|
+public class WordUtil {
|
|
|
26
|
+
|
|
|
27
|
+ private CellRenderData cell;
|
|
|
28
|
+
|
|
|
29
|
+ /**
|
|
|
30
|
+ * 查询生成的word文件流
|
|
|
31
|
+ *
|
|
|
32
|
+ * @param response
|
|
|
33
|
+ * @param datas
|
|
|
34
|
+ * @param modalFilePath
|
|
|
35
|
+ * @param resultFilePath
|
|
|
36
|
+ * @throws IOException
|
|
|
37
|
+ */
|
|
|
38
|
+ public static void getDocStreamFile(HttpServletResponse response, Map<String, Object> datas, String modalFilePath, String resultFilePath, String fileName) throws IOException {
|
|
|
39
|
+ //获取word模板和填充数据
|
|
|
40
|
+ XWPFTemplate template = XWPFTemplate.compile(modalFilePath).render(datas);
|
|
|
41
|
+ //设置返回类型及文件名
|
|
|
42
|
+ response.setContentType("application/octet-stream");
|
|
|
43
|
+ response.setHeader("Content-disposition", "attachment;filename=\"" + fileName + "\"");
|
|
|
44
|
+ //获取返回输出流
|
|
|
45
|
+ OutputStream out = response.getOutputStream();
|
|
|
46
|
+ BufferedOutputStream bos = new BufferedOutputStream(out);
|
|
|
47
|
+ template.write(bos);
|
|
|
48
|
+ bos.flush();
|
|
|
49
|
+ out.flush();
|
|
|
50
|
+ PoitlIOUtils.closeQuietlyMulti(template, bos, out);
|
|
|
51
|
+ }
|
|
|
52
|
+
|
|
|
53
|
+ /**
|
|
|
54
|
+ * 查询生成的word文件路径
|
|
|
55
|
+ *
|
|
|
56
|
+ * @param datas
|
|
|
57
|
+ * @param modalFilePath
|
|
|
58
|
+ * @param resultFilePath
|
|
|
59
|
+ * @return
|
|
|
60
|
+ * @throws IOException
|
|
|
61
|
+ */
|
|
|
62
|
+ public static String getDocFilePath(Map<String, Object> datas, String modalFilePath, String resultFilePath) throws IOException {
|
|
|
63
|
+ //获取word模板和填充数据
|
|
|
64
|
+ XWPFTemplate template = XWPFTemplate.compile(modalFilePath).render(datas);
|
|
|
65
|
+ template.writeAndClose(new FileOutputStream(resultFilePath));
|
|
|
66
|
+ return resultFilePath;
|
|
|
67
|
+ }
|
|
|
68
|
+
|
|
|
69
|
+ /**
|
|
|
70
|
+ * 构造绘制表格的方法
|
|
|
71
|
+ */
|
|
|
72
|
+ public static TableRenderData rebuildWordTableData(List<String> headerList, String headerBackgroundColor, String textColor, List<List<String>> tableContentList) {
|
|
|
73
|
+ RowRenderData header = rebuildWordTableHead(headerList, headerBackgroundColor, textColor);
|
|
|
74
|
+ List<RowRenderData> content = rebuildWordTableContent(tableContentList);
|
|
|
75
|
+ TableRenderData tableRenderData = Tables.create().addRow(header);
|
|
|
76
|
+ for (int i = 0; content != null && i < content.size(); i++) {
|
|
|
77
|
+ tableRenderData.addRow(content.get(i));
|
|
|
78
|
+ }
|
|
|
79
|
+ return tableRenderData;
|
|
|
80
|
+ }
|
|
|
81
|
+
|
|
|
82
|
+ /**
|
|
|
83
|
+ * 构造表格表头
|
|
|
84
|
+ *
|
|
|
85
|
+ * @return
|
|
|
86
|
+ */
|
|
|
87
|
+ public static RowRenderData rebuildWordTableHead(List<String> headerList, String headerBackgroundColor, String textColor) {
|
|
|
88
|
+ RowRenderData header = new RowRenderData();
|
|
|
89
|
+ for (int i = 0; headerList != null && i < headerList.size(); i++) {
|
|
|
90
|
+ CellRenderData cellRenderData = new CellRenderData();
|
|
|
91
|
+ ParagraphRenderData paragraphRenderData = new ParagraphRenderData();
|
|
|
92
|
+ Style style = new Style();
|
|
|
93
|
+ style.setColor(textColor);
|
|
|
94
|
+ style.setFontSize(12.0);
|
|
|
95
|
+ ParagraphStyle paragraphStyle = ParagraphStyle.builder()
|
|
|
96
|
+ .withAlign(ParagraphAlignment.CENTER)
|
|
|
97
|
+ // .withBackgroundColor(headerBackgroundColor)
|
|
|
98
|
+ .withDefaultTextStyle(style)
|
|
|
99
|
+ .build();
|
|
|
100
|
+ paragraphRenderData.addText(headerList.get(i));
|
|
|
101
|
+ paragraphRenderData.setParagraphStyle(paragraphStyle);
|
|
|
102
|
+ cellRenderData.addParagraph(paragraphRenderData);
|
|
|
103
|
+ header.addCell(cellRenderData);
|
|
|
104
|
+ }
|
|
|
105
|
+ return header;
|
|
|
106
|
+ }
|
|
|
107
|
+
|
|
|
108
|
+ /**
|
|
|
109
|
+ * 构造表格内容
|
|
|
110
|
+ *
|
|
|
111
|
+ * @return
|
|
|
112
|
+ */
|
|
|
113
|
+ public static List<RowRenderData> rebuildWordTableContent(List<List<String>> tableContentList) {
|
|
|
114
|
+ List<RowRenderData> rowContentList = new ArrayList<>();
|
|
|
115
|
+ for (int i = 0; tableContentList != null && i < tableContentList.size(); i++) {
|
|
|
116
|
+ List<String> rowdataList = tableContentList.get(i);
|
|
|
117
|
+ RowRenderData rowcontent = new RowRenderData();
|
|
|
118
|
+ for (int j = 0; rowdataList != null && j < rowdataList.size(); j++) {
|
|
|
119
|
+ CellRenderData cellRenderData = new CellRenderData();
|
|
|
120
|
+ ParagraphRenderData paragraphRenderData = new ParagraphRenderData();
|
|
|
121
|
+ Style style = new Style();
|
|
|
122
|
+ style.setFontSize(12.0);
|
|
|
123
|
+ ParagraphStyle paragraphStyle = ParagraphStyle.builder()
|
|
|
124
|
+ .withAlign(ParagraphAlignment.CENTER)
|
|
|
125
|
+ .withDefaultTextStyle(style)
|
|
|
126
|
+ .build();
|
|
|
127
|
+ paragraphRenderData.addText(rowdataList.get(j));
|
|
|
128
|
+ paragraphRenderData.setParagraphStyle(paragraphStyle);
|
|
|
129
|
+ cellRenderData.addParagraph(paragraphRenderData);
|
|
|
130
|
+ rowcontent.addCell(cellRenderData);
|
|
|
131
|
+ }
|
|
|
132
|
+ rowContentList.add(rowcontent);
|
|
|
133
|
+ }
|
|
|
134
|
+ return rowContentList;
|
|
|
135
|
+ }
|
|
|
136
|
+
|
|
|
137
|
+ /**
|
|
|
138
|
+ * 构建图片内容
|
|
|
139
|
+ */
|
|
|
140
|
+ public static PictureRenderData rebuildImageContent(Integer with, Integer height, String imageUrl, String relatedPath, Byte[] imageBytes) {
|
|
|
141
|
+ PictureRenderData pictureRenderData = null;
|
|
|
142
|
+ if (!StringUtils.isBlank(imageUrl)) {
|
|
|
143
|
+ // pictureRenderData = Pictures.of(imageUrl).size(with, height).create();
|
|
|
144
|
+ //Pictures.PictureBuilder pictureBuilder = Pictures.of("https://res.wx.qq.com/a/wx_fed/weixin_portal/res/static/img/1EtCRvm.png");
|
|
|
145
|
+ }else if (!StringUtils.isBlank(relatedPath)) {
|
|
|
146
|
+ pictureRenderData = Pictures.ofLocal(relatedPath).size(with,height).create();
|
|
|
147
|
+// Pictures.PictureBuilder pictureBuilder = Pictures.of("https://res.wx.qq.com/a/wx_fed/weixin_portal/res/static/img/1EtCRvm.png");
|
|
|
148
|
+ }
|
|
|
149
|
+ return pictureRenderData;
|
|
|
150
|
+ }
|
|
|
151
|
+ public static String getResultFilePath(Map<String, Object> datas, Configure config, String modalFilePath, String resultFilePath) throws IOException {
|
|
|
152
|
+ //获取word模板和填充数据
|
|
|
153
|
+ XWPFTemplate template = XWPFTemplate.compile(modalFilePath,config).render(datas);
|
|
|
154
|
+ template.writeAndClose(new FileOutputStream(resultFilePath));
|
|
|
155
|
+ return resultFilePath;
|
|
|
156
|
+ }
|
|
|
157
|
+}
|