java simply operates on an word instance
- 2020-05-05 11:21:00
- OfStack
This article has Shared java simple operation word example 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";
//##################### Export based on custom content Word The document #################################################
StringBuffer fileCon=new StringBuffer();
fileCon.append(" A cannon male 317258963215223\n" +
"2011 09 2013 07 3\n" +
" Second artillery 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");
new ExportDocTest().exportDoc(destFile, fileCon.toString());
//################## According to the Word Template export single Word The document ###################################################
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", " A graduate student ");
map.put("bianhao", "2011020301");
map.put("nowy", "2011");
map.put("nowm", "01");
map.put("nowd", "20220301");
// Pay attention to biyezheng_moban.doc Document location , In this case, the application root
HWPFDocument document=new ExportDocTest().replaceDoc("biyezheng_moban.doc", map);
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
try {
document.write(ostream);
// The 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 Template and replace variables
* @param srcPath
* @param map
* @return
*/
public HWPFDocument replaceDoc(String srcPath, Map<String, String> map) {
try {
// read word The template
FileInputStream fis = new FileInputStream(new File(srcPath));
HWPFDocument doc = new HWPFDocument(fis);
// read word The 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 the entire content of this article, I hope to help you with your study.