PoitlUtils.java 5.99 KB
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;
    }



}