In diesem Artikel werden Beispiele für das einfache Java -Operation -Wort für Sie für Ihre Referenz angezeigt. Der spezifische Inhalt ist wie folgt
paket apache.poi; import Java.io.BytearrayInputStream; Import Java.io.BytearrayoutputStream; Import Java.io.file; Import Java.io.FileInputStream; import Java.io.io.io.fileOutputStream; Import Java.ioxception. 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 Klasse ExportDoctest {public static void main (String [] args) {String destfile = "d: //11.doc"; //############################################################################################################### StringBuffer fileCon = new StringBuffer (); filecon.Append ("Zhang Dapaoman 317258963215223/N" + "2011 09 2013 07 3/n" + "Second Pawn Research 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/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/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 /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/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/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/n/n/n 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", "graduate student"); map.put("bianhao", "2011020301"); map.put("nowy", "2011"); map.put("nowm", "01"); map.put("nowd", "20220301"); //Note Der Ort des dokuments biyezheng_moban.doc, in diesem Beispiel das Anwendungsroot -Verzeichnis hwpfdocument document = new ExportDoctest (). Ersatzedoc ("biyezheng_moban.doc", map); BytearrayoutputStream ostream = new bytearrayoutputStream (); try {document.write (ostream); // Word -Datei Ausgabe ausgiehen Stream 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 (); Verzeichnis.Createdocument ("WordDocument", Bais); FileOutputStream ostream = new FileOutputStream (destfile); fs.WriteFilessystem (Ostream); bais.close (); ostream.close (); } catch (ioException e) {e.printstacktrace (); }} / ** * Wortvorlage lesen und Variable ersetzen * @param srcPath * @param map * @return * / public hwpfdocument ersetztesDoc (String srcPath, map <String> map) {try {// Word -Vorlagen -Dateilate -Datei -fileStream lesen. Hwpfdocument doc = new hwpfdocument (fis); // Word Text Inhaltsbereich Lesen Sie BodyRange = doc.getRange (); // Textinhalt ersetzen für (map.Entry <String, String> Eintrag: map.entryset ()) {bodyRange.replacetExt ("$ {" + Eintrag.getkey () + "}", Eintrag .getValue ()); } return doc; } catch (Ausnahme e) {e.printstacktrace (); null zurückkehren; }}}Das Obige dreht sich alles um diesen Artikel, ich hoffe, es wird für das Lernen aller hilfreich sein.