This article shares examples of simple Java operation word for you for your reference. The specific content is 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"; //############################################################################################################### 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/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 the location of the biyezheng_moban.doc document, in this example, the application 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.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 = 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; } }}The above is all about this article, I hope it will be helpful to everyone's learning.