java 读word文档模板,同时往word文档里面插入数据

1.先看word文档模板 word文档先换成.xml格式。现阶段的word都支持另存为xml格式



2.看如下代码的作用。该段断码,只是传一个文件的路径。我们就能读取文件中的所有内容。

private static String getTemplateContext(String templatePath) {
try {
return FileUtils.readFileToString(FileUtils.getFile(templatePath), "UTF-8");                 //获取传入文件路径, 这里即为该模板中的内容。
} catch (FileNotFoundException e) {
log.debug("读取模板文件异常");
return null;
} catch (IOException e) {
log.debug("读取模板文件异常");
return null;
}
}


3.由于2中获取的内容含有${.....}  我们需要将这些$转换成我们需要的内容。如下代码就可以满足我们的需求

这里说一下 templateContext为步骤2中获取的内容。

Map propertiesMap 是一个map对象,里面为键值对  就是模板里面的${..}的值。按照步骤一。我们可以认为map对象propertiesMap为

{zhongbianzhishu=12,xingzheng=13,guyuan=14,year=2015} 

private static String replaceTemplateContent(String templateContext,
Map propertiesMap) {
int tagstart = 0;
int tagend = 0;
StringBuffer sb =  new StringBuffer();
for (tagstart = templateContext.indexOf("${"); tagstart >= 0;) {
if (tagstart > tagend) {
String content = templateContext.substring(tagend, tagstart);
sb.append(content);
}
tagend = templateContext.indexOf('}', tagstart);
if (tagend > 0) {
String key = templateContext.substring(tagstart + 2, tagend).trim();
String value = propertiesMap.get(key)==null?"":propertiesMap.get(key).toString();
sb.append(value);
tagend++;
tagstart = templateContext.indexOf("${", tagend);
} else {
tagend = tagstart;
}
}
if (templateContext.length() > tagend) {
String end = templateContext.substring(tagend);
sb.append(end);
}
return sb.toString();
}


4.步骤4的作用就是讲步骤3的内容写入到我们自己定义的文本中


/**
* 将内容写入文件
* @param content
* @param filePath
*/
public static void writeFileWithEncoding(String content, String filePath,String encoding) {
try {
if (FileUtils.createFile(filePath)){
FileWriterWithEncoding fileWriter = new FileWriterWithEncoding(filePath,encoding, true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(content);
bufferedWriter.close();
fileWriter.close();
}else{
log.info("生成失败,文件已存在!");
}
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 创建单个文件
* @param descFileName 文件名,包含路径
* @return 如果创建成功,则返回true,否则返回false
*/
public static boolean createFile(String descFileName) {
File file = new File(descFileName);
if (file.exists()) {
log.debug("文件 " + descFileName + " 已存在!");
return false;
}
if (descFileName.endsWith(File.separator)) {
log.debug(descFileName + " 为目录,不能创建目录!");
return false;
}
if (!file.getParentFile().exists()) {
// 如果文件所在的目录不存在,则创建目录
if (!file.getParentFile().mkdirs()) {
log.debug("创建文件所在的目录失败!");
return false;
}
}


// 创建文件
try {
if (file.createNewFile()) {
log.debug(descFileName + " 文件创建成功!");
return true;
} else {
log.debug(descFileName + " 文件创建失败!");
return false;
}
} catch (Exception e) {
e.printStackTrace();
log.debug(descFileName + " 文件创建失败!");
return false;
}


}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值