OO模版技术输出文档Web新法
前言:
近日,事务繁忙,无暇写作,正式声明并非“封笔”。愿对诸位matrixer兄弟能予以善意的谅解。早在去年的某个时候,本人钻研“jacob”时写下了篇有关“jacob生成pdf文档”的文章发表在matrix上,不时收到热心开发者的ask,问题集中于jacob的要害――开发难度大。对于不熟知.net和java技术的开发者,若想领会文中的含义,谈何容易(幸亏当时本人瞥了瞥.net),不然一头雾水更无法行文讲解了。面对jacob冷峻的面庞,cleverpig不禁慨叹:“中国人历来有刻骨铭心的苦难史,就不要让程序员再来过了吧。”
此问题藏于脑中,时时煎熬,不想一日终有斩获便持执笔写来和大家共享。
软件准备:
oo:是openoffice的简称,而非众人皆知的面向对象,有关它的开发文档有好几沓,在此不多言了。别忘了先安装一下啊,否则无法进行类型转换的。oo支持的文档类型:odt、ods等,对应熟知的office文档类型:doc、xls。
jooreports:是基于oo文档模版的文档生成软件,支持动态替换和freemarker el、collection的文档重复替换,附随源代码提供了比较精简的api和doc,但源代码中的example很值得一看。
jooconvert:毋庸置疑就是配套的文档转换软件,将文档托管给oo进行转换,然后获得转换后的文档,支持多种类型文档:odt、ods、doc、rtf、txt、pdf、xls。后面的代码中有详细介绍。
代码框架:
信手写来也称不上“框架”,总体上分了三部分:调用jooreports/jooconvert的核心类、基于strieps框架的web实现、必不可少的unittest。
核心部分:
documenttemplateparam:文档生成参数类。
documentconvertparam:文档转换参数类。
convertcontext:用于连接oo网络服务的转换上写文类。
documentgenerator:文档生成类。
documentformatconverter:文档格式转换类。
之所以分如此细致,也许是本人的“重构癖”作祟吧。但扩展起来是很具灵性的。如果martin flower大人看到,可能感激落泪吧。
web实现:很简单的stripes框架实现的产品登记输出为doc、pdf、xls格式的文件,在此抛砖引玉啦。
product:产品bean类。
productdetailactionbean:实现产品登记的actionbean类。
web-inf/filetemplate:odt、ods文档模版保存位置。
unittest:对核心部分进行测试,当然stripes也可作测试,但应用简单懒于编写了。
编写文档模版:
oo在设计时考虑了通用性,所以每个odt、ods文件都是以zip的压缩模式保存,只要用相关软件便可以观其状了。content.xml尤为重要,其中保存了模版数据,如hello-template.odt:
[iocblog.net 来源]
<?xml version="1.0" encoding="utf-8"?>
<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:
xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:
xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
xmlns:xlink=http://www.w3.org/1999/xlink
xmlns:dc=http://purl.org/dc/elements/1.1/
xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"
xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"
xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0"
xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0"
xmlns:math=http://www.w3.org/1998/math/mathml
xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0"
xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0"
xmlns:ooo=http://openoffice.org/2004/office
xmlns:ooow=http://openoffice.org/2004/writer
xmlns:oooc=http://openoffice.org/2004/calc
xmlns:dom=http://www.w3.org/2001/xml-events
xmlns:xforms=http://www.w3.org/2002/xforms
xmlns:xsd=http://www.w3.org/2001/xmlschema
xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" office:version="1.0">
<office:scripts/>
<office:font-face-decls>
<style:font-face style:name="tahoma1" svg:font-family="tahoma"/>
<style:font-face style:name="tahoma" svg:font-family="tahoma"
style:font-pitch="variable"/><style:font-face style:name="瀹��"
svg:font-family="瀹��" style:font-pitch="variable"/>
<style:font-face style:name="times new roman"
svg:font-family="'times new roman'"
style:font-family-generic="roman" style:font-pitch="variable"/>
<style:font-face style:name="arial" svg:font-family="arial"
style:font-family-generic="swiss" style:font-pitch="variable"/>
</office:font-face-decls>
<office:automatic-styles>
<style:style style:name="p1" style:family="paragraph"
style:parent-style-name="standard">
<style:paragraph-properties fo:text-align="center"
style:justify-single-word="false"/>
<style:text-properties fo:font-weight="bold"
style:language-asian="zh" style:country-asian="cn"
style:font-weight-asian="bold" style:font-weight-complex="bold"/>
</style:style>
<style:style style:name="p2" style:family="paragraph"
style:parent-style-name="standard">
<style:text-properties style:language-asian="zh" style:country-asian="cn"/>
</style:style><style:style style:name="t1" style:family="text">
<style:text-properties fo:font-weight="bold"
style:font-weight-asian="bold" style:font-weight-complex="bold"/>
</style:style>
</office:automatic-styles>
<office:body><office:text>[iocblog.net 来源]
<text:sequence-decls>
<text:sequence-decl text:display-outline-level="0" text:name="illustration"/>
<text:sequence-decl text:display-outline-level="0" text:name="table"/>
<text:sequence-decl text:display-outline-level="0" text:name="text"/>
<text:sequence-decl text:display-outline-level="0" text:name="drawing"/>
</text:sequence-decls>
<text:p text:style-name="p1">浜у�娉ㄥ�琛?/text:p>
<text:p text:style-name="p2">
<text:span text:style-name="t1">娉ㄥ�浜猴�</text:span>${username}[#if username="cleverpig"],haha[/#if]
</text:p>
[#list productlist as prod]
<text:p text:style-name="p2">
<text:span text:style-name="t1">浜у���о锛?/text:span>${prod.name}
<text:span text:style-name="t1">浜у�缂��锛?/text:span>${prod.number}
</text:p>
[/#list]
</office:text></office:body></office:document-content>
其中的乱码是因utf-8编码造成。文档风格就是xml分割,但其中红色代码使用了freemarker的表达式风格,这也是jooreport所支持的。具体详见freemarker和jooconvert manual。编写模版时,建议使用设计好格式的文档,然后使用文本编辑器在其中插入表达式,切勿在oo中直接编写表达式,因为oo会对其进行转换,使其失效。
转换文档时启动oo后台服务:
在调用jooreport生成文档时无需启动oo,因为与oo服务无关;而调用jooconvert进行文档转换时就需要在控制台中输入一下:
cd %openoffice_home%program
soffice.exe -headless -accept=socket,port=8100,host=localhost;urp
openoffice_home代表了oo安装时的目录。
Tag: 模版
文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。