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" +
"3\n" +
"Second artillery 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 example, to apply 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 ();
}
}
/**
* Read Word templates and replace variables
* @param Srcpath
* @param map
* @return
*/
Public hwpfdocument Replacedoc (String srcpath, map<string, string> Map) {
try {
Read a Word template
FileInputStream fis = new FileInputStream (new File (Srcpath));
Hwpfdocument doc = new hwpfdocument (FIS);
Read Word text content
Range Bodyrange = Doc.getrange ();
Replace 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;
}
}
/**
* Operation form
*/
public void operatetable () {
try {
Poifsfilesystem fs = new Poifsfilesystem (New FileInputStream (
"D:\\xbrl.doc"));
Hwpfdocument doc = new hwpfdocument (FS);
Range range = Doc.getoverallrange ();
Tableiterator ti = new Tableiterator (range);
Table table = null;
while (Ti.hasnext ()) {
System.out.println ("Getting table!");
Table table = (table) ti.next ();
System.out.println ("Number of rows:" + table.numrows ());
for (int a = 0; a < table.numrows (); a++) {
TableRow row = Table.getrow (a);
System.out.println ("\ttable row number:" + a);
for (int b = 0; b < row.numcells (); b++) {
System.out.println ("\t\ttable cell number:" + b);
TableCell TableCell = Row.getcell (b);
for (int c = 0; c < tablecell.numparagraphs (); C + +) {
Paragraph Tablepara = Tablecell.getparagraph (c);
for (int d = 0; d < tablepara.numcharacterruns (); d++) {
Characterrun run = Tablepara.getcharacterrun (d);
System.out.println ("\t\ttext: \" "+ Run.text ()
+ "\"");
}
}
}
}
System.out.println ("\ n");
}
} catch (Exception e) {
E.printstacktrace ();
}
}
}
Java POI Word Writing document