java简单操作word实例
程序员文章站
2024-03-31 19:47:22
本文为大家分享了java简单操作word例子,供大家参考,具体内容如下
package apache.poi;
import java.io.bytearr...
本文为大家分享了java简单操作word例子,供大家参考,具体内容如下
package apache.poi; import java.io.bytearrayinputstream; import java.io.bytearrayoutputstream; import java.io.file; import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.ioexception; import java.io.outputstream; import java.util.hashmap; import java.util.map; import org.apache.poi.hwpf.hwpfdocument; import org.apache.poi.hwpf.usermodel.range; import org.apache.poi.poifs.filesystem.directoryentry; import org.apache.poi.poifs.filesystem.poifsfilesystem; public class exportdoctest { public static void main(string[] args) { string destfile="d:\\11.doc"; //#####################根据自定义内容导出word文档################################################# stringbuffer filecon=new stringbuffer(); filecon.append(" 张大炮 男 317258963215223\n" + "2011 09 2013 07 3\n" + " 二炮研究 成人\n" + "2013000001 2013 07 08"); 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"); new exportdoctest().exportdoc(destfile, filecon.tostring()); //##################根据word模板导出单个word文档################################################### map<string, string> map=new hashmap<string, string>(); map.put("name", "zues"); map.put("sex", "男"); map.put("idcard", "200010"); map.put("year1", "2000"); map.put("month1", "07"); map.put("year2", "2008"); map.put("month2", "07"); map.put("gap", "2"); map.put("zhuanye", "计算机科学与技术"); map.put("type", "研究生"); map.put("bianhao", "2011020301"); map.put("nowy", "2011"); map.put("nowm", "01"); map.put("nowd", "20220301"); //注意biyezheng_moban.doc文档位置,此例中为应用根目录 hwpfdocument document=new exportdoctest().replacedoc("biyezheng_moban.doc", map); bytearrayoutputstream ostream = new bytearrayoutputstream(); try { document.write(ostream); //输出word文件 outputstream outs=new fileoutputstream(destfile); outs.write(ostream.tobytearray()); outs.close(); } catch (ioexception e) { e.printstacktrace(); } } /** * * @param destfile * @param filecon */ public void exportdoc(string destfile,string filecon){ try { //doc content bytearrayinputstream bais = new bytearrayinputstream(filecon.getbytes()); poifsfilesystem fs = new poifsfilesystem(); directoryentry directory = fs.getroot(); directory.createdocument("worddocument", bais); fileoutputstream ostream = new fileoutputstream(destfile); fs.writefilesystem(ostream); bais.close(); ostream.close(); } catch (ioexception e) { e.printstacktrace(); } } /** * 读取word模板并替换变量 * @param srcpath * @param map * @return */ public hwpfdocument replacedoc(string srcpath, map<string, string> map) { try { // 读取word模板 fileinputstream fis = new fileinputstream(new file(srcpath)); hwpfdocument doc = new hwpfdocument(fis); // 读取word文本内容 range bodyrange = doc.getrange(); // 替换文本内容 for (map.entry<string, string> entry : map.entryset()) { bodyrange.replacetext("${" + entry.getkey() + "}", entry .getvalue()); } return doc; } catch (exception e) { e.printstacktrace(); return null; } } }
以上就是本文的全部内容,希望对大家的学习有所帮助。