---
title: "&#22522;&#20110;OO&#27169;&#29256;&#25216;&#26415;&#30340;&#25991;&#26723;Web&#36755;&#20986;&#26032;&#27861;"
author: "cleverpig"
date: "2006-12-11T01:14:41Z"
tags: []
slug: "22522-20110-oo-27169-29256-25216-26415-30340-25991-26723-web-36755-20986-26032-27861"
draft: false
lastmod: "2006-12-11T01:14:41Z"
id: 44705
url: "https://cleverpig.iteye.com/blog/44705"
---

基于OO模版技术的文档Web输出新法  
作者：cleverpig([http://blog.matrix.org.cn/page/cleverpig](../page/cleverpig))

**前言：**  
         近日，事务繁忙，无暇写作，正式声明并非“封笔”。愿对诸位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：

```
<?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="&apos;Times New Roman&apos;" 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><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安装时的目录。

**代码下载：**  
        由于代码很完整，size为7M，所以分为了6份：  
        \[[下载文件](http://www.matrix.org.cn/resource/upload/forum/2006_06_15_150658_XPaOkRfywC.rar)]  
        \[[下载文件](http://www.matrix.org.cn/resource/upload/forum/2006_06_15_150741_UgCXjYpAji.rar)]  
        \[[下载文件](http://www.matrix.org.cn/resource/upload/forum/2006_06_15_150811_UUbKgQZTst.rar)]  
        \[[下载文件](http://www.matrix.org.cn/resource/upload/forum/2006_06_15_150902_UOvKUdmdCW.rar)]  
        \[[下载文件](http://www.matrix.org.cn/resource/upload/forum/2006_06_15_150941_QtcuiibHNe.rar)]  
        \[[下载文件](http://www.matrix.org.cn/resource/upload/forum/2006_06_15_150957_jpWWyVzlNi.rar)]

**参考资源：**  
[JOOReports](http://jooreports.sourceforge.net/)  
[freemarker](http://www.freemarker.org/docs/)  
[OpenOffice](http://www.openoffice.org/)  
[Matrix](http://www.matrix.org.cn/)