您的位置:首页 > 其它

POI之根据模板导出word-yellowcong

2017-11-21 16:46 459 查看
根据模版导出word这个东西在实际开发中,是很常用的,我今天遇到坑了,就是如何根据word模版生成word。网上的代码好多都是坑爹,不是缺环境,就是却方法,所以我自己做了一个小例子。如果希望有图片替换的教程,大家可以给我留言

环境搭建

<!-- excel -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>

<!-- word -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.17</version>
</dependency>

<!-- xlsx -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<!-- xlsx  依赖这个包 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.17</version>
</dependency>


帐票模版



实现代码

package com.yellowcong.dao;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;

/**
* @version $Id$
*/
public class POITest {
public static void main(String[] args) throws Exception, IOException {

String file = "C:/Users/zhangrw/Desktop/test/指名業者指名理由書.doc";

//获取到文档
HWPFDocument doc = new HWPFDocument(new FileInputStream(file));
Range range = doc.getRange();
//查看段落数量
int paraNum = range.numParagraphs();
for(int i=0;i<paraNum;i++){
//获取段落
Paragraph paragraph =range.getParagraph(i);
//当存在替换符号的情况

if(paragraph.text().indexOf("$")>-1){
paragraph.replaceText("$共通.契約名$", "yellowcong");
paragraph.replaceText("$共通.施工場所$", "yellowcong");
paragraph.replaceText("$案件.工事担当課$", "NB");
}
}

//保存doc文件
FileOutputStream out = new FileOutputStream(new File("C:/Users/zhangrw/Desktop/test/demo2.doc"));
doc.write(out);
out.close();
}

}


优化后的代码

只需要提供输入、输出、替换的代码三个参数,就可以完成张票的生成

package com.yellowcong.dao;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;

/**
*
*
* @version $Id$
*/
public class POITest {
/**
* @param args
* @throws Exception
* @throws IOException
*/
public static void main(String[] args) throws Exception, IOException {

String inFile = "C:/Users/zhangrw/Desktop/test/指名業者指名理由書.doc";
String outFile = "C:/Users/zhangrw/Desktop/test/demo3.doc";

//需要替换的模版代码
Map<String,String> map = new HashMap<String,String>();
map.put("$共通.契約名$", "yellowcong");
map.put("$共通.施工場所$", "yellowcong");
map.put("$案件.工事担当課$", "NB");

//获取修改模版后的代码
HWPFDocument doc = createTemplateDoc(inFile, map);

//保存doc文件
saveDoc(doc,outFile);
}
/**
* 输出文件
* @param doc
* @param outPath
*/
private static void saveDoc(HWPFDocument doc,String outPath){
FileOutputStream out = null;
try {
out = new FileOutputStream(new File(outPath));
doc.write(out);
} catch (Exception e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}finally{
if(out != null){
try {
out.close();
} catch (IOException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
}
}
}
/**
根据代码的code,格式化输出数据
* @param file
* @param map
* @return
* @throws IOException
* @throws FileNotFoundException
*/
private static HWPFDocument createTemplateDoc(String file, Map<String, String> map) throws IOException,
FileNotFoundException {
//获取到文档
HWPFDocument doc = new HWPFDocument(new FileInputStream(file));
Range range = doc.getRange();
//查看段落数量
int paraNum = range.numParagraphs();
for(int i=0;i<paraNum;i++){
//获取段落
Paragraph paragraph =range.getParagraph(i);
if(paragraph.text().indexOf("$")>-1){
for(Map.Entry<String, String> entry:map.entrySet()){
paragraph.replaceText(entry.getKey(), entry.getValue());
}
}
}
return doc;
}

}


替换后效果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: