docx生成模块
This commit is contained in:
parent
387831ef73
commit
14bd36e714
8
pom.xml
8
pom.xml
@ -32,6 +32,7 @@
|
||||
<mapstruct.version>1.6.2</mapstruct.version>
|
||||
<fastjson.version>2.0.53</fastjson.version>
|
||||
<docker-maven-plugin.version>0.44.0</docker-maven-plugin.version>
|
||||
<poi-tl.version>1.12.2</poi-tl.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<!-- <dependency>-->
|
||||
@ -131,6 +132,13 @@
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.deepoove</groupId>
|
||||
<artifactId>poi-tl</artifactId>
|
||||
<version>${poi-tl.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.springframework.boot</groupId>-->
|
||||
<!-- <artifactId>spring-boot-starter-data-r2dbc</artifactId>-->
|
||||
|
@ -0,0 +1,28 @@
|
||||
package com.zsc.edu.dify.framework.generateDocx;
|
||||
|
||||
import com.deepoove.poi.XWPFTemplate;
|
||||
import com.zsc.edu.dify.framework.generateDocx.styleConfig.styleDataWrapper;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class generateDocx {
|
||||
void generateSpider4(List<Map<String, Object>> rawData) {
|
||||
String resource = "src/main/resources/docxTemplate/spiderFourModel.docx";
|
||||
String target = "src/main/resources/docxTemplate/resultDocx.docx";
|
||||
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
|
||||
List<Map<String, Object>> styledData = styleDataWrapper.wrap(rawData);
|
||||
|
||||
data.put("context", styledData);
|
||||
|
||||
try (XWPFTemplate template = XWPFTemplate.compile(resource).render(data)) {
|
||||
template.writeToFile(target);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.zsc.edu.dify.framework.generateDocx.styleConfig;
|
||||
|
||||
import com.deepoove.poi.data.style.Style;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class labelStyles {
|
||||
private final Style titleStyle;
|
||||
private final Style textStyle;
|
||||
|
||||
// 私有构造函数,强制通过工厂方法创建
|
||||
private labelStyles(Style titleStyle, Style textStyle) {
|
||||
this.titleStyle = titleStyle;
|
||||
this.textStyle = textStyle;
|
||||
}
|
||||
|
||||
// 工厂方法:初始化默认样式
|
||||
public static labelStyles createDefault() {
|
||||
Style titleStyle = new Style();
|
||||
titleStyle.setBold(true);
|
||||
titleStyle.setFontSize(11);
|
||||
|
||||
Style textStyle = new Style();
|
||||
textStyle.setFontSize(11);
|
||||
|
||||
return new labelStyles(titleStyle, textStyle);
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.zsc.edu.dify.framework.generateDocx.styleConfig;
|
||||
|
||||
import com.deepoove.poi.data.style.ParagraphStyle;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class paragraphStylesTesting {
|
||||
private final ParagraphStyle commonParagraphStyles;
|
||||
|
||||
private paragraphStylesTesting(ParagraphStyle commonParagraphStyles) {
|
||||
this.commonParagraphStyles = commonParagraphStyles;
|
||||
}
|
||||
|
||||
public static paragraphStylesTesting createDefault() {
|
||||
ParagraphStyle paragraphStyle = new ParagraphStyle();
|
||||
paragraphStyle.setSpacing(1.0);
|
||||
return new paragraphStylesTesting(paragraphStyle);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package com.zsc.edu.dify.framework.generateDocx.styleConfig;
|
||||
|
||||
import com.deepoove.poi.data.ParagraphRenderData;
|
||||
import com.deepoove.poi.data.Paragraphs;
|
||||
import com.deepoove.poi.data.TextRenderData;
|
||||
import com.deepoove.poi.data.style.Style;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class styleDataWrapper {
|
||||
static labelStyles styles = labelStyles.createDefault();
|
||||
|
||||
// 定义标签与样式映射规则。标题用一个样式,其他标签用一个样式
|
||||
private static final Map<String, Style> STYLE_MAPPING = Map.of(
|
||||
"title", styles.getTitleStyle(),
|
||||
"*", styles.getTextStyle()
|
||||
);
|
||||
|
||||
public static List<Map<String, Object>> wrap(List<Map<String, Object>> rawData) {
|
||||
return rawData.stream()
|
||||
.map(styleDataWrapper::processMap)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
// 为每个字段绑定特定的样式
|
||||
private static Map<String, Object> processMap(Map<String, Object> rawMap) {
|
||||
Map<String, Object> styledMap = new HashMap<>();
|
||||
rawMap.forEach((key, value) -> {
|
||||
Style style = STYLE_MAPPING.getOrDefault(key, STYLE_MAPPING.get("*"));
|
||||
TextRenderData textRenderData = new TextRenderData(value.toString(), style);
|
||||
|
||||
// ParagraphRenderData paragraph = Paragraphs.of(textRenderData).create();
|
||||
// paragraph.setParagraphStyle(paragraphStylesTesting.createDefault().getCommonParagraphStyles());
|
||||
|
||||
styledMap.put(key, textRenderData);
|
||||
});
|
||||
return styledMap;
|
||||
}
|
||||
}
|
0
src/main/resources/docxTemplate/resultDocx.docx
Normal file
0
src/main/resources/docxTemplate/resultDocx.docx
Normal file
BIN
src/main/resources/docxTemplate/spiderFourModel.docx
Normal file
BIN
src/main/resources/docxTemplate/spiderFourModel.docx
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user