|
@@ -37,16 +37,18 @@ import com.deepoove.poi.config.ConfigureBuilder;
|
|
import com.deepoove.poi.policy.RenderPolicy;
|
|
import com.deepoove.poi.policy.RenderPolicy;
|
|
import com.deepoove.poi.template.ElementTemplate;
|
|
import com.deepoove.poi.template.ElementTemplate;
|
|
import com.deepoove.poi.util.TableTools;
|
|
import com.deepoove.poi.util.TableTools;
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 模板方法模式, 定义一个导出流程的骨架
|
|
* 模板方法模式, 定义一个导出流程的骨架
|
|
*
|
|
*
|
|
* @author lixuesong
|
|
* @author lixuesong
|
|
* @date 2024/05/30
|
|
* @date 2024/05/30
|
|
*/
|
|
*/
|
|
-public abstract class AbstractReportExporter implements ReportDataProcessor {
|
|
|
|
|
|
+public abstract class AbstractReportExporter implements ReportDataProcessor {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 将原始raw数据设置类型为poitl1.12版本超链接数据类型
|
|
* 将原始raw数据设置类型为poitl1.12版本超链接数据类型
|
|
|
|
+ *
|
|
* @param data raw数据集合
|
|
* @param data raw数据集合
|
|
* @param key 模版key
|
|
* @param key 模版key
|
|
*/
|
|
*/
|
|
@@ -59,6 +61,7 @@ public abstract class AbstractReportExporter implements ReportDataProcessor {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 将原始raw数据设置类型为poitl1.12版本图片列表数据类型
|
|
* 将原始raw数据设置类型为poitl1.12版本图片列表数据类型
|
|
|
|
+ *
|
|
* @param data raw数据集合
|
|
* @param data raw数据集合
|
|
* @param key 模版key
|
|
* @param key 模版key
|
|
*/
|
|
*/
|
|
@@ -82,8 +85,8 @@ public abstract class AbstractReportExporter implements ReportDataProcessor {
|
|
PictureType pictureType = determinePictureType(fileExtension);
|
|
PictureType pictureType = determinePictureType(fileExtension);
|
|
|
|
|
|
paragraph.addPicture(Pictures.ofBase64(base64, pictureType)
|
|
paragraph.addPicture(Pictures.ofBase64(base64, pictureType)
|
|
- .size(width, height)
|
|
|
|
- .create());
|
|
|
|
|
|
+ .size(width, height)
|
|
|
|
+ .create());
|
|
|
|
|
|
paragraphImages.add(paragraph);
|
|
paragraphImages.add(paragraph);
|
|
}
|
|
}
|
|
@@ -95,6 +98,7 @@ public abstract class AbstractReportExporter implements ReportDataProcessor {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 图片格式
|
|
* 图片格式
|
|
|
|
+ *
|
|
* @param fileExtension raw中的后缀
|
|
* @param fileExtension raw中的后缀
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
@@ -114,32 +118,33 @@ public abstract class AbstractReportExporter implements ReportDataProcessor {
|
|
default -> PictureType.PNG;
|
|
default -> PictureType.PNG;
|
|
};
|
|
};
|
|
}
|
|
}
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 报告导出
|
|
* 报告导出
|
|
|
|
+ *
|
|
* @param relationId
|
|
* @param relationId
|
|
- * @param data 数据
|
|
|
|
|
|
+ * @param data 数据
|
|
* @param templatePath 模板位置
|
|
* @param templatePath 模板位置
|
|
* @param reportBastPath pdf导出文件位置
|
|
* @param reportBastPath pdf导出文件位置
|
|
* @return
|
|
* @return
|
|
* @throws IOException
|
|
* @throws IOException
|
|
*/
|
|
*/
|
|
public final String exportReport(Map<String, Object> data, String templatePath,
|
|
public final String exportReport(Map<String, Object> data, String templatePath,
|
|
- String reportBastPath, Long relationId) throws Exception {
|
|
|
|
|
|
+ String reportBastPath, Long relationId) throws Exception {
|
|
|
|
|
|
String basicPath = this.getBasicPath();
|
|
String basicPath = this.getBasicPath();
|
|
String reportImagePath = this.getReportImagePath();
|
|
String reportImagePath = this.getReportImagePath();
|
|
String label = String.valueOf(relationId) + "_" + IdUtil.fastSimpleUUID();
|
|
String label = String.valueOf(relationId) + "_" + IdUtil.fastSimpleUUID();
|
|
- //word导出位置
|
|
|
|
|
|
+ // word导出位置
|
|
String reportTempWordFile = basicPath + "/" + label + ".docx";
|
|
String reportTempWordFile = basicPath + "/" + label + ".docx";
|
|
// 定义策略
|
|
// 定义策略
|
|
ConfigureBuilder builder = Configure.builder();
|
|
ConfigureBuilder builder = Configure.builder();
|
|
this.bindConfigure(builder);
|
|
this.bindConfigure(builder);
|
|
// 渲染数据
|
|
// 渲染数据
|
|
Map<String, Object> processedData = data.entrySet().stream()
|
|
Map<String, Object> processedData = data.entrySet().stream()
|
|
- .collect(Collectors.toMap(
|
|
|
|
- Map.Entry::getKey,
|
|
|
|
- entry -> entry.getValue() != null ? entry.getValue() : "-"
|
|
|
|
- ));
|
|
|
|
|
|
+ .collect(Collectors.toMap(
|
|
|
|
+ Map.Entry::getKey,
|
|
|
|
+ entry -> entry.getValue() != null ? entry.getValue() : "-"));
|
|
this.renderTemplate(reportTempWordFile, templatePath, builder, processedData);
|
|
this.renderTemplate(reportTempWordFile, templatePath, builder, processedData);
|
|
// word转pdf
|
|
// word转pdf
|
|
String html = OfficeUtil.convert(reportTempWordFile, reportImagePath);
|
|
String html = OfficeUtil.convert(reportTempWordFile, reportImagePath);
|
|
@@ -165,7 +170,7 @@ public abstract class AbstractReportExporter implements ReportDataProcessor {
|
|
System.err.println("写入 3.html 文件时发生错误: " + e.getMessage());
|
|
System.err.println("写入 3.html 文件时发生错误: " + e.getMessage());
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
- return OfficeUtil.convertHtmlToPdf(html2, reportBastPath, getFontPaths(), reportImagePath, false);
|
|
|
|
|
|
+ return OfficeUtil.convertHtmlToPdf(html2, reportBastPath, getFontPaths(), reportImagePath, false);
|
|
}
|
|
}
|
|
|
|
|
|
protected abstract String getReportImagePath();
|
|
protected abstract String getReportImagePath();
|
|
@@ -188,9 +193,7 @@ public abstract class AbstractReportExporter implements ReportDataProcessor {
|
|
*/
|
|
*/
|
|
protected abstract void bindConfigure(ConfigureBuilder builder);
|
|
protected abstract void bindConfigure(ConfigureBuilder builder);
|
|
|
|
|
|
-
|
|
|
|
- protected abstract String imagePath();
|
|
|
|
-
|
|
|
|
|
|
+ protected abstract String imagePath();
|
|
|
|
|
|
/**
|
|
/**
|
|
* 渲染数据
|
|
* 渲染数据
|
|
@@ -201,11 +204,10 @@ public abstract class AbstractReportExporter implements ReportDataProcessor {
|
|
* @throws IOException io异常
|
|
* @throws IOException io异常
|
|
*/
|
|
*/
|
|
protected abstract void renderTemplate(String reportTempWordFile, String templatePath, ConfigureBuilder builder,
|
|
protected abstract void renderTemplate(String reportTempWordFile, String templatePath, ConfigureBuilder builder,
|
|
- Map<String, Object> data) throws IOException;
|
|
|
|
|
|
+ Map<String, Object> data) throws IOException;
|
|
|
|
|
|
protected abstract String getBasicPath() throws IOException;
|
|
protected abstract String getBasicPath() throws IOException;
|
|
|
|
|
|
-
|
|
|
|
protected RenderPolicy customHackLoopTableRenderPolicy() {
|
|
protected RenderPolicy customHackLoopTableRenderPolicy() {
|
|
RenderPolicy policy = new LoopRowTableRenderPolicy() {
|
|
RenderPolicy policy = new LoopRowTableRenderPolicy() {
|
|
@Override
|
|
@Override
|
|
@@ -230,6 +232,7 @@ public abstract class AbstractReportExporter implements ReportDataProcessor {
|
|
};
|
|
};
|
|
return policy;
|
|
return policy;
|
|
}
|
|
}
|
|
|
|
+
|
|
protected RenderPolicy indicatorsRenderPolicy() {
|
|
protected RenderPolicy indicatorsRenderPolicy() {
|
|
return new LoopRowTableRenderPolicy() {
|
|
return new LoopRowTableRenderPolicy() {
|
|
@Override
|
|
@Override
|
|
@@ -248,8 +251,8 @@ public abstract class AbstractReportExporter implements ReportDataProcessor {
|
|
if (data instanceof List) {
|
|
if (data instanceof List) {
|
|
// 处理List类型数据
|
|
// 处理List类型数据
|
|
return ((List<?>) data).stream()
|
|
return ((List<?>) data).stream()
|
|
- .map(this::processItem)
|
|
|
|
- .collect(Collectors.toList());
|
|
|
|
|
|
+ .map(this::processItem)
|
|
|
|
+ .collect(Collectors.toList());
|
|
}
|
|
}
|
|
|
|
|
|
return data;
|
|
return data;
|
|
@@ -264,10 +267,9 @@ public abstract class AbstractReportExporter implements ReportDataProcessor {
|
|
if (item instanceof Map) {
|
|
if (item instanceof Map) {
|
|
Map<?, ?> map = (Map<?, ?>) item;
|
|
Map<?, ?> map = (Map<?, ?>) item;
|
|
return map.entrySet().stream()
|
|
return map.entrySet().stream()
|
|
- .collect(Collectors.toMap(
|
|
|
|
- Map.Entry::getKey,
|
|
|
|
- e -> e.getValue() == null ? "-" : e.getValue()
|
|
|
|
- ));
|
|
|
|
|
|
+ .collect(Collectors.toMap(
|
|
|
|
+ Map.Entry::getKey,
|
|
|
|
+ e -> e.getValue() == null ? "-" : e.getValue()));
|
|
}
|
|
}
|
|
|
|
|
|
return item;
|
|
return item;
|
|
@@ -275,7 +277,6 @@ public abstract class AbstractReportExporter implements ReportDataProcessor {
|
|
};
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
protected RenderPolicy getScoreRenderPolicy() {
|
|
protected RenderPolicy getScoreRenderPolicy() {
|
|
RenderPolicy policy = new LoopRowTableRenderPolicy() {
|
|
RenderPolicy policy = new LoopRowTableRenderPolicy() {
|
|
@SuppressWarnings("checkstyle:NestedForDepth")
|
|
@SuppressWarnings("checkstyle:NestedForDepth")
|
|
@@ -284,7 +285,7 @@ public abstract class AbstractReportExporter implements ReportDataProcessor {
|
|
// 检查数据是否为空
|
|
// 检查数据是否为空
|
|
if (data == null || (data instanceof Collection && ((Collection<?>) data).isEmpty())) {
|
|
if (data == null || (data instanceof Collection && ((Collection<?>) data).isEmpty())) {
|
|
// 数据为空时,删除整个表格
|
|
// 数据为空时,删除整个表格
|
|
- // removeTemplateTable(eleTemplate, template);
|
|
|
|
|
|
+ // removeTemplateTable(eleTemplate, template);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -364,6 +365,7 @@ public abstract class AbstractReportExporter implements ReportDataProcessor {
|
|
};
|
|
};
|
|
return policy;
|
|
return policy;
|
|
}
|
|
}
|
|
|
|
+
|
|
private List<String> getCellText(XWPFTableRow row) {
|
|
private List<String> getCellText(XWPFTableRow row) {
|
|
List<String> texts = new ArrayList<>();
|
|
List<String> texts = new ArrayList<>();
|
|
if (row == null) {
|
|
if (row == null) {
|