public class Xwpftest {
/**
* Use a docx document as a template, then replace the contents of it, and then write to the target document.
* @throws Exception
*/
@Test
public void Testtemplatewrite () throws Exception {
map<string, object> params = new hashmap<string, object> ();
Params.put ("Reportdate", "2014-02-28");
Params.put ("Appleamt", "100.00");
Params.put ("Bananaamt", "200.00");
Params.put ("Totalamt", "300.00");
String FilePath = "D:\\word\\template.docx";
InputStream is = new FileInputStream (FilePath);
Xwpfdocument doc = new Xwpfdocument (IS);
Replace the variable inside the paragraph
This.replaceinpara (doc, params);
Replace the variables inside the table
This.replaceintable (doc, params);
OutputStream OS = new FileOutputStream ("D:\\word\\write.docx");
Doc.write (OS);
This.close (OS);
This.close (IS);
}
/**
* Replace the variables inside the paragraph
* @param doc to replace the document
* @param params parameter
*/
private void Replaceinpara (Xwpfdocument doc, map<string, object> params) {
iterator<xwpfparagraph> Iterator = Doc.getparagraphsiterator ();
Xwpfparagraph para;
while (Iterator.hasnext ()) {
Para = Iterator.next ();
This.replaceinpara (para, params);
}
}
/**
* Replace the variables inside the paragraph
* @param para The paragraph to be replaced
* @param params parameter
*/
private void Replaceinpara (Xwpfparagraph para, map<string, object> params) {
List<xwpfrun> runs;
Matcher Matcher;
String runtext= "";
if (This.matcher (Para.getparagraphtext ()). Find ()) {
Runs = Para.getruns ();
if (Runs.size () >0) {
Int J = runs.size ();
for (int i = 0;i<j;i++) {
Xwpfrun run = runs.get (0);
String i1 = run.tostring;
RUNTEXT+=I1;
Para.removerun (0);
}
}
Matcher =This.matcher (Runtext);
if (Matcher.find ()) {
while ((Matcher = This.matcher (Runtext)). Find ()) {
Runtext = Matcher.replacefirst (string.valueof (Params.get (Matcher.group (1)));
}
When you set the text directly by calling Xwpfrun's SetText () method, a xwpfrun is recreated at the bottom, attaching the text behind the current text.
So we can not set the value directly, we need to delete the current run, and then manually insert a new run.
Para.insertnewrun (0). SetText (Runtext);
}
}
}
}
/**
* Replace the variables inside the table
* @param doc to replace the document
* @param params parameter
*/
private void Replaceintable (Xwpfdocument doc, map<string, object> params) {
iterator<xwpftable> Iterator = Doc.gettablesiterator ();
xwpftable table;
list<xwpftablerow> rows;
List<xwpftablecell> cells;
List<xwpfparagraph> paras;
while (Iterator.hasnext ()) {
Table = Iterator.next ();
rows = Table.getrows ();
for (Xwpftablerow row:rows) {
Cells = Row.gettablecells ();
for (Xwpftablecell cell:cells) {
paras = cell.getparagraphs ();
for (Xwpfparagraph Para:paras) {
This.replaceinpara (para, params);
}
}
}
}
}
/**
* Regular Match string
* @param str
* @return
*/
Private Matcher Matcher (String str) {
Pattern pattern = Pattern.compile ("\\$\\{(. +?) \ \} ", pattern.case_insensitive);
Matcher Matcher = Pattern.matcher (str);
return matcher;
}
/**
* Turn off the input stream
* @param is
*/
private void Close (InputStream is) {
if (is = null) {
try {
Is.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
/**
* Turn off the output stream
* @param os
*/
private void Close (OutputStream os) {
if (OS! = null) {
try {
Os.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}
Word includes a list of sets of hits (which have already been resolved and may not) for splitting into two or more xwpfrun issues