- public class HwpfTest {
- @Test
- public void testWrite() throws Exception {
- String templatePath = "D:\\word\\template.doc";
- InputStream is = new FileInputStream(templatePath);
- HWPFDocument doc = new HWPFDocument(is);
- Range range = doc.getRange();
- //把range范围内的${reportDate}替换为当前的日期
- range.replaceText("${reportDate}", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
- range.replaceText("${appleAmt}", "100.00");
- range.replaceText("${bananaAmt}", "200.00");
- range.replaceText("${totalAmt}", "300.00");
- OutputStream os = new FileOutputStream("D:\\word\\write.doc");
- //把doc输出到输出流中
- doc.write(os);
- this.closeStream(os);
- this.closeStream(is);
- //浏览器导出
response.reset(); response.setContentType("application/msword; charset=UTF-8"); // response.setCharacterEncoding ("utf-8");//设置编码集,文件名不会发生中文乱码 response.setHeader("Content-Disposition", "Attachment;filename= " + new String((adYear+"年"+adMonth+"月计划.doc").toString().getBytes( //"UTF-8"),"UTF-8")); "gb2312"), "ISO8859_1")); OutputStream outstream= null; try { outstream = response.getOutputStream(); document.write(outstream); }catch (Exception e){ e.printStackTrace(); }finally { try { if(tempFileInputStream!=null){ outstream.close(); } if(outstream!=null){ outstream.close(); } }catch (Exception io){ io.printStackTrace(); } }
- }
- /**
- * 关闭输入流
- * @param is
- */
- private void closeStream(InputStream is) {
- if (is != null) {
- try {
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 关闭输出流
- * @param os
- */
- private void closeStream(OutputStream os) {
- if (os != null) {
- try {
- os.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
1.2 实例2
需要的jar包:poi-3.10.1.jar;poi-ooxml-3.10.1.jar;poi-ooxml-schemas-3.10.1.jar;poi-scratchpad-3.10.1.jar
* POI方式导出word文档,需要提前创建word导出模板
* POI方式导出word时,本机安装的office版本是2007以前的版本,使用到的是HWPFDocument
* POI方式导出word时,本机安装的office版本是2007+的版本,使用到的是XWPFDocument
- package officeWordDoc;
- import java.io.FileInputStream;
- import java.net.URLEncoder;
- import java.util.HashMap;
- import java.util.Map;
- import javax.faces.context.FacesContext;
- import javax.servlet.ServletOutputStream;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.poi.hwpf.HWPFDocument;
- import org.apache.poi.hwpf.usermodel.Range;
- /**
- * @ClassName:WordDocDemo_POI
- * @Description:使用POI方式实现word文档导出功能
- * 需要的jar包:poi-3.10.1.jar;poi-ooxml-3.10.1.jar;
- * poi-ooxml-schemas-3.10.1.jar;poi-scratchpad-3.10.1.jar
- * POI方式导出word文档,需要提前创建word导出模板
- * POI方式导出word时,本机安装的office版本是2007以前的版本,使用到的是HWPFDocument
- * POI方式导出word时,本机安装的office版本是2007+的版本,使用到的是XWPFDocument
- * @date:2017年5月12日
- * 修改备注:
- */
- public class WordDocDemo_POI{
- public static void main(String[] args){
- WordDocDemo_POI poi = new WordDocDemo_POI();
- poi.exportWord();
- }
- /**
- * @Description:使用POI实现word导出功能,JSF页面请求,此处涉及到JSF框架中FacesContext对象,可摘除,
- * @date: 2017年5月12日 下午6:50:51
- * @修改备注:
- */
- public void exportWord() {
- FacesContext context = FacesContext.getCurrentInstance();
- context.responseComplete();
- HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
- HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
- response.reset();
- try {
- String fileName = "关于"+"eventInfo.getEventName()"+"的情况报告.doc";
- fileName = URLEncoder.encode(fileName, "UTF-8");
- response.setHeader("Content-disposition", "attachment;filename="+fileName);
- response.setContentType("application/msword");
- ServletOutputStream outputStream = response.getOutputStream();
- //引入word模板,转化成流
- //String wordTemp = "E:\\项目\\导出报告模板\\reportTemplate.doc";
- String wordTemp = request.getSession().getServletContext().getRealPath("eventInfo.export.reportTemplate");
- FileInputStream inputStream = new FileInputStream(wordTemp);
- HWPFDocument document = new HWPFDocument(inputStream);
- //替换内容
- Range range = document.getRange();
- Map<String, String> content = this.getReplaceContent();
- for(java.util.Map.Entry<String, String> entry : content.entrySet()){
- range.replaceText(entry.getKey(), entry.getValue());
- }
- document.write(outputStream);
- inputStream.close();
- outputStream.flush();
- outputStream.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- FacesContext.getCurrentInstance().responseComplete();
- }
- /**
- * 获取模板中需要替换的文字
- * @param report
- * @return
- */
- private Map<String, String> getReplaceContent(){
- Map<String, String> content = new HashMap<String, String>();
- content.put("${eventType}", "typeName类型名称");
- content.put("${reportName}", "report.getReportName报告名称");
- content.put("${reporterUnit}", "report.getReporterUnit报送单位");
- content.put("${loginUser}", "loginUser登录人员信息");
- content.put("${nowTime}", "yyyy年MM月dd日时间消息");
- content.put("${occurTime}", "yyyy年MM月dd日事发时间");
- content.put("${disaster}", "非必填写1");
- content.put("${eventDesc}", "非必填写2");
- content.put("${eventDesc}", "非必填写3");
- return content;
- }
- }
https://blog.csdn.net/QQ578473688/article/details/72082085?locationNum=15&fps=1