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
- 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+". xls"); //path to the generated file
- 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 ();
- }
- }
- //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.
- 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", "Xin");
- Datamap.put ("Phone", "xxxxxxxxxxxxx");
- Datamap.put ("weave", "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);
- }
- }
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)