renkang
2025-01-24 d3767d594de66cb5f9d1294931acefea1866f783
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
package com.doumee.core.excel;
 
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.utils.Constants;
import com.doumee.dao.business.model.YwTempConfig;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
 
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
 
/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Rk
 * @create 2025/1/3 9:11
 */
public class ExcelReplaceCommon {
 
//    public static void main(String[] args) {
//        modifyWordTemplate("https://bdreport.oss-cn-beijing.aliyuncs.com/xczl/20250110/temp/BE9C167EE35C40E78029C68BABBD0EAB.docx", "d://output.docx", "张三", "Java课程");
//    }
 
 
 
 
 
    public static String modifyWordTemplate_old(String templatePath, List<YwTempConfig> ywTempConfigList,String fileName) {
        try (
             XWPFDocument document = new XWPFDocument(getFileInputStream(templatePath))) {
            // 此处连接到后续替换步骤
            // 遍历段落
            for (XWPFParagraph paragraph : document.getParagraphs()) {
                for (XWPFRun run : paragraph.getRuns()) {
                    // 替换占位符
                    String text = run.getText(0);
                    System.out.println(text);
                    if (text != null) {
                        for (YwTempConfig ywTempConfig:ywTempConfigList) {
                            if(StringUtils.isNotBlank(ywTempConfig.getUrl())){
                                text = text.replace(ywTempConfig.getTitle(), ywTempConfig.getUrl());
                            }else{
                                text = text.replace(ywTempConfig.getTitle(), "");
                            }
 
                        }
                        run.setText(text, 0); // 重新设置文本
                    }
                }
            }
            String path = "d://"+fileName;
            try (FileOutputStream fos = new FileOutputStream(path)) {
                document.write(fos); // 将修改后的文档写入输出流
            }
            return path;
            /*//二进制OutputStream
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            document.write(baos);//文档写入流
            //OutputStream写入InputStream二进制流
            ByteArrayInputStream in = new ByteArrayInputStream(baos.toByteArray()); */
        } catch (IOException e) {
            e.printStackTrace();
        }
        throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"文件转换异常!");
    }
 
 
 
 
    public static String modifyWordTemplate(String templatePath, List<YwTempConfig> ywTempConfigList,String fileName,String zipFilePath) {
        InputStream ins = null;
        OutputStream out = null;
        try{
            //获取Word模板,模板存放路径在项目的resources目录下
            ins = getFileInputStream(templatePath) ;
//            ins = new FileInputStream("D:\\3.docx");
            //注册xdocreport实例并加载FreeMarker模板引擎
            IXDocReport report = XDocReportRegistry.getRegistry().loadReport(ins,
                    TemplateEngineKind.Freemarker);
            //创建xdocreport上下文对象
            IContext context = report.createContext();
            for (YwTempConfig ywTempConfig:ywTempConfigList) {
                if(StringUtils.isBlank(ywTempConfig.getUrl())){
                    ywTempConfig.setUrl("-");
                }
                context.put(ywTempConfig.getTitle().replace("${","").replace("}",""), ywTempConfig.getUrl());
            }
            String path = zipFilePath + fileName;
            out = new FileOutputStream(new File(path));
            report.process(context, out);
            return path;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != ins) {
                try {
                    ins.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
 
 
 
 
 
 
 
 
 
    /*读取网络文件*/
    public static InputStream getFileInputStream(String path) {
        URL url = null;
        try {
            url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            //设置超时间为3秒
            conn.setConnectTimeout(3*1000);
            //防止屏蔽程序抓取而返回403错误
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            //得到输入流
            return conn.getInputStream();
        } catch (Exception e) {
 
        }
        return null;
    }
 
 
 
}