Java POI Word Writing document

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.