Java generates Word documents (tested)

Source: Internet
Author: User

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
  1. Package com;
  2. Import Java.io.BufferedWriter;
  3. Import Java.io.File;
  4. Import java.io.FileNotFoundException;
  5. Import Java.io.FileOutputStream;
  6. Import java.io.IOException;
  7. Import Java.io.OutputStreamWriter;
  8. Import Java.io.Writer;
  9. Import java.util.ArrayList;
  10. Import Java.util.HashMap;
  11. Import java.util.List;
  12. Import Java.util.Map;
  13. Import freemarker.template.Configuration;
  14. Import Freemarker.template.Template;
  15. Import freemarker.template.TemplateException;
  16. Public class Wordtest {
  17. Private Configuration configuration = null;
  18. Public wordtest () {
  19. Configuration = new configuration ();
  20. Configuration.setdefaultencoding ("UTF-8");
  21. }
  22. public static void Main (string[] args) {
  23. Wordtest test = new Wordtest ();
  24. Test.createword ();
  25. }
  26. public void Createword () {
  27. map<string,object> datamap=New hashmap<string,object> ();
  28. GetData (DATAMAP);
  29. Configuration.setclassfortemplateloading (this.getclass (), "/com"); location where the//FTL file exists
  30. Template t=null;
  31. try {
  32. t = configuration.gettemplate ("WORDMODEL.FTL"); //File name
  33. } catch (IOException e) {
  34. E.printstacktrace ();
  35. }
  36. File OutFile = new File ("O:/outfilessa" +math.random () *10000+". Doc");
  37. Writer out = null;
  38. try {
  39. out = new BufferedWriter (New OutputStreamWriter (NewFileOutputStream (outFile)));
  40. } catch (FileNotFoundException E1) {
  41. E1.printstacktrace ();
  42. }
  43. try {
  44. T.process (DataMap, out);
  45. } catch (Templateexception e) {
  46. E.printstacktrace ();
  47. } catch (IOException e) {
  48. E.printstacktrace ();
  49. }
  50. }
  51. private void GetData (map<string, object> dataMap) {
  52. Datamap.put ("title", "title");
  53. Datamap.put ("year", "2012");
  54. Datamap.put ("Month", "2");
  55. Datamap.put ("Day", "13");
  56. Datamap.put ("auditor", "us");
  57. Datamap.put ("Phone", "13020265912");
  58. Datamap.put ("weave", "Jim Wentao");
  59. Datamap.put ("number", 1);
  60. Datamap.put ("Content", "contents", +2);
  61. list<map<string,object>> list = new arraylist<map<string,object>> ();
  62. For (int i = 0; i < i++) {
  63. map<string,object> map = new hashmap<string,object> ();
  64. Map.put ("number", I);
  65. Map.put ("content", "contents", +i);
  66. List.add (map);
  67. }
  68. Datamap.put ("list", list);
  69. }
  70. }


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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.