try {
//获取Word模板,模板存放路径在项目的resources目录下
InputStream ins = new FileInputStream(“/aa.docx”);
//注册xdocreport实例并加载FreeMarker模板引擎
IXDocReport report = XDocReportRegistry.getRegistry().loadReport(ins,
TemplateEngineKind.Freemarker);
//创建xdocreport上下文对象
IContext context = report.createContext();
//创建要替换的文本变量
context.put(“num”, “2233”);
List goodsList = new ArrayList<>();
FileVo goods1 = new FileVo();
goods1.setLeq(“25”);
goods1.setHumidity(“25”);
goods1.setApress(“25”);
goods1.setState(“25”);
goods1.setWspeed(“25”);
goods1.setTemperature(“30”);
goods1.setReceivetime(“40”);
goodsList.add(goods1);
context.put(“goods”, goodsList);
//创建字段元数据
FieldsMetadata fm = report.createFieldsMetadata();
//Word模板中的表格数据对应的集合类型
fm.load(“goods”, FileVo.class, true);
String eTag = String.valueOf(System.currentTimeMillis());
String fileName = “DATA” + eTag + “.docx”;
//输出到本地目录 // FileOutputStream out = new FileOutputStream(“D://cc10.docx”);
//浏览器下载
response.setCharacterEncoding(“utf-8”);
response.setContentType(“application/msword”);
response.setHeader(
“Content-Disposition”,
“attachment;filename=”.concat(String.valueOf(URLEncoder.encode(fileName,“UTF-8”))));
report.process(context, response.getOutputStream());
} catch (Exception e) {
logger.error(“查询错误”, e);
return null;
}