pom文件依赖
<!-- XDocReport 核心库 -->
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.core</artifactId>
<version>2.0.2</version>
</dependency>
<!-- 如果你使用 Freemarker 模板引擎 -->
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.template.freemarker</artifactId>
<version>2.0.2</version>
</dependency>
<!-- 如果你使用 Velocity 模板引擎 -->
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.template.velocity</artifactId>
<version>2.0.2</version>
</dependency>
<!-- 如果你处理 DOCX 文档 -->
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.document.docx</artifactId>
<version>2.0.2</version>
</dependency>
模板文件

表格

主体方法
public void generateWord(String start, String end) throws Exception {
//上传地址路径前缀(可配置yml读取配置文件)
String prefixUploadPath = "C:/***/uploadPath";
//模板路径地址resource目录地址下
InputStream ins = WeekReportServiceImpl.class.getClassLoader().getResourceAsStream("template/template.docx");
IXDocReport report = XDocReportRegistry.getRegistry().loadReport(ins, TemplateEngineKind.Freemarker);
IContext context = report.createContext();
context.put("start", convertDateFormat(start));
context.put("end", convertDateFormat(end));
context.put("k1", "v1");
context.put("k2", 'v2');
//设置表格对象信息
context.put("one", new ArrayList<>());
context.put("two", new ArrayList<>());
FieldsMetadata fm = report.createFieldsMetadata();
fm.load("one", SafetyRecord.class, true);
fm.load("two", DefectRecord.class, true);
String fileNamePath = prefixUploadPath + "/report";
String finalPath = getFileDir(fileNamePath) + "\\report_" + System.currentTimeMillis() + ".docx";
FileOutputStream out = new FileOutputStream(new File(finalPath));
// 核心代码,处理占位模板解析设置数据值
report.process(context, out);
// 保存报告记录到数据库
SafetyReport safetyReport = new SafetyReport();
safetyReport.setReportDate(end);
safetyReport.setFilePath(finalPath);
safetyReport.setCreateTime(LocalDateTime.now());
safetyReport.setStartDate(start);
safetyReport.setEndDate(end);
weekReportMapper.insertWeekReport(safetyReport);
}
Util类
/**
* 上传路径地址
* @param path
* @return
*/
public static String getFileDir(String path) {
LocalDate now = LocalDate.now();
DateTimeFormatter yearFormatter = DateTimeFormatter.ofPattern("yyyy");
DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("MM");
DateTimeFormatter dayFormatter = DateTimeFormatter.ofPattern("dd");
String year = now.format(yearFormatter);
String month = now.format(monthFormatter);
String day = now.format(dayFormatter);
String fullPath = path + File.separator + year + File.separator + month + File.separator + day;
File fullDir = new File(fullPath);
if (!fullDir.exists()) {
fullDir.mkdirs();
}
return fullPath;
}
/**
* 日期转换为中文年月日格式
* @param inputDate
* @return
*/
public static String convertDateFormat(String inputDate) {
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy年MM月dd日");
try {
Date date = inputFormat.parse(inputDate);
return outputFormat.format(date);
} catch (ParseException e) {
System.err.println("日期解析出错: " + e.getMessage());
return null;
}
}
注(OS):
生成的docx模板中的字符占位,需要采用特殊格式生成编辑域,以下是如何模板占位的字符操作流程:
1、选中需要替换的文本内容:-->“插入”-->"文档部件"--> "域"

2、找到“邮件合并” -->在域代码输入占位字符、
z
3、如果是表格的格式为 ${one.no}
注意表格模板的占位字符必须与代码中的一致。其中对象的变量名“one”需要对应代码的入参格式
4、
134

被折叠的 条评论
为什么被折叠?



