Preface Word document generation is required in many projects, and there are two scenarios, one generated through the Apache POI Toolkit, Itext, and the other using the Office Open XML specification. Various programs have advantages and disadvantages, here does not have a detailed comparison, has been a lot of people have carried out in-depth comparison, please consult Google and degrees Niang details. The scenario used here is to document the way that Office Open XML is generated.
For Poi,itext, it is not easy to generate complex doc files. The XML structure is used to generate a document to generate an XML file, look inside the content, it is estimated that the full head is Khan.
Tiny builders, that the creation of doc,xls,pdf and other document types is the management system around the pit of the past, and since there is no way to bypass, that is to say anyway?
Casually Baidu Google, found that this program in fact many people have done, such as in the following connection, the author has a good practice: http://developer.51cto.com/art/201106/270815.htm
The solution in this article, similar to that, is to use the template language in combination with XML to generate the XML Word file, but the implementation pattern is not the same, this article provides a more universal, easy-to-use, easy-to-use to create a Word document like a Web site.
For this reason, the tiny framework abstracts the content of the text format file, as long as it is a text-formatted file that can be extended by the tiny framework's document generation framework, enabling developers to easily and quickly generate text-type documents.
Because Office documents are already stored in XML format, because PDFs can be generated from XML, the most commonly used Office documents and PDF documents can be generated quickly through this framework, although there are some basic tasks to do by themselves ( Secretly reveal: Common template framework will be included in the.
Implementation IdeasBecause Office Open XML is actually generated as an XML file, you can open it in Word. So the problem is to generate XML text file, and generate XML text file, the better solution is the template language, this time can be used velocity, freemaker or other template language can be, this article uses Velocity template language to build.
Of course, the tiny builder also generalizes this problem because it not only generates word, it also generates files such as PDFs, XLS, and even source files in a language. Therefore, we believe that all text type files can be generated by macro files + template files, macros are defined in the macro file has been written to generate some kind of text content, and the macro file is the final file used to invoke. Macro files are written by people who are proficient in a document format, while macro files are written by ordinary programmers.
The framework section of the code is already written, so the extension of the type of document to be generated can only be done by writing the corresponding macro file.
macro File WritingThere are two ways to write macro files, one is to write according to the Office Open XML specification, and the other is to use a Word document to write a little bit of content, save it as an XML format, then find the relevant content, and then modify it to schema.
test Cases insert in-memory images into Word documents
1 2 3 4 5 6 7 8 9 10 11 |
# @wordDocument ()
# @body ()
# @image ({"Name": "111.jpg", "width": "249pt", "height": "119.25pt", "Data": "$picData"})
#end
#end
#end |
Word results:
22142036_z0aq.jpg (8.16 KB, download number: 0)
Download attachments
2015-5-27 21:33 Upload
Insert an image from a file into a Word document ?
1 2 3 4 5 6 7 8 9 10 11 |
# @wordDocument ()
# @body ()
# @imageFromFile ({"Name": "111.jpg", "width": "249pt", "height": "119.25pt", "File": "C:\pic.jpg"})
#end
#end
#end |
Word results:
22142037_isu2.jpg (8.15 KB, download number: 0)
Download attachments
2015-5-27 21:33 Upload
Common functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 29 30 31 32 33 |
# @wordDocument ()
# @body ()
# @bookmark ("My Bookmarks")
#set ($content = "Bookmark Here")
#h ($content,
{"Font": {"name": "Founder Yao Body", "size": "$", "color": "FF0000", "bold": "", "highlight": "Cyan"}}
)
#end
#set ($url = "Http://my.oschina.net/tinyframework")
#set ($text = "Click here to jump to leisurely home")
# @link ($url)
#h ($text,
{"Font": {"name": "Founder Yao Body", "size": "$", "color": "FF0000", "bold": "", "highlight": "Green"}}
)
#end
#end
#end |
Word results:
22142037_pefy.jpg (17.75 KB, download number: 0)
Download attachments
2015-5-27 21:33 Upload
Outline Features
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# @wordDocument ()
# @body ()
# @outline ({"Grade": "1", "Bookmarkno": "_toc372801234", "name": "1th Chapter Object Primer", "font": {"name": "Founder Yao Body", "size": "", "" Color " : "FF0000", "bold": "", "highlight": "Cyan"}}) # # Defines the first level of the outline, the name is: The 1th chapter of the object, the outline ID: _toc372801234
# @outline ({"Grade": "2", "Bookmarkno": "_toc2ny301234", "name": "1.1 Abstract Progress"})
# @outline ({"Grade": "3", "Bookmarkno": "_toc37ahy2234", "name": "1.1.1 Note Document"})
#end
#end
# @outline ({"Grade": "2", "Bookmarkno": "_toc3728mi734", "Name": "Interface of 1.2 objects"})
# @outline ({"Grade": "3", "Bookmarkno": "_toc37280me84", "name": "1.2.1 Collection and Inheritors"})
#end
#end
# @outline ({"Grade": "2", "Bookmarkno": "_toc3plk23234", "name": "1.3 Scheme Reuse"})
#end
#end
# @outline ({"Grade": "1", "Bookmarkno": "_toc37281nju4", "Name": "2nd Chapter Everything is Object"})
#end
#end
#end |
Word results:
22142037_egst.jpg (11.27 KB, download number: 0)
Download attachments
2015-5-27 21:33 Upload
Index
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# @wordDocument ()
# @body ()
#cataHeader ("Table of Contents", {"Font": "Arial", "Color": "00b050", "size": "+", "bold": "On"})
#catalogue ({"Begin": "", "Grade": "Ten", "Bookmarkno": "_toc374566201", "PageNo": "1", "Content": "1. Getting Started with objects"})
#catalogue ({"Grade": "A", "Bookmarkno": "_toc374566202", "PageNo": "1", "Content": "1.1 Abstract Progress"})
#catalogue ({"Grade": "+", "Bookmarkno": "_toc374566203", "PageNo": "1", "Content": "1.1.1 Note Document"})
#catalogue ({"Grade": "$", "Bookmarkno": "_toc374566204", "PageNo": "1", "Content": "Interface of 1.2 objects"})
#catalogue ({"Grade": "+", "Bookmarkno": "_toc374566205", "PageNo": "1", "Content": "1.2.1 Collection and Inheritors"})
#catalogue ({"Grade": "$", "Bookmarkno": "_toc374566206", "PageNo": "1", "Content": "1.3 Scheme Reuse"})
#cataEnd ()
# @outline ({"Grade": "1", "Bookmarkno": "_toc374566201", "Name": "Object Getting Started", "multilevel": {"level": "0", "Ilfo": "$", "char" : "1"}) #end
# @outline ({"Grade": "2", "Bookmarkno": "_toc374566202", "name": "Abstract Progress", "multilevel": {"Level": "1", "Ilfo": "$", "char" ":" 1.1 "}}) #end
# @outline ({"Grade": "3", "Bookmarkno": "_toc374566203", "name": "Comment Document", "multilevel": {"Level": "2", "Ilfo": "$", "char" : "1.1.1"}) #end
# @outline ({"Grade": "2", "Bookmarkno": "_toc374566204", "Name": "Interface of Object", "multilevel": {"Level": "1", "Ilfo": "$", "char" ":" 1.2 "}}) #end
# @outline ({"Grade": "3", "Bookmarkno": "_toc374566205", "Name": "Collection and Inheritors", "multilevel": {"Level": "2", "Ilfo": "41", " Char ":" 1.2.1 "}}) #end
# @outline ({"Grade": "2", "Bookmarkno": "_toc374566206", "name": "Reuse of Scheme", "multilevel": {"Level": "1", "Ilfo": "41", " Char ":" 1.3 "}}) #end
#end
#end |
Word Effects:
22142038_erjd.jpg (12.91 KB, download number: 0)
Download attachments
2015-5-27 21:33 Upload
Paragraph:
1 2 3 4 5 6 7 8 9 Ten 14 br>15 + 32 + ( ) - - - - - $ PNs |
# @wordDocument () # @body () #p ("Normal paragraph test") #p ("Align Left", {" Align ":" Left "}) #p (" center ", {" Align ":" Center "}) #p (" Align Right ", {" Align ":" R "}) #p (" Justify both ends justify justified justify justify justified justify justified justify justify justify justified justify justify align justified justify justified end-to-end justification align justify justification #p ("scatter", {"align": "Distribute"}), and align both ends aligned #p ("founder Yao Body", {"font": {"name": "Founder Yao Body"}}) #p ("Font" {"48th", {"fonts": {"size": ""}} " #p (" Red ", {" Font " : {"Color": "FF0000"}) #p ("bold", {"font": {"bold": "}}) #p (" italic ", {" font ": {" Incline ":"}}) #p (" Strikethrough ", {" font ": {" strike ":" "}}) #p (" Double underscore ", {" font ": {" U ":" Double "}) #p (" Yellow highlight "{" Font ": {" Highlight ":" Yellow "}}) #p (" indent First ", {" indent ": {" Firstline ":" "}}) #end #end |
Word Effects:
22142038_f4pa.jpg (9.47 KB, download number: 0)
Download attachments
2015-5-27 21:33 Upload
Form
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 04T 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 09u 298 299 300 301 60W 60V 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# @wordDocument ()
# @body ()
#p ("table Test 1, two rows two columns, cell height and width auto", {"font": {"size": "", "" Color ":" FF0000 "," bold ":"}})
# @table ()
# @tableRow ()
# @tableCell ()
#p (cell 11 cell 11 cell 11 cell 11 cell 11)
#end
# @tableCell ()
#p ("Cell 12")
#end
#end
# @tableRow ()
# @tableCell ()
#p ("cell 21")
#end
# @tableCell ()
#p (" Cell 22 Cell 22 cell 22 cell 22 cell 22 cell 22 cell 22 cell 22 cell 22 cell 22 cell 22 cell 22 cell 22 Cell 22 cell 22 cells 22 cell 22 cell 22 cell 22 cell 22 cell 22 Cell 22 cell 22 cells, cells, cells, cells, and cells. 22 Prompt 22 cell cell 22 cell 22 cell 22 cell 22 cell 22 cell 22 cell 22 cell 22 cell 22 cell 22 cell 22 cell 22 cell 22 cell 22 cell 22 Cell 22 cell 22 Cells 22 cells + cells cells
#end
#end
#end
#p ("Table Test 2, two rows two columns, height automatic", {"font": {"size": "", "" Color ":" FF0000 "," bold ":" "}})
# @table ()
# @tableRow ()
# @tableCell ({"span": {"width": "4000"}}) # #单元格宽度 4000
#p ("Cell 11")
#end
# @tableCell ({"span": {"width": "+"}}) # #单元格宽度 3000
#p ("Cell 12")
#end
#end
# @tableRow ()
# @tableCell ({"span": {"width": "4000"}}) # #单元格宽度 4000
#p ("cell 21")
#end
# @tableCell ({"span": {"width": "+"}}) # #单元格宽度 3000
#p ("cell 22")
#end
#end
#end
#p ("Table Test 3, two rows two columns, Width automatic", {"font": {"size": "", "" Color ":" FF0000 "," bold ":" "}})
# @table ()
# @tableRow ()
# @tableCell ()
#p ("Cell 11")
#end
# @tableCell ()
#p ("Cell 12")
#end
#end
# @tableRow ()
# @tableCell ()
#p ("cell 21")
#end
# @tableCell ()
#p ("cell 22")
#end
#end
#end
#p ("Table test 4, the border type is single-line, the color is green, the width is", {"font": {"size": "", "" "", "" "," FF0000 "," bold ":"}})
# @table ({"borders": {"Wval": "single", "Color": "92d050", "Size": "30"}})
# @tableRow ()
# @tableCell ()
#p ("Cell 11")
#end
# @tableCell ()
#p ("Cell 12")
#end
#end
# @tableRow ()
# @tableCell ()
#p ("cell 21")
#end
# @tableCell ()
#p ("cell 22")
#end
#end
#end
#p ("table test 5, Border type is double dash, color is purple, width is", {"font": {"size": "", "" "", "" "," "", "" FF0000 "," bold ":"}})
# @table ({"borders": {"wval": "Double", "Color": "7030a0", "Size": "20"}})
# @tableRow ()
# @tableCell ()
#p ("Cell 11")
#end
# @tableCell ()
#p ("Cell 12")
#end
#end
# @tableRow ()
# @tableCell ()
#p ("cell 21")
#end
# @tableCell ()
#p ("cell 22")
#end
#end
#end
#p ("Table Test 6,", {"font": {"size": "+", "Color": "FF0000", "bold": ""}})
# @table ({"shading": {"wval": "Solid", "Color": "92d050", "Fill": "Auto"})
# @tableRow ()
# @tableCell ({"shading": {"wval": "Solid", "Color": "FFFF00", "Fill": "Auto"},
"Span": {"width": "6000", "type": "DXA"}})
#p ("Cell 11")
#end
# @tableCell ()
#p ("Cell 12")
#end
#end
# @tableRow ()
# @tableCell ()
#p ("cell 21")
#end
# @tableCell ()
#p ("cell 22")
#end
#end
#end
#p ("Table Test 7,", {"font": {"size": "+", "Color": "FF0000", "bold": ""}})
# @table ({"shading": {"wval": "Solid", "Color": "92d050", "Fill": "Auto"})
# @tableRow ()
# @tableCell ()
#p ("Cell 11")
#end
# @tableCell ({"borders": {"Wval": "Thin-thick-medium-gap", "width": "+", "Color": "00b050"},
"Shading": {"wval": "Solid", "Color": "FF0000", "Fill": "FF0000"}})
#p ("Cell 12")
#end
#end
# @tableRow ()
# @tableCell ()
#p ("cell 21")
#end
# @tableCell ()
#p ("cell 22")
#end
#end
#end
#p ("Table Test 8,", {"font": {"size": "+", "Color": "FF0000", "bold": ""}})
# @table ({"shading": {"wval": "Solid", "Color": "92d050", "Fill": "Auto"})
# @tableRow ()
# @tableCell ({"borders": {"Wval": "Dash-dot-stroked", "width": "+", "Color": "e36c0a"},
"Shading": {"wval": "Solid", "Color": "FFFF00", "Fill": "Auto"})
#p ("Cell 11")
#end
# @tableCell ()
#p ("Cell 12")
#end
#end
# @tableRow ()
# @tableCell ()
#p ("cell 21")
#end
# @tableCell ()
#p ("cell 22")
#end
#end
#end
#end
#end |
Word Effects:
22142039_npm8.jpg (16.74 KB, download number: 0)
Download attachments
2015-5-27 21:33 Upload
Summary from the above example, it is indeed convenient to include a Word document of various elements. For the word meta that is not supported in the framework, it can also be easily extended, the extension process does not need to write programs, just write the corresponding macro file.
Maven dependency Coordinates:
1 2 3 4 5 |
<dependency> <groupId>org.tinygroup</groupId> <artifactId>docgen</artifactId> <version>0.0.12</version> </dependency> |
The latest version of the source code online view address:
Https://git.oschina.net/tinyframework/tiny
Https://git.oschina.net/tinyfram ... Rg.tinygroup.docgen
Document Generation Framework