最近公司给我下达的任务越来越有意思了。这两天要做的是一个jquery的样式的问题,改成tab的样式。这个本不难,最麻烦的是业务。呵呵,当然这个不是今天博客的重点哈。
前几天刚刚完成的一个任务是使用POI根据模版导出word,用了一天半的时间才搞完。开始觉得肯定是个技术活,于是十分happy的上网查资料,找了一圈又一圈,始终不明白怎么回事,最后看到一个网址才明白,原来是这么回事:(先把我的参考网址贴过来)
http://topic.csdn.net/u/20110415/17/6cfbe1a1-ccdf-420e-b868-38b9a4ca13e0.html(最后一句,将模版存为xml文件,然后改写xml)
将模版存为xml文件,然后改写xml,这句话就是我解决问题的重点,当然确定了以后也就不再是技术问题,而是一个力气活了。呵呵。
具体过程如下:
1、定义word模版文件:
打开你的模版word,另存为xml格式的,根据需要,将相应的标签加一个attribute ,比如id,为了方便以后的操作。然后放到一个目录下(为后面的调用准备)
2、准备读写xml文件的环境,和poi环境
我使用的是dom4j,所以就将dom4j的jar包拷到项目中就可以了。将poi相关jar包拷入项目中
3、读写xml文件
代码如下:
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
String destFilePath = request.getSession().getServletContext()
.getRealPath("\\");
//用dom4j读写xml文件(模版文件)
SAXReader reader = new SAXReader();
//模版文件为 : conf\\testReportTemplate.xml
Document document = reader.read(new File(destFilePath+"WEB-INF\\testReportTemplate.xml"));
Element root = document.getRootElement();
//读取相关节点信息(以下的这几个标签在xml模版文件中都是w:body,w:sect等这样的形式,但是读取的时候不能加w:,并且在增加内容的时候一定要加上w:,比如下面我们经常用到的增加p、r、t,就是要用w:p w:r w:t。
Element wbody = root.element("body");
Element wsect = wbody.element("sect");
//读取相关section的信息,也就是模版的具体内容,将要往里面填充数据内容的区域
List<Element> sections = wsect.elements("sub-section");
//循环section,根据section标签的ID值,得到相关的区域,将数据填充进来
for (Element eleSection : sections) {
String eleSectionId = eleSection.attributeValue("id");
if ("testMethodSection".equalsIgnoreCase(eleSectionId)) {
if (testingWorkInfoVO.getTestingMethod() != null) {
eleSection.addElement("w:p")
.addElement("w:r")
.addElement("w:t")
.setText(testingWorkInfoVO.getTestingMethod());//在数据库中读取的数据
}
continue;
}
if ("testContentSection".equalsIgnoreCase(eleSectionId)) {
if (testingWorkInfoVO.getTestingContent() != null) {
eleSection.addElement("w:p")
.addElement("w:r")
.addElement("w:t")
.setText(testingWorkInfoVO.getTestingContent());//在数据库中读取的数据
}
continue;
}
}
4、poi导出为word
ByteArrayInputStream bais = new ByteArrayInputStream(document.asXML().getBytes("utf-8"));
POIFSFileSystem fs = new POIFSFileSystem();
DirectoryEntry directory = fs.getRoot();
DocumentEntry de = directory.createDocument("WordDocument", bais);
//文件输出流
OutputStream fos = response.getOutputStream();
//文件输出相关信息
String outputFileName = null; //输出文件名
String destFile = destFilePath + "word_template\\"; //输出文件地址;
outputFileName = "测试报告" + ".doc";
destFile = destFilePath + outputFileName; //文件的下载地址
response.setContentType("application/octet-stream");
response.setHeader("name", destFile);
response.setHeader("Content-disposition", "attachment; filename=\""
+ URLEncoder.encode(outputFileName, "UTF-8") + "\""); //
fos.flush();
fs.writeFilesystem(fos);
bais.close();
fos.flush();
fos.close();
5、基本上就完事了。呵呵。有问题的随时给我留言,我会很快给您答复。
6、附加:xml格式的word模版的xml代码稍微贴一段过来(跟我上面的例子不一致):
<w:document mc:Ignorable="w14 wp14" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
<w:body><w:p w:rsidR="007B7334" w:rsidRDefault="00FB1288" w:rsidP="00FB1288"><w:pPr><w:pStyle w:val="2"/><w:rPr><w:rFonts w:hint="eastAsia"/></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="eastAsia"/></w:rPr><w:t>一、</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/><w:r><w:rPr><w:rFonts w:hint="eastAsia"/></w:rPr><w:t>测试模版一</w:t></w:r></w:p><w:p w:rsidR="00FB1288" w:rsidRDefault="00FB1288" w:rsidP="00FB1288"><w:pPr><w:rPr><w:rFonts w:hint="eastAsia"/></w:rPr></w:pPr></w:p><w:p w:rsidR="00FB1288" w:rsidRDefault="00FB1288" w:rsidP="00FB1288"><w:pPr><w:rPr><w:rFonts w:hint="eastAsia"/></w:rPr></w:pPr></w:p><w:p w:rsidR="00FB1288" w:rsidRDefault="00FB1288" w:rsidP="00FB1288"><w:pPr><w:rPr><w:rFonts w:hint="eastAsia"/></w:rPr></w:pPr></w:p><w:p w:rsidR="00FB1288" w:rsidRDefault="00FB1288" w:rsidP="00FB1288"><w:pPr><w:rPr><w:rFonts w:hint="eastAsia"/></w:rPr></w:pPr></w:p><w:p w:rsidR="00FB1288" w:rsidRDefault="00FB1288" w:rsidP="00FB1288"><w:pPr><w:pStyle w:val="2"/></w:pPr><w:r><w:rPr><w:rFonts w:hint="eastAsia"/></w:rPr><w:t>二、测试模版二</w:t></w:r></w:p><w:sectPr w:rsidR="00FB1288"><w:pgSz w:w="11906" w:h="16838"/><w:pgMar w:top="1440" w:right="1800" w:bottom="1440" w:left="1800" w:header="851" w:footer="992" w:gutter="0"/><w:cols w:space="425"/><w:docGrid w:type="lines" w:linePitch="312"/></w:sectPr></w:body>
</w:document>