jiangping
2024-05-17 eb1456da5058bbb08569da4fcae768c3e043cc39
提交一把
已添加1个文件
已修改1个文件
101 ■■■■■ 文件已修改
server/service/pom.xml 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
server/service/src/main/java/com/doumee/core/annotation/excel/ExcelToPdfToolNewSev.java 91 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
server/service/pom.xml
@@ -21,10 +21,20 @@
            <artifactId>dianziqian</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
        <!--jacob依赖-->
        <dependency>
            <groupId>com.jacob</groupId>
            <artifactId>jacob</artifactId>
            <version>1.19</version>
            <scope>system</scope>
            <!--本地的jacob.jar的路径-->
            <systemPath>${project.basedir}/lib/jacob.jar</systemPath>
        </dependency>
    </dependencies>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <log4j2.version>2.17.2</log4j2.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
server/service/src/main/java/com/doumee/core/annotation/excel/ExcelToPdfToolNewSev.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,91 @@
package com.doumee.core.annotation.excel;import java.io.*;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
public class ExcelToPdfToolNewSev {
        /**
         * ä½¿ç”¨jacob实现excel转PDF
         *
         * @param inputFilePath å¯¼å…¥Excel文件路径
         * @param outputFilePath å¯¼å‡ºPDF文件路径
         */
        public static void jacobExcelToPDF(String inputFilePath, String outputFilePath) {
            ActiveXComponent ax = null;
            Dispatch excel = null;
            try {
                ComThread.InitSTA();
                ax = new ActiveXComponent("Excel.Application");
                ax.setProperty("Visible", new Variant(false));
                //        ç¦ç”¨å®
                ax.setProperty("AutomationSecurity", new Variant(3));
                Dispatch excels = ax.getProperty("Workbooks").toDispatch();
                Object[] obj = {
                        inputFilePath,
                        new Variant(false),
                        new Variant(false)
                };
                excel = Dispatch.invoke(excels, "Open", Dispatch.Method, obj, new int[9]).toDispatch();
                Dispatch currentSheet = Dispatch.get(excel,
                        "ActiveSheet").toDispatch();
                // èŽ·å–é¡µé¢è®¾ç½®
                Dispatch pageSetup = Dispatch.get(currentSheet, "PageSetup") .toDispatch();
                Dispatch.put(pageSetup, "Orientation", new Variant(2));
                Dispatch.put(pageSetup, "FitToPagesTall", new Variant(0));
                Dispatch.put(pageSetup, "FitToPagesWide", new Variant(1));
                // èŽ·å–é¡µé¢è®¾ç½®
//                Dispatch.put(pageSetup, "PaperSize", pageSize);
                // å†…容缩放
//                Dispatch.put(pageSetup, "Zoom", new Variant(false));
                //                Dispatch Application = Dispatch.get(currentSheet, "Application") .toDispatch();
//                Dispatch.put(pageSetup, "PaperSize", pageSize);
//                Dispatch.put(Application, "ActivePrinter", "Microsoft Print to PDF");
                // å†…容缩放
//                Dispatch.put(pageSetup, "Orientation", new Variant(2));
//                Dispatch.put(pageSetup, "Zoom", new Variant(false));
//                Dispatch.put(pageSetup, "FitToPagesWide", new Variant(1));
//                Dispatch.put(pageSetup, "FitToPagesTall", new Variant(1));
//                Dispatch.put(pageSetup, "PaperSize",  new Variant(8)); // A3是8,A4是9,A5是11等等
                //        è½¬æ¢æ ¼å¼
                Object[] obj2 = {
                        //                PDF格式等于0
                        new Variant(0),
                        outputFilePath,
                        //                0=标准(生成的PDF图片不会模糊),1=最小的文件
                        new Variant(0)
                };
                Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, obj2, new int[1]);
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            } finally {
                if (excel != null) {
                    Dispatch.call(excel, "Close", new Variant(false));
                }
                if (ax != null) {
                    ax.invoke("Quit", new Variant[]{});
                    ax = null;
                }
                ComThread.Release();
            }
        }
    public static void main(String[] args) {
        String inputExcelPath = "D:\\2.xlsx";
        String outputPdfPath = "D:\\22.pdf";
        jacobExcelToPDF(inputExcelPath, outputPdfPath);
    }
}