Java generates complex Excel with Freemarker. (Freemarker)

Source: Internet
Author: User

In the generation of Excel, most of the time is the use of POI,JXL, and so on, but for the complex of Excel, this workload is very large, and, for me so lazy people, it is rather painful one thing, so, I have to find out if there is a simple way to get this thing done. I thought about the last time I successfully generated a Word document with Freemarker. I was wondering if I could build Excel too? Then I'll try. The result is great, however, and there are some differences in the generation of Word documents, okay, no nonsense anymore.

A relatively simple Excel diagram.

This is a structure of Excel, then, save it as "XML Spreadsheet 2003 (*.xml)"

To how to fill the data exactly where it is, the trick to generating a Word document is to add a marker, or write pinyin, where you want to populate the data.

is the generated XML

I think the structure should be clear to everyone, row: Represents a row, the cell represents a cell.

A friend who has used POI,JXL should be very familiar with it.

Some friends may open the time, the XML is very messy, can not find the need to modify the place, the above mentioned small tricks played a role, according to Pinyin or your unique logo to find the location needed to change.

Write the Freemarker module statement where you want to populate the data. For example: To change the type to "${type}", double quotes do not copy the past, hehe.

Also, there may be many lines in this line, all of which require an iterative label, as I mentioned in the previous article.

< #list sequence as item>
...
</#list >

In the syntax format above, sequence is either a collection object or an expression, but the expression returns a collection object.

Item is an arbitrary name, which is a collection element that is iterated over. In addition, when iterating over a collection object, it also contains two special loop variables:
Item_index: Index value of the current variable
Item_has_next: Whether the next object exists
You can also use the < #break > command to jump out of an iteration

In fact, these are all in the http://www.havenliu.com/goodarticle/420.html, there are time friends can see. There are many interesting things.

Back to the point, cut the line you need to loop through to < #list ></#list > Center.

Well, after this step, you can change its suffix to: FTL.

Work to do this step has been almost, the code, you can look at an article. Come on, take the blame, put the code on and write some notes.

[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+". xls"); //path to the generated file
  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. //When assigning a value here, you need to be aware that the data you need in XML must be provided to it, or you will not find an element wrong.
  52. private void GetData (map<string, object> dataMap) {
  53. Datamap.put ("title", "title");
  54. Datamap.put ("year", "2012");
  55. Datamap.put ("Month", "2");
  56. Datamap.put ("Day", "13");
  57. Datamap.put ("auditor", "Xin");
  58. Datamap.put ("Phone", "xxxxxxxxxxxxx");
  59. Datamap.put ("weave", "Wentao");
  60. Datamap.put ("number", 1);
  61. Datamap.put ("Content", "contents", +2);
  62. list<map<string,object>> list = new arraylist<map<string,object>> ();
  63. For (int i = 0; i < i++) {
  64. map<string,object> map = new hashmap<string,object> ();
  65. Map.put ("number", I);
  66. Map.put ("content", "contents", +i);
  67. List.add (map);
  68. }
  69. Datamap.put ("list", list);
  70. }
  71. }

Well, that's about it.

How the program did not error, then your Excel has been successfully generated, but.

Sometimes you can't open the table?

This is because excel in the generation of XML, it may be to not waste resources, at the time of generation, will specify how many lines of Excel, if beyond this length, it will compile does not pass. Error.

The problem is well solved.

To address these 2 issues, 2 different places need to be modified.

1: The sheet to the settings. It's sheet. OK, open XML, search for "Worksheet", if you have multiple worksheets, find your own.

Modify its property value "Ss:expandedrowcount", see this, you should understand it? Set it up a little bit, or pass a value to it by the program. The problem is solved.

2: Table, if you have patience, you can according to it to your log to see, a look will know. But not very easy to find, hehe.

Search for the last row, and modify its property value "Ss:index".

Java generates complex Excel with Freemarker. (Freemarker)

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.