模板word
1.先把这个模板word转化为xml(这个太简单就不做介绍了)
2.利用freemarker模板引擎,java代码实现xml转化word
引入依赖
<dependency>
<groupId>freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.8</version>
</dependency>
代码实现
package com.dc.esb.Word;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.util.ResourceUtils;
import java.io.*;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
/**
* @auther zhanggt
*/
public class XMLtoWord {
/**
* 读取文件
*
* @param path
* @return
* @throws IOException
*/
private static String ReadFile(String path) throws IOException {
File file = new File(path);
FileInputStream fis = null;
StringBuffer sb = new StringBuffer();
byte[] bytes = new byte[1024];
try {
fis = new FileInputStream(file);
int len;
while ((len = fis.read(bytes)) != -1) {
sb.append(new String(bytes, 0, len, Charset.defaultCharset()));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fis != null) {
fis.close();
}
}
return sb.toString();
}
/**
* 写入文件
*
* @param content
* @param path
*/
private static void WriteFile(String content, String path) {
File file = new File(path);
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
fos = new FileOutputStream(file, false);
bos = new BufferedOutputStream(fos);
bos.write(content.getBytes(Charset.defaultCharset()));
bos.flush();
} catch (FileNotFoundException e) {
System.err.println("文件写入错误");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
if (bos != null) {
bos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 生成word文件
* @param tempPath
* @param tempName
* @param newWordPath
* @throws IOException
* @throws TemplateException
*/
private static void generateWord(String tempPath, String tempName, String newWordPath) throws IOException, TemplateException {
Writer outWriter = null;
Configuration configuration = new Configuration();
configuration.setDefaultEncoding(Charset.defaultCharset().toString());
try {
configuration.setDirectoryForTemplateLoading(new File(tempPath));
Template template = configuration.getTemplate(tempName, Charset.defaultCharset().toString());
outWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(newWordPath)),Charset.defaultCharset().toString()));
template.process(new HashMap<String,String>(),outWriter);
} catch (IOException e) {
throw e;
} catch (TemplateException e1) {
throw e1;
}finally {
if(outWriter!=null){
outWriter.close();
}
}
}
}
测试代码
public static void main(String[] args) throws Exception {
String file = ReadFile(new File("").getCanonicalPath()+"//src//main//resources//xmmb.xml");
file = file.replaceAll("<w:t>Project</w:t>","<w:t>"+"测试项目"+"</w:t>");
WriteFile(file,new File("").getCanonicalPath()+"//src//main//resources//xmmbtemp.xml");
generateWord(new File("").getCanonicalPath()+"//src//main//resources//","xmmbtemp.xml",new File("").getCanonicalPath()+"//src//main//resources//结果.doc");
}
展示结果:(这里我们只改了project)