Artikel ini berbagi contoh kata operasi java sederhana untuk Anda untuk referensi Anda. Konten spesifiknya adalah sebagai berikut
Paket apache.poi; impor java.io.bytearrayinputstream; import java.io.bytearrayoutputStream; import java.io.file; impor java.io.fileinputStream; impor java.fileOutputStream; impor java.io.io exception; java.util.map; impor org.apache.poi.hwpf.hwpfdocument; impor org.apache.poi.hwpf.usermodel.range; impor org.apache.poi.poif.filesystem.directoryentry; impor org.apache.poifs.poifsystem.directoryentry; impor org.apache.poifoypoilsystem.directoryentry; impor org.apache.poifoif.poipoysysysy kelas ExportDoctest {public static void main (string [] args) {string destfile = "d: //11.doc"; // ### filecon = new StringBuffer (); FileCon.Append ("Zhang Dapaoman 317258963215223/n" + "2011 09 2013 07 3/n" + "Penelitian Pawn Kedua Dewasa/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/N/N/N/N/N/N/N/N/ N/N/N/N/N/N/N/N/N/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 (celah "," 2); // Perhatikan lokasi dokumen biyezheng_moban.doc, dalam contoh ini, direktori root aplikasi dokumen hwpfdocument = new ExportDoctest (). REPLACEDOC ("BIYEZHENG_MOBAN.DOC", peta); BytearrayoutputStream ostream = bytearrayoutputStream baru (); coba {document.write (ostream); // output kata file outputStream out = fileoutputStream baru (destfile); outs.write (ostream.tobytearray ()); outs.close (); } catch (ioException e) {e.printstacktrace (); }} /** * * @param destfile * @param filecon * /public void exportDoc (string destfile, string filecon) {coba {// doc konten bytearrayinputStream bais = bytearrayInputStream baru (filecon.getBytes ()); PoifsfileSystem fs = poifsfileSystem baru (); Direktori direktori = fs.getroot (); direktori.createdocument ("WordDocument", Bais); FileOutputStream ostream = FileOutputStream baru (destfile); fs.writeFilessystem (ostream); bais.close (); ostream.close (); } catch (ioException e) {e.printstacktrace (); } } /** * Read word template and replace variable* @param srcPath * @param map * @return */ public HWPFDocument replaceDoc(String srcPath, Map<String, String> map) { try { // Read word template FileInputStream fis = new FileInputStream(new File(srcPath)); Hwpfdocument doc = hwpfdocument baru (FIS); // baca kata rentang konten teks bodyrange = doc.getRange (); // ganti konten teks untuk (map.entry <string, string> entri: map.entryset ()) {bodyrange.replacetext ("$ {" + entry.getKey () + "}", entri .getValue ()); } return doc; } catch (Exception e) {E.PrintStackTrace (); kembali nol; }}}Di atas adalah semua tentang artikel ini, saya harap ini akan membantu untuk pembelajaran semua orang.