学习笔记-使用poi替换word内容 发表于 2017-04-18 所需用到的maven依赖 word内容替换Service层1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677import 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;@Servicepublic 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模版生成文件,并下载 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748import 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;@Controllerpublic 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); }}