学习笔记-使用poi替换word内容

所需用到的maven依赖

word内容替换

Service层

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
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.model.FieldsDocumentPart;
import org.apache.poi.hwpf.usermodel.Field;
import org.apache.poi.hwpf.usermodel.Fields;
import org.apache.poi.hwpf.usermodel.Range;
import org.springframework.stereotype.Service;
@Service
public class WordService {
public void readwriteWord(String sourcefilePath, String filepath, String fileName, Map<String, String> map) {
// 读取word模板
FileInputStream in = null;
try {
in = new FileInputStream(new File(sourcefilePath));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
HWPFDocument hdt = null;
try {
hdt = new HWPFDocument(in);
} catch (IOException e1) {
e1.printStackTrace();
}
Fields fields = hdt.getFields();
Iterator<Field> it = fields.getFields(FieldsDocumentPart.MAIN).iterator();
while (it.hasNext()) {
System.out.println(it.next().getType());
}
// 读取word文本内容
Range range = hdt.getRange();
// 替换文本内容
for (Map.Entry<String, String> entry : map.entrySet()) {
range.replaceText(entry.getKey(), entry.getValue());
}
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
FileOutputStream out = null;
try {
out = new FileOutputStream(filepath + fileName, true);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
hdt.write(ostream);
} catch (IOException e) {
e.printStackTrace();
}
// 输出字节流
try {
out.write(ostream.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
ostream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Controller层

根据word模版生成文件,并下载

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
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import tk.jiemin.gx.service.WordService;
@Controller
public class IndexController {
@Resource
WordService wordService;
@RequestMapping("/download")
public ResponseEntity<byte[]> export(@RequestParam("company") String company, HttpServletRequest request)
throws IOException {
String path = request.getSession().getServletContext().getRealPath("/");
Map<String, String> map = new HashMap<String, String>();
map.put("${company}", company);
String srcPath = "/Users/zjm/Documents/tongda/1、高新技术企业认定委托协议书.doc";
String fileName = "1、高新技术企业认定委托协议书"+"_"+company+ ".doc";
wordService.readwriteWord(srcPath, path, fileName, map);
File file = new File(path + fileName);
HttpHeaders headers = new HttpHeaders();
String fn = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", fn);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
}
}