Spring项目将echart图导出到doc文档中
最近项目中遇到这种需求,现在将方法整理记录下来方便以后查阅,同时希望能够帮助到有需求的人。
1.创建doc文件
首先创建一个doc文档作为模板,在文档中先添加几张图片
2.将文档另存为.xml文件
注意:不要手动修改扩展名
3.打开文件,推荐使用sublime打开文件
怎么使用sublime格式化xml文件,参考这篇文章使用sublime格式化xml文件
4.修改文件内容
将图片内容删掉,改为使用如${name}这样格式的内容
5.修改文件格式
将文件格式修改为.ftl,然后直接将文件放到程序中的某个位置
6.编写java程序
这里参考的是:png导出到doc文档中
import java.io.*;
import java.lang.annotation.Target;
import java.net.URLEncoder;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
public class WordUtils {
//配置信息,代码本身写的还是很可读的,就不过多注解了
private static Configuration configuration = null;
private static String image;
private static void getTemplate(String path) {
configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
try {
//这里因为作者是要将程序打成jar包运行,所以使用这个方法
//如果只是在本地运行的话,使用 configuration.setDirectoryForTemplateLoading(new File(String path)) 即可
configuration.setClassForTemplateLoading(WordUtils.class,path);
} catch (Exception e) {
e.printStackTrace();
}
}
private WordUtils() {
throw new AssertionError();
}
public static void exportMillCertificateWord(HttpServletResponse resp, Map map, String title, String ftlFile) throws IOException {
String path = "/static/model/";
getTemplate(path);
Template freemarkerTemplate = configuration.getTemplate(ftlFile);
File file = null;
InputStream fin = null;
ServletOutputStream out = null;
try {
// 调用工具类的createDoc方法生成Word文档
file = createDoc(map, freemarkerTemplate);
fin = new FileInputStream(file);
resp.setCharacterEncoding("utf-8");
resp.setContentType("application/msword");
// 设置浏览器以下载的方式处理该文件名
String fileName = title + ".doc";
resp.setHeader("Content-Disposition", "attachment;filename="
.concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8"))));
out = resp.getOutputStream();
byte[] buffer = new byte[512]; // 缓冲区
int bytesToRead = -1;
// 通过循环将读入的Word文件的内容输出到浏览器中
while ((bytesToRead = fin.read(buffer)) != -1) {
out.write(buffer, 0, bytesToRead);
}
} finally {
if (fin != null) fin.close();
if (out != null) out.close();
if (file != null) file.delete(); // 删除临时文件
}
}
private static File createDoc(Map<?, ?> dataMap, Template template) {
String name = "sellPlan.doc";
File f = new File(name);
try {
Template t = template;
// 这个地方不能使用FileWriter因为需要指定编码类型否则生成的Word文档会因为有无法识别的编码而无法打开
Writer w = new OutputStreamWriter(new FileOutputStream(f), "utf-8");
t.process(dataMap, w);
w.close();
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
return f;
}
}
7.测试调用
图片转成base64字符串时,注意删掉圈中的内容
@PostMapping("/exportword")
public void exportWordTest1(HttpServletRequest req, HttpServletResponse resp, @RequestBody List<String> imageList) throws Exception {
Map<String, String> stringStringHashMap = new HashMap<>();
for (int i = 0; i < imageList.size(); i++) {
stringStringHashMap.put("image" + (i + 1),imageList.get(i).split("base64,")[0]);//这里存的是图片转成的base64字符串
}
WordUtils.exportMillCertificateWord(resp,stringStringHashMap,"test","test33.ftl");
}
注意,模板中有几张图片的位置,就要传几张图片。如果数量对不上的话,会报错