本说明显示了如何使用简单的Java代码根据模板生成Word文档。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Hashtable;
/**
* This code takes in a hashtable containing key fields required to populate
* values into a Word template (XML) and output a Word document (also XML).
* Template should contain ##KEY## fields for each hashtable key with same
* name (without the ##s); the ##KEY## will be replaced by the value.
* The main() method is written as an example.
* Modified from code found at http://dinoch.dyndns.org:7070/WordML/AboutWordML.jsp
* @author C. Peter Chen of http://dev-notes.com
* @date 20080327
*/
public class msWordUtils {
/**
* This main() method is used for demonstration purposes only.
* @param args
* @author C. Peter Chen of http://dev-notes.com
* @date 20080327
*/
public static void main(String[] args) {
String templatePathFilename = "c:\\word_template.xml";
String outputPathFilename = "c:\\word_output.xml";
Hashtable ht = new Hashtable();
ht.put("INVOICENUMBER","384123");
ht.put("CUSTOMERNAME","Some Company, LLC.");
ht.put("ITEMNAME1","Coffee");
ht.put("UNITPRICE1","1.50");
ht.put("QTY1","1");
ht.put("LINETOTAL1","1.50");
ht.put("ITEMNAME2","Donut");
ht.put("UNITPRICE2","1.00");
ht.put("QTY2","2");
ht.put("LINETOTAL2","2.00");
ht.put("INVOICETOTAL","3.50");
ht.put("DUEDATE","4/1/2008");
generateWordDoc(ht, templatePathFilename, outputPathFilename);
}
/**
*
* @param ht
* @param templatePathFileName
* @param outputPathFileName
* @author C. Peter Chen of http://dev-notes.com
* @date 20080327
*/
public static void generateWordDoc(Hashtable ht, String templatePathFilename, String outputPathFilename) {
try {
BufferedReader reader = new BufferedReader(new FileReader(templatePathFilename));
File destination = new File(outputPathFilename);
BufferedWriter writer = new BufferedWriter(new FileWriter(destination));
String thisLine;
int i = 0;
while ((thisLine = reader.readLine()) != null) {
System.out.println(i);
for (java.util.Enumeration e = ht.keys(); e.hasMoreElements();) {
String name = (String) e.nextElement();
String value = ht.get(name).toString();
// Use this if we need to XML-encode the string in hashtable value...
thisLine = thisLine.replaceAll("##" + name.toUpperCase() + "##", XmlEncode(value));
// ... or this if we do not need to do XML-encode.
//thisLine= thisLine.replaceAll("##" + name.toUpperCase() + "##", value);
}
writer.write(thisLine);
writer.newLine();
i++;
}
writer.close();
System.out.println("done");
}
catch (Exception e) {
System.out.println("exception!=" + e);
}
}
/**
* Encodes regular text to XML.
* @param text
* @return string
* @author http://dinoch.dyndns.org:7070/WordML/AboutWordML.jsp
* @date 20050328
*/
private static String XmlEncode(String text) {
int[] charsRequiringEncoding = {38, 60, 62, 34, 61, 39};
for(int i = 0; i < charsRequiringEncoding.length - 1; i++) {
text = text.replaceAll(String.valueOf((char)charsRequiringEncoding[i]),"&#"+charsRequiringEncoding[i]+";");
}
return text;
}
}