First of all, tell us the original source of this article: http://www.havenliu.com/java/514.html/comment-page-1#comment-756
I have done it according to what he described, but there are some places that are a little vague and not easy to get out. So, I wrote another article, let everyone less go some detours.
: is the Word document content, that is, the template, in order to follow the steps to hygienic handrub, so in the need to input data into the place of pinyin,
Save the Word document as an XML file.
Next, the pinyin written above will play a role.
Open the XML file. Search for title.
Change title to ${title}
The same changes in other places.
When you're done, change the file suffix directly to FTL.
And then, directly on the code:
[Java]View Plaincopy
- Package com;
- Import Java.io.BufferedWriter;
- Import Java.io.File;
- Import java.io.FileNotFoundException;
- Import Java.io.FileOutputStream;
- Import java.io.IOException;
- Import Java.io.OutputStreamWriter;
- Import Java.io.Writer;
- Import java.util.ArrayList;
- Import Java.util.HashMap;
- Import java.util.List;
- Import Java.util.Map;
- Import freemarker.template.Configuration;
- Import Freemarker.template.Template;
- Import freemarker.template.TemplateException;
- Public class Wordtest {
- Private Configuration configuration = null;
- Public wordtest () {
- Configuration = new configuration ();
- Configuration.setdefaultencoding ("UTF-8");
- }
- public static void Main (string[] args) {
- Wordtest test = new Wordtest ();
- Test.createword ();
- }
- public void Createword () {
- map<string,object> datamap=New hashmap<string,object> ();
- GetData (DATAMAP);
- Configuration.setclassfortemplateloading (this.getclass (), "/com"); location where the//FTL file exists
- Template t=null;
- try {
- t = configuration.gettemplate ("WORDMODEL.FTL"); //File name
- } catch (IOException e) {
- E.printstacktrace ();
- }
- File OutFile = new File ("O:/outfilessa" +math.random () *10000+". Doc");
- Writer out = null;
- try {
- out = new BufferedWriter (New OutputStreamWriter (NewFileOutputStream (outFile)));
- } catch (FileNotFoundException E1) {
- E1.printstacktrace ();
- }
- try {
- T.process (DataMap, out);
- } catch (Templateexception e) {
- E.printstacktrace ();
- } catch (IOException e) {
- E.printstacktrace ();
- }
- }
- private void GetData (map<string, object> dataMap) {
- Datamap.put ("title", "title");
- Datamap.put ("year", "2012");
- Datamap.put ("Month", "2");
- Datamap.put ("Day", "13");
- Datamap.put ("auditor", "us");
- Datamap.put ("Phone", "13020265912");
- Datamap.put ("weave", "Jim Wentao");
- Datamap.put ("number", 1);
- Datamap.put ("Content", "contents", +2);
- list<map<string,object>> list = new arraylist<map<string,object>> ();
- For (int i = 0; i < i++) {
- map<string,object> map = new hashmap<string,object> ();
- Map.put ("number", I);
- Map.put ("content", "contents", +i);
- List.add (map);
- }
- Datamap.put ("list", list);
- }
- }
What you may be most concerned with now is the traversal of the file. Next we look at how the data is traversed.
Freemarker usage: http://www.havenliu.com/goodarticle/420.html in the FTL file that was just edited: Open FTL file, search <w:tr find the first one, tr meaning don't explain it , representing a row. This also means that I have found the first row in the table, but I need to traverse it not from the first line, but from the second row. OK, keep searching and find the second one. Add a < #list your collection name as Xxxx>ok on its head, have the end at the beginning, and play the El expression on the JSP should not feel strange. Search </w:tr> also find a second, plus </#list > terminator. OK, so it's okay.
Java generates Word documents (tested)