PoiUtils.java
8.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package com.sincere.file.utils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.ooxml.POIXMLTypeLoader;
import org.apache.poi.ooxml.util.DocumentHelper;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.InputSource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PoiUtils {
static Logger logger = LoggerFactory.getLogger(PoiUtils.class);
public static String createWord(JSONArray questions) {
XWPFDocument doc = new XWPFDocument();//创建word文档
for (int i = 0; i < questions.size(); i++) {
JSONObject question = questions.getJSONObject(i);
String question_text = question.getString("question_text");
String channel_type_name = question.getString("channel_type_name");
JSONObject options = question.getJSONObject("options");
XWPFParagraph p = doc.createParagraph();//新建一个段落
p.setAlignment(ParagraphAlignment.LEFT);//设置对其方式
if (channel_type_name.contains("img")) {
getImg(channel_type_name, doc, p);
} else {
writeP(HtmlUtil.delHTMLTag(channel_type_name) + "\r\n", p);
}
question_text = question_text.replaceAll(" ", " ");
if (question_text.contains("img")) {
writeP(question_text.replaceAll("<img[^>]*/>", " "), p);
getImg(question_text, doc, p);
} else {
writeP(HtmlUtil.delHTMLTag(question_text) + "\r\n", p);
}
if (null != options) {
String A = options.getString("A");
if (A.contains("src")) {
writeP("A." + A.replaceAll("<img[^>]*/>", " "), p);
getImg(A, doc, p);
} else {
writeP("A." + A + "\r\n", p);
}
String B = options.getString("B");
if (B.contains("src")) {
writeP("B." + B.replaceAll("<img[^>]*/>", " "), p);
getImg(B, doc, p);
} else {
writeP("B." + B + "\r\n", p);
}
String C = options.getString("C");
if (C.contains("src")) {
writeP("C." + C.replaceAll("<img[^>]*/>", " "), p);
getImg(C, doc, p);
} else {
writeP("C." + C + "\r\n", p);
}
String D = options.getString("D");
if (D.contains("src")) {
writeP("D." + D.replaceAll("<img[^>]*/>", " "), p);
getImg(D, doc, p);
} else {
writeP("D." + D + "\r\n", p);
}
}
}
try {
File file = new File("D://POI");
if (!file.exists()) file.mkdirs();
File file1 = new File(file.getAbsolutePath() + File.separator + System.currentTimeMillis() + ".doc");
if (!file1.exists()) file1.createNewFile();
FileOutputStream out = new FileOutputStream(file1);
doc.write(out);
out.close();
return file1.getAbsolutePath();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
private static void writeP(String content, XWPFParagraph p) {
XWPFRun r = p.createRun();//创建段落文本
r.setFontSize(14);
r.setText(content);
r.setColor("000000");//设置颜色
}
private static void getImg(String imgHtml, XWPFDocument doc, XWPFParagraph paragraph) {
// imgHtml = HtmlUtil.delHTMLTag(imgHtml);
// imgHtml = imgHtml.substring(imgHtml.indexOf("src="), imgHtml.indexOf("style"));
// imgHtml = imgHtml.replace("\"", "");
// imgHtml = imgHtml.replace("src=", "");
// logger.error("imgHtml:{}", imgHtml);
Set<String> imgSer = getImgStr(imgHtml);
Iterator<String> iterator = imgSer.iterator();
while (iterator.hasNext()) {
String imgUrl = iterator.next();
String imgName = System.currentTimeMillis() + ".png";
String filePath = "D://POI//Img";
download(imgUrl, imgName, filePath);
writeImg2Word(filePath + File.separator + imgName, doc, paragraph);
}
}
private static String writeImg2Word(String s, XWPFDocument doc, XWPFParagraph paragraph) {
try {
XWPFRun run = paragraph.createRun();
// 获取图片
InputStream is = new FileInputStream(s);
byte[] bs = IOUtils.toByteArray(is);
BufferedImage image = ImageIO.read(new ByteArrayInputStream(bs));
// 获取组装图片宽高,单位pt
StringBuffer dataSize = new StringBuffer();
dataSize.append("width:").append(image.getWidth()).append("pt;");
dataSize.append("height:").append(image.getHeight()).append("pt;");
// 添加图片到Word中
String rid = doc.addPictureData(bs, Document.PICTURE_TYPE_PNG);
StringBuffer xml = new StringBuffer();
xml.append("<w:pict xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"");
xml.append(" xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
xml.append(" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">");
xml.append(" <v:shape id=\"图片1").append("\" o:spid=\"\" type=\"\" alt=\"\" style=\"").append(dataSize).append("\">\r\n");
xml.append(" <v:imagedata r:id=\"").append(rid).append("\" o:title=\"\" />\r\n");
xml.append(" </v:shape>");
xml.append("</w:pict>\r\n");
InputSource source = new InputSource(new StringReader(xml.toString()));
org.w3c.dom.Document pictDoc = DocumentHelper.readDocument(source);
// 将信息写入run中
run.setEmbossed(true);
XmlObject xmlObject = XmlObject.Factory.parse(pictDoc.getDocumentElement(), POIXMLTypeLoader.DEFAULT_XML_OPTIONS);
run.getCTR().set(xmlObject);
run.setText("\r\n");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void download(String urlString, String filename, String savePath) {
try {
// 构造URL
URL url = new URL(urlString);
// 打开连接
URLConnection con = url.openConnection();
//设置请求超时为5s
con.setConnectTimeout(5 * 1000);
// 输入流
InputStream is = con.getInputStream();
// 1K的数据缓冲
byte[] bs = new byte[1024];
// 读取到的数据长度
int len;
// 输出的文件流
File sf = new File(savePath);
if (!sf.exists()) {
sf.mkdirs();
}
// 获取图片的扩展名
String extensionName = filename.substring(filename.lastIndexOf(".") + 1);
// 新的图片文件名 = 编号 +"."图片扩展名
OutputStream os = new FileOutputStream(sf.getPath() + "\\" + filename);
// 开始读取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
// 完毕,关闭所有链接
os.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 得到网页中图片的地址
*/
private static Set<String> getImgStr(String htmlStr) {
Set<String> pics = new HashSet<String>();
String img = "";
Pattern p_image;
Matcher m_image;
String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
m_image = p_image.matcher(htmlStr);
while (m_image.find()) {
// 得到<img />数据
img = m_image.group();
// 匹配<img>中的src数据
Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
while (m.find()) {
pics.add(m.group(1));
}
}
return pics;
}
}