博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java操作word
阅读量:4630 次
发布时间:2019-06-09

本文共 4279 字,大约阅读时间需要 14 分钟。

一个使用Apache POI写word文档的实例:

1 package apache.poi;  2   3 import java.io.ByteArrayInputStream;  4 import java.io.ByteArrayOutputStream;  5 import java.io.File;  6 import java.io.FileInputStream;  7 import java.io.FileOutputStream;  8 import java.io.IOException;  9 import java.io.OutputStream; 10 import java.util.HashMap; 11 import java.util.Map; 12  13 import org.apache.poi.hwpf.HWPFDocument; 14 import org.apache.poi.hwpf.usermodel.Range; 15 import org.apache.poi.poifs.filesystem.DirectoryEntry; 16 import org.apache.poi.poifs.filesystem.POIFSFileSystem; 17  18  19 public class ExportDocTest { 20      21     public static void main(String[] args) { 22         String destFile="D:\\11.doc"; 23         //#####################根据自定义内容导出Word文档################################################# 24         StringBuffer fileCon=new StringBuffer(); 25         fileCon.append("               张大炮            男              317258963215223\n" + 26                 "2011     09        2013     07       3\n" + 27                 "    二炮研究              成人\n" + 28                 "2013000001                             2013     07     08"); 29         fileCon.append("\n\r\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); 30          31         new ExportDocTest().exportDoc(destFile, fileCon.toString()); 32          33         //##################根据Word模板导出单个Word文档################################################### 34         Map
map=new HashMap
(); 35 36 map.put("name", "Zues"); 37 map.put("sex", "男"); 38 map.put("idCard", "200010"); 39 map.put("year1", "2000"); 40 map.put("month1", "07"); 41 map.put("year2", "2008"); 42 map.put("month2", "07"); 43 map.put("gap", "2"); 44 map.put("zhuanye", "计算机科学与技术"); 45 map.put("type", "研究生"); 46 map.put("bianhao", "2011020301"); 47 map.put("nowy", "2011"); 48 map.put("nowm", "01"); 49 map.put("nowd", "20220301"); 50 //注意biyezheng_moban.doc文档位置,此例中为应用根目录 51 HWPFDocument document=new ExportDocTest().replaceDoc("biyezheng_moban.doc", map); 52 ByteArrayOutputStream ostream = new ByteArrayOutputStream(); 53 try { 54 document.write(ostream); 55 //输出word文件 56 OutputStream outs=new FileOutputStream(destFile); 57 outs.write(ostream.toByteArray()); 58 outs.close(); 59 } catch (IOException e) { 60 e.printStackTrace(); 61 } 62 63 } 64 65 66 /** 67 * 68 * @param destFile 69 * @param fileCon 70 */ 71 public void exportDoc(String destFile,String fileCon){ 72 try { 73 //doc content 74 ByteArrayInputStream bais = new ByteArrayInputStream(fileCon.getBytes()); 75 POIFSFileSystem fs = new POIFSFileSystem(); 76 DirectoryEntry directory = fs.getRoot(); 77 directory.createDocument("WordDocument", bais); 78 FileOutputStream ostream = new FileOutputStream(destFile); 79 fs.writeFilesystem(ostream); 80 bais.close(); 81 ostream.close(); 82 83 } catch (IOException e) { 84 e.printStackTrace(); 85 } 86 } 87 88 89 /** 90 * 读取word模板并替换变量 91 * @param srcPath 92 * @param map 93 * @return 94 */ 95 public HWPFDocument replaceDoc(String srcPath, Map
map) { 96 try { 97 // 读取word模板 98 FileInputStream fis = new FileInputStream(new File(srcPath)); 99 HWPFDocument doc = new HWPFDocument(fis);100 // 读取word文本内容101 Range bodyRange = doc.getRange();102 // 替换文本内容103 for (Map.Entry
entry : map.entrySet()) {104 bodyRange.replaceText("${" + entry.getKey() + "}", entry105 .getValue());106 }107 return doc;108 } catch (Exception e) {109 e.printStackTrace();110 return null;111 }112 }113 114 }

转载于:https://www.cnblogs.com/lcngu/p/5021981.html

你可能感兴趣的文章
《php源码学习研究》 第一天
查看>>
C++虚函数和纯虚函数的异同
查看>>
checkbox操作
查看>>
Spring概念
查看>>
VS2017 添加引用报错问题
查看>>
LeetCode 147. Insertion Sort List
查看>>
sqlalchemy增删改查
查看>>
Java校验时间段重叠
查看>>
iOS开发 仿淘宝,京东商品详情3D动画
查看>>
虚拟机中安装SQL server 2008 R2
查看>>
Oracle建表
查看>>
HTML5中的数据集dataset和自定义属性data-*
查看>>
redis底层数据结构初解析
查看>>
Makefile的作用
查看>>
21.无向图邻接表的邻接结点类
查看>>
iOS7状态栏字体颜色修改
查看>>
SPS配置基础架构
查看>>
解决TestNG多线程并发时,线程不安全问题
查看>>
Gym 100952A&&2015 HIAST Collegiate Programming Contest A. Who is the winner?【字符串,暴力】
查看>>
结束线程的方法
查看>>