选择显示字体大小

基于oo模版技术的文档web输出新法

基于oo模版技术的文档web输出新法

作者:cleverpig(http://blog.matrix.org.cn/page/cleverpig)


前言:
        近日,事务繁忙,无暇写作,正式声明并非“封笔”。愿对诸位matrixer兄弟能予以善意的谅解。早在去年的某个时候,本人钻研“jacob”时写下了篇有关“jacob生成pdf文档”的文章发表在matrix上,不时收到热心开发者的ask,问题集中于jacob的要害——开发难度大。对于不熟知.netjava技术的开发者,若想领会文中的含义,谈何容易(幸亏当时本人瞥了瞥.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:
<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
<office:document-content xmlns:office=&quot;urn:oasis:names:tc:opendocument:xmlns:office:1.0&quot; xmlns:style=&quot;urn:oasis:names:tc:opendocument:xmlns:style:1.0&quot; xmlns:text=&quot;urn:oasis:names:tc:opendocument:xmlns:text:1.0&quot; xmlns:table=&quot;urn:oasis:names:tc:opendocument:xmlns:table:1.0&quot; xmlns:draw=&quot;urn:oasis:names:tc:opendocument:xmlns:drawing:1.0&quot; xmlns:fo=&quot;urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot; xmlns:dc=&quot;http://purl.org/dc/elements/1.1/&quot; xmlns:meta=&quot;urn:oasis:names:tc:opendocument:xmlns:meta:1.0&quot; xmlns:number=&quot;urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0&quot; xmlns:svg=&quot;urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0&quot; xmlns:chart=&quot;urn:oasis:names:tc:opendocument:xmlns:chart:1.0&quot; xmlns:dr3d=&quot;urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0&quot; xmlns:math=&quot;http://www.w3.org/1998/math/mathml&quot; xmlns:form=&quot;urn:oasis:names:tc:opendocument:xmlns:form:1.0&quot; xmlns:script=&quot;urn:oasis:names:tc:opendocument:xmlns:script:1.0&quot; xmlns:ooo=&quot;http://openoffice.org/2004/office&quot; xmlns:ooow=&quot;http://openoffice.org/2004/writer&quot; xmlns:oooc=&quot;http://openoffice.org/2004/calc&quot; xmlns:dom=&quot;http://www.w3.org/2001/xml-events&quot; xmlns:xforms=&quot;http://www.w3.org/2002/xforms&quot; xmlns:xsd=&quot;http://www.w3.org/2001/xmlschema&quot; xmlns:xsi=&quot;http://www.w3.org/2001/xmlschema-instance&quot; office:version=&quot;1.0&quot;><office:scripts/><office:font-face-decls><style:font-face style:name=&quot;tahoma1&quot; svg:font-family=&quot;tahoma&quot;/><style:font-face style:name=&quot;tahoma&quot; svg:font-family=&quot;tahoma&quot; style:font-pitch=&quot;variable&quot;/><style:font-face style:name=&quot;瀹嬩綋&quot; svg:font-family=&quot;瀹嬩綋&quot; style:font-pitch=&quot;variable&quot;/><style:font-face style:name=&quot;times new roman&quot; svg:font-family=&quot;&apos;times new roman&apos;&quot; style:font-family-generic=&quot;roman&quot; style:font-pitch=&quot;variable&quot;/><style:font-face style:name=&quot;arial&quot; svg:font-family=&quot;arial&quot; style:font-family-generic=&quot;swiss&quot; style:font-pitch=&quot;variable&quot;/></office:font-face-decls><office:automatic-styles><style:style style:name=&quot;p1&quot; style:family=&quot;paragraph&quot; style:parent-style-name=&quot;standard&quot;><style:paragraph-properties fo:text-align=&quot;center&quot; style:justify-single-word=&quot;false&quot;/><style:text-properties fo:font-weight=&quot;bold&quot; style:language-asian=&quot;zh&quot; style:country-asian=&quot;cn&quot; style:font-weight-asian=&quot;bold&quot; style:font-weight-complex=&quot;bold&quot;/></style:style><style:style style:name=&quot;p2&quot; style:family=&quot;paragraph&quot; style:parent-style-name=&quot;standard&quot;><style:text-properties style:language-asian=&quot;zh&quot; style:country-asian=&quot;cn&quot;/></style:style><style:style style:name=&quot;t1&quot; style:family=&quot;text&quot;><style:text-properties fo:font-weight=&quot;bold&quot; style:font-weight-asian=&quot;bold&quot; style:font-weight-complex=&quot;bold&quot;/></style:style></office:automatic-styles><office:body><office:text><text:sequence-decls><text:sequence-decl text:display-outline-level=&quot;0&quot; text:name=&quot;illustration&quot;/><text:sequence-decl text:display-outline-level=&quot;0&quot; text:name=&quot;table&quot;/><text:sequence-decl text:display-outline-level=&quot;0&quot; text:name=&quot;text&quot;/><text:sequence-decl text:display-outline-level=&quot;0&quot; text:name=&quot;drawing&quot;/></text:sequence-decls>
<text:p text:style-name=&quot;p1&quot;>浜у搧娉ㄥ唽琛?/text:p>
<text:p text:style-name=&quot;p2&quot;><text:span text:style-name=&quot;t1&quot;>娉ㄥ唽浜猴細</text:span>${username}&#91;#if username=&quot;cleverpig&quot;&#93;,haha&#91;/#if&#93;</text:p>
&#91;#list productlist as prod&#93;
<text:p text:style-name=&quot;p2&quot;>
<text:span text:style-name=&quot;t1&quot;>浜у搧鍚嶇о锛?/text:span>${prod.name}
<text:span text:style-name=&quot;t1&quot;>浜у搧缂栧彿锛?/text:span>${prod.number}
</text:p>
&#91;/#list&#93;
</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安装时的目录。

代码下载:
        由于代码很完整,size为7m,所以分为了6份:
        [下载文件]
        [下载文件]
        [下载文件]
        [下载文件]
        [下载文件]
        [下载文件]

参考资源:
jooreports
freemarker
openoffice
matrix


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons