1.pom引入依赖:
java
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.10.5</version>
</dependency>
2.写工具类
java
package *****;
import com.deepoove.poi.XWPFTemplate;
import java.io.FileOutputStream;
import java.io.IOException;
public class GenerateWordUtils {
/**
*
* @param path 文件模板路径
* @param fileName 生成的文件名
* @param param 需要替换的对象
*/
public static String generateWord(String path,String fileName,Object param) {
try {
XWPFTemplate template = XWPFTemplate.compile(path).render(param);
template.writeAndClose(new FileOutputStream(fileName));
} catch (IOException e) {
e.printStackTrace();
}
return fileName;
}
}
3.调用
例子:
java
//相对路径在项目根目录
String path = GenerateWordUtils.generateWord(new String(模板路径path.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8), 文件名fileName, 需替换的结果集resultMap);
//项目路径
String projectPath=System.getProperty("user.dir");
//文件真实路径
String realPath = projectPath+File.separator+path