This article for you to share the Java simple operation Word example, for your reference, the specific content as follows
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 ("Zhang Cannon man 317258963215223\n" + "2013 3\n" + "Second Artillery Corps study
Adult \ 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", "male");
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", "Computer Science and Technology");
Map.put ("type", "postgraduate");
Map.put ("Bianhao", "2011020301");
Map.put ("Nowy", "2011");
Map.put ("Nowm", "01");
Map.put ("Nowd", "20220301"); Note the Biyezheng_moban.doc document location, in this case, the root directory hwpfdocument document=new exportdoctest (). Replacedoc ("Biyezheng_moban.doc",
MAP);
Bytearrayoutputstream ostream = new Bytearrayoutputstream ();
try {document.write (ostream); Output Word file 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 (); /** * Reads the Word template and replaces the variable * @param srcpath * @param map * @return * * Public hwpfdocument replaced OC (String Srcpath, map<string, string> Map) {try {//Read Word template FileInputStream fis = new fileinputs Tream (New File (Srcpath));
Hwpfdocument doc = new hwpfdocument (FIS);
Read Word text content Range Bodyrange = Doc.getrange (); Replace the text content for (map.entry<string, string> entry:map.entrySet ()) {Bodyrange.replacetext ("${" + Entry).
Getkey () + "}", entry. GetValue ());
return doc;
catch (Exception e) {e.printstacktrace ();
return null;
}
}
}
The above is the entire content of this article, I hope to help you learn.