1.引入依赖包
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.30</version>
</dependency>
2.模板文件
1.)先用word画出模板,将模板另存为.xml文件,再将模板文件名改为.ftl文件
2.)进入模板文件对填充参数进行修改,这里需要注意对参数进行空处理不然导出数据为空会直接抛异常
3.代码实现
import freemarker.template.Template;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.InputStreamSource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
@RestController
public class TestController {
@GetMapping(value = "/apply_order/v1/getTest")
public void exportTest(HttpServletRequest request, HttpServletResponse response) throws Exception {
// 准备数据
Map dataMap = new HashMap();
dataMap.put("applyNo", "测试");
dataMap.put("mchtName", "测试");
dataMap.put("applyTime", "测试");
dataMap.put("customerName", "测试");
dataMap.put("receiverAddress", "测试");
List<ExportProductmentProductDto> productList = new ArrayList<>();
ExportProductmentProductDto dto = new ExportProductmentProductDto();
dto.setUnit("台");
dto.setProductName("商品1");
dto.setDeliverNums("10");
dto.setPurchasePrice("200.00");
dto.setDeviceModel("M-550");
dto.setPurchaseTotalPrice("2000.00");
productList.add(dto);
productList.add(dto);
productList.add(dto);
dataMap.put("productList", productList);
// 纠正编码
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("utf-8");
// 初始化配置文件
// Configuration configuration = new Configuration();
// 设置编码
// configuration.setDefaultEncoding("utf-8");
// 加载D盘下的模板文件
// configuration.setDirectoryForTemplateLoading(new File("D:\\"));
// 加载模板,通过Word转XML文件转换过来的
// Template template = configuration.getTemplate("productment.ftl");
Template template = configurer.getConfiguration().getTemplate("productment.ftl");
// 生成Word文档,生成文件流
StringWriter out1 = new StringWriter();
Writer writer = new BufferedWriter(out1, 10240);
// 将数据输出到模板
template.process(dataMap, writer);
InputStreamSource file = new ByteArrayResource(out1.toString().getBytes(StandardCharsets.UTF_8));
InputStream fin = file.getInputStream();
ServletOutputStream out;
response.setContentType("application/msword");
// 设置浏览器以下载的方式处理该文件
response.setHeader("content-disposition", "attachment;filename=document.doc");
out = response.getOutputStream();
// 缓冲区
byte[] buffer = new byte[512];
int bytesToRead;
// 通过循环将读入的Word文件的内容输出到浏览器中
while ((bytesToRead = fin.read(buffer)) != -1) {
out.write(buffer, 0, bytesToRead);
}
fin.close();
if (out != null) {
out.close();
}
}
}