读书人

POI依据模版导出word

发布时间: 2012-08-02 11:35:26 作者: rapoo

POI根据模版导出word

最近公司给我下达的任务越来越有意思了。这两天要做的是一个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文件

代码如下:

<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>

1楼lfmilaoshi17小时前
7、、、、文章结束的画龙点睛。。。。文尾的总结。。。

读书人网 >其他相关

热点推荐