PoitlUtils.java
5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package com.lhcredit.common.utils;
import com.alibaba.fastjson.JSONObject;
import com.aspose.words.Document;
import com.aspose.words.License;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.Includes;
import com.deepoove.poi.data.PictureType;
import com.deepoove.poi.data.Pictures;
import java.io.*;
import com.deepoove.poi.config.ConfigureBuilder;
import com.deepoove.poi.data.Texts;
import com.deepoove.poi.plugin.table.LoopColumnTableRenderPolicy;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.lhcredit.common.utils.poi.MultipleRowTableRenderPolicy;
import com.lhcredit.project.business.reportMake.ApiJsonPathService;
import com.lhcredit.project.business.reportMake.LoopRowTableRenderPolicyByReport;
import com.lhcredit.project.business.reportMake.reportTemp.LoopColumnTableRenderPolicy2;
import org.apache.poi.ooxml.POIXMLDocument;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSettings;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;
import org.springframework.stereotype.Component;
import java.util.*;
@Component
public class PoitlUtils {
public ConfigureBuilder builder= Configure.builder();
public Map<String,Object> data;
public List<String> bingObject=new ArrayList<String>(){
{add("sifa1_list");}
{add("sifa2_list");}
{add("sifa3_list");}
{add("sifa4_list");}
{add("sifa5_list");}
{add("sifa6_list");}
};
public List<String> bingList=new ArrayList<String>(){
{add("financeXcData_4_2.assetTable");}
{add("financeXcData_4_2.liabilitTable");}
{add("financeXcData_4_2.profitTable");}
{add("xc_lastFinance.assetTable");}
{add("xc_lastFinance.liabilitTable");}
{add("xc_lastFinance.profitTable");}
{add("modelData_5_4_1");}
{add("modelData_5_4_2");}
};
public ConfigureBuilder getConfigureBuilder(){
for (String s:bingObject)builder.bind(s,new MultipleRowTableRenderPolicy());
for (String s:bingList)builder.bind(s,new LoopRowTableRenderPolicyByReport(true));
return builder;
}
public void addObject(String name){ bingObject.add(name);}
public void addBingList(String name){ bingList.add(name);}
public static String outPutDocxTest(String templateName, String fileName, Object params, Configure test) {
XWPFTemplate template = XWPFTemplate.compile(new File(templateName), test).render(
params);
try {
// 对 fileName 中的特殊字符进行转义
String escapedFileName = fileName.replaceAll("[.\\\\/]", "_");
String path = "D://" + escapedFileName + System.currentTimeMillis() + ".docx";
template.write(new FileOutputStream(path));
return path;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static String outPutDocx2(String templateName, String outPath, String fileName, Object params, Configure test) {
templateName = WordToPdfUtil.filePathChangeOs(templateName);
outPath = WordToPdfUtil.filePathChangeOs(outPath);
XWPFTemplate template = XWPFTemplate.compile(new File(templateName), test).render(
params);
try {
// 对 fileName 中的特殊字符进行转义
String escapedFileName = fileName.replaceAll("[.\\\\/]", "_");
String path = outPath + escapedFileName + ".docx";
template.write(new FileOutputStream(path));
return path;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static String outPutDocx(String templateName, String fileName, Object params, Configure configureBuilder) {
XWPFTemplate template = XWPFTemplate.compile(WordToPdfUtil.tempDirectory + templateName, configureBuilder).render(params);
try {
// 对 fileName 中的特殊字符进行转义
String path = WordToPdfUtil.fileDirectory + fileName + System.currentTimeMillis() + ".docx";
template.write(new FileOutputStream(path));
return path;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 更新word目录
* @param filepath
* @throws Exception
*/
public static void updateCatalogue(String filepath) {
if(!getAsposeWordLicense()){
return ;
}
try{
Document doc = new Document(filepath);
doc.updateFields();
doc.save(filepath);
}catch (Exception e){
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
List<JSONObject> list = Arrays.asList(new JSONObject() {
{
put("size", 1);
}
{
put("title", "1111");
}
});
LoopColumnTableRenderPolicy2 policy = new LoopColumnTableRenderPolicy2();
Configure config = Configure.builder().bind("zy_public", policy).bind("zy_business", policy).build();
PoitlUtils.outPutDocx2("C:\\Users\\HASEE\\Desktop\\新建 DOCX 文档.docx","C:\\Users\\HASEE\\Desktop\\","111",new JSONObject(){{put("zy_public",list);}{put("zy_business",list);}},
config);
}
public static boolean getAsposeWordLicense(){
boolean result = false;
try {
InputStream is = new FileInputStream(WordToPdfUtil.tempDirectory + "license.xml");
// InputStream is = new FileInputStream("D:\\javaPorject\\credit-manager-new\\src\\main\\resources\\docTemp\\license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}