在 Java 中為 Excel 添加數位簽章

介紹

是否曾經需要手動簽署數十個 Excel 試算表,結果發現必須重新發送,因為有人修改了資料?更糟的是,收到一份關鍵的財務報告,卻懷疑自會計部門發出後是否被竄改?

Add digital signature excel 透過提供加密證明,證明您的 Excel 檔案未被更改,解決了這些困擾。可將其視為防篡改封印:若有人更改哪怕一個儲存格,簽章即失效。

在本教學中,您將學會如何使用 GroupDocs.Signature for Java 以程式方式為 Excel 試算表加入數位簽章。無論是建置自動化開票系統,或在分發財務報告前確保其安全,我們都會一步步說明所需內容,並提醒開發者常見的陷阱。

快速回答

  • 需要的函式庫是什麼? GroupDocs.Signature for Java (v23.12+)。
  • 需要網路連線嗎? 不需要,簽署完全離線進行。
  • 可以在沒有可見標記的情況下簽署嗎? 可以,將 options.setVisible(false) 設為隱形簽章。
  • GroupDocs 支援多少種格式? 超過 50 種輸入與輸出格式,包括 XLSX、DOCX、PDF 與影像。
  • 驗證簽章的最快方法是什麼? 在已簽署的檔案上使用 Signature.verify;它會在毫秒內回傳布林值。

什麼是 add digital signature excel?

add digital signature excel 指的是在 Excel 活頁簿內嵌入加密簽章,使任何之後的修改都會使簽章失效。這為基於試算表的商業資料提供了驗證、完整性與不可否認性。

為什麼要使用 GroupDocs.Signature for Java?

GroupDocs.Signature 支援 50+ 種檔案格式,且能在不將整個檔案載入記憶體的情況下處理上百頁的 Excel 活頁簿,較傳統實作可減少高達 70 % 的記憶體佔用。其 API 在 PDF、Word、影像與 CAD 檔案間保持一致,讓您能在不同專案間重複使用相同的簽署邏輯。

前置條件

  • GroupDocs.Signature for Java – 版本 23.12 或更新。
  • JDK – 版本 8 或以上(建議 11+)。
  • IDE – IntelliJ IDEA、Eclipse 或 NetBeans。
  • 建置工具 – Maven 或 Gradle。
  • 數位憑證 – 包含私鑰的 .pfx.p12 檔案。

您應該熟悉基本的 Java 語法、相依管理與檔案 I/O。若對憑證不熟悉,下一節將提供快速回顧。

了解數位憑證(快速版)

數位憑證是一種 公鑰容器,用以證明簽署者的身分。

  • PFX/P12 檔案 將私鑰與公鑰憑證打包於單一加密檔案中。
  • 密碼保護 用於保護私鑰;切勿硬編碼此密碼。
  • 憑證機構(或測試用自簽憑證)頒發憑證。

使用 Java 的 keytool 建立自簽憑證:

<dependency>
    <groupId>com.groupdocs</groupId>
    <artifactId>groupdocs-signature</artifactId>
    <version>23.12</version>
</dependency>

設定 GroupDocs.Signature for Java

首先,將函式庫加入您的專案。

Maven 設定

將以下相依加入您的 pom.xml

implementation 'com.groupdocs:groupdocs-signature:23.12'

Gradle 設定

或將以下內容加入您的 build.gradle

import com.groupdocs.signature.Signature;

public class DigitalSignatureSetup {
    public static void main(String[] args) {
        // Load your Excel file
        Signature signature = new Signature("path/to/your/document.xlsx");
        
        // We'll add signing logic here in the next section
    }
}

Pro tip: 使用環境變數儲存授權金鑰與憑證密碼,避免硬編碼。

如何使用 Java 為 Excel 添加數位簽章?

DigitalSignature 類別代表可套用於支援文件格式的加密簽章,封裝簽署演算法與憑證資訊。

在本指南中,您將學會如何使用 GroupDocs.Signature for Java 以程式方式將加密簽章嵌入 Excel 活頁簿。流程包括載入活頁簿、使用憑證建立 DigitalSignature 物件、設定簽署選項,最後呼叫簽署方法產生已簽檔案。整個工作流程可在不到十行程式碼內完成。

載入您的 Excel 活頁簿、設定 DigitalSignature 物件,然後呼叫 sign。以下步驟在十行程式碼以內說明完整流程。

步驟 1:載入數位憑證

KeyStore 是 Java 安全類別,用於從 PKCS12(.pfx/.p12)檔案載入私鑰與憑證。

import java.io.FileInputStream;
import java.security.KeyStore;

// Load the KeyStore containing your certificate
KeyStore keyStore = KeyStore.getInstance("JKS");
FileInputStream certStream = new FileInputStream("path/to/your/certificate.pfx");

// Load the certificate with your password
keyStore.load(certStream, "yourPassword".toCharArray());
certStream.close();

步驟 2:建立 DigitalSignature 物件

DigitalSignature 封裝簽署文件所需的加密操作。

import com.groupdocs.signature.domain.signatures.DigitalSignature;

// Create the digital signature object from your KeyStore
DigitalSignature digitalSignature = new DigitalSignature(keyStore);

步驟 3:設定 DigitalSignOptions

DigitalSignOptions 設定數位簽章的套用方式,包括可見性、簽署原因與工作簿內的目標位置。

import com.groupdocs.signature.options.sign.DigitalSignOptions;
import com.groupdocs.signature.domain.enums.HorizontalAlignment;
import com.groupdocs.signature.domain.enums.VerticalAlignment;

// Configure the signing options
DigitalSignOptions options = new DigitalSignOptions("path/to/your/certificate.pfx");
options.setPassword("yourPassword"); // Unlock your certificate
options.setCertificate(digitalSignature); // Attach the signature object

// Position the signature (bottom-right corner is standard)
options.setVerticalAlignment(VerticalAlignment.Bottom);
options.setHorizontalAlignment(HorizontalAlignment.Right);

// Optional: Add a visible signature line
options.setVisible(true); // Set to false for invisible signatures
options.setComments("Approved by Finance Department"); // Add context

步驟 4:簽署工作簿

呼叫 sign 會將簽章寫入工作簿的中繼資料,並儲存為新檔案。

// Sign the document and save to output path
signature.sign("path/to/your/output/digitalSignedSpreadsheet.xlsx", options);

System.out.println("Excel spreadsheet signed successfully!");

完整範例:整合所有步驟

以下是完整、可直接執行的程式碼,將所有部件串接在一起。

import com.groupdocs.signature.Signature;
import com.groupdocs.signature.domain.signatures.DigitalSignature;
import com.groupdocs.signature.options.sign.DigitalSignOptions;
import com.groupdocs.signature.domain.enums.HorizontalAlignment;
import com.groupdocs.signature.domain.enums.VerticalAlignment;

import java.io.FileInputStream;
import java.security.KeyStore;

public class ExcelDigitalSignature {
    public static void main(String[] args) {
        try {
            // 1. Load the Excel file you want to sign
            Signature signature = new Signature("input/financial-report.xlsx");
            
            // 2. Load your digital certificate
            KeyStore keyStore = KeyStore.getInstance("PKCS12");
            FileInputStream certStream = new FileInputStream("certificates/mycert.pfx");
            keyStore.load(certStream, "securePassword123".toCharArray());
            certStream.close();
            
            // 3. Create digital signature object
            DigitalSignature digitalSignature = new DigitalSignature(keyStore);
            
            // 4. Configure signing options
            DigitalSignOptions options = new DigitalSignOptions("certificates/mycert.pfx");
            options.setPassword("securePassword123");
            options.setCertificate(digitalSignature);
            options.setVerticalAlignment(VerticalAlignment.Bottom);
            options.setHorizontalAlignment(HorizontalAlignment.Right);
            options.setComments("Verified by Finance - Q4 2025");
            
            // 5. Sign and save
            signature.sign("output/financial-report-signed.xlsx", options);
            
            System.out.println("✓ Excel file signed successfully!");
            
        } catch (Exception e) {
            System.err.println("Error signing document: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

驗證數位簽章

Signature 類別是 GroupDocs.Signature API 的主要入口,提供簽署與驗證文件的方法。

驗證可確認工作簿自簽署以來未被修改。

import com.groupdocs.signature.Signature;
import com.groupdocs.signature.domain.signatures.DigitalSignature;

public class VerifyExcelSignature {
    public static void main(String[] args) {
        try {
            // Load the signed Excel file
            Signature signature = new Signature("output/financial-report-signed.xlsx");
            
            // Search for digital signatures
            List<DigitalSignature> signatures = signature.search(DigitalSignature.class);
            
            // Check each signature
            for (DigitalSignature sig : signatures) {
                System.out.println("Signature found:");
                System.out.println("  Signed by: " + sig.getSubject());
                System.out.println("  Valid: " + sig.isValid());
                System.out.println("  Sign time: " + sig.getSignTime());
                System.out.println("  Comments: " + sig.getComments());
            }
            
        } catch (Exception e) {
            System.err.println("Error verifying signature: " + e.getMessage());
        }
    }
}

若任意儲存格變更,驗證方法會回傳 false,且 SignatureInfo 物件會列出原因。

常見問題與解決方法

問題 1:「找不到憑證路徑」

Solution: 使用絕對路徑進行測試,或從 classpath 資源資料夾載入憑證。

問題 2:「憑證密碼錯誤」

Solution: 再次確認密碼,留意隱藏的空白字元,並始終從安全來源讀取。

問題 3:「文件已簽署」

Solution: GroupDocs 支援多重簽章。先呼叫 Signature.isSigned;若為 true,則先驗證或建立新副本再加入另一個簽章。

問題 4:輸出檔案損毀

Solution: 確認使用 GroupDocs 23.12+、具備寫入輸出資料夾的權限,且避免簽署舊版 .xls 檔案——先轉換為 .xlsx

問題 5:簽章在 Excel 中不可見

Solution: 隱形簽章屬正常情況。於 Excel 中前往 檔案 → 資訊 → 檢視簽章 即可看到,或將 options.setVisible(true) 設為可見簽章行。

何時在 Excel 中使用數位簽章

理想情境

  • 需要外部稽核師驗證的財務報表。
  • 價格合約,任何變更都可能影響收入。
  • 需具備不可變更稽核追蹤的合規報告。
  • 自動化工作流程,需要在後續處理前以程式方式驗證。

過度使用情境

  • 每日變更的草稿試算表。
  • 個人預算檔案。
  • 僅在組織內使用的臨時計算表。

實務應用

1. 財務報告分發系統

// Sign quarterly reports before sending to stakeholders
public void distributeQuarterlyReport(String reportPath) {
    String signedPath = signDocument(reportPath, "CFO Certificate");
    emailService.sendToBoard(signedPath);
    auditLog.record("Q4 report signed and distributed");
}

2. 發票產生流程

// Sign invoices before sending to clients
public void generateInvoice(Order order) {
    String invoicePath = createInvoiceExcel(order);
    String signedInvoice = signDocument(invoicePath, "Accounting Certificate");
    customerService.sendInvoice(signedInvoice, order.getCustomerEmail());
}

3. 多部門審批工作流程

// Multiple signatures from different departments
public void approvalChain(String documentPath) {
    String stage1 = signDocument(documentPath, "Engineering Certificate");
    String stage2 = signDocument(stage1, "Finance Certificate");
    String stage3 = signDocument(stage2, "Legal Certificate");
    archiveService.store(stage3);
}

4. 資料匯出與完整性驗證

// Sign data exports from your database
public void exportSecureData(String query) {
    List<Record> data = database.query(query);
    String excelPath = excelGenerator.create(data);
    String signedExport = signDocument(excelPath, "System Certificate");
    return signedExport; // Recipients can verify data hasn't been altered
}

5. 合約管理系統整合

將簽章驗證整合至您的 DMS,能自動標記簽署後被更改的合約。

效能考量

資源使用指引

  • 記憶體: 每次簽署會載入整個工作簿;若檔案 > 50 MB,請監控堆積使用情況,並考慮提升 JVM -Xmx 設定。
  • CPU: RSA‑2048 簽章在標準 2.6 GHz CPU 上約需 150 ms;批次簽署 100 個檔案在一般伺服器上可於 20 秒內完成。
  • I/O: 使用 SSD 儲存來源與目標資料夾,以避免瓶頸。

Java 記憶體管理最佳實踐

重複使用已載入的 KeyStore 於多次簽署作業,並及時關閉串流。

// Always use try-with-resources for automatic cleanup
try (Signature signature = new Signature("input.xlsx")) {
    // Signing operations here
} // Signature object automatically disposed

// For batch operations, process files sequentially to avoid memory spikes
for (String file : filesToSign) {
    try (Signature sig = new Signature(file)) {
        sig.sign(file + "-signed.xlsx", options);
    }
    // Explicitly suggest garbage collection between large files
    System.gc();
}

優化技巧

  1. 批次簽署:重複使用相同的 Signature 實例。
  2. 非同步處理:於 Web 應用中保持 UI 響應。
  3. 快取憑證:將憑證保存在記憶體中,而非每次都從磁碟讀取。
  4. 壓縮:在可能的情況下於簽署前壓縮大型工作簿。

常見問答

Q:什麼是數位憑證,該從哪裡取得?
A:數位憑證是一種包含公鑰與身分資訊的電子身分證。正式環境請向受信任的憑證機構購買;測試時可使用 Java 的 keytool 產生自簽憑證。

Q:GroupDocs.Signature 能處理其他文件類型嗎?
A:可以,支援 50+ 種格式——包括 PDF、DOCX、PPTX 與影像檔——使用相同的 API 模式。

Q:GroupDocs 所建立的簽章有多安全?
A:它使用業界標準的 RSA 2048(或更高)加密。安全等級取決於憑證的金鑰長度;2048 位元對大多數商業需求已足夠。

Q:我可以在同一個 Excel 檔案中加入多個簽章嗎?
A:絕對可以。每次呼叫 sign 都會新增一個獨立的簽章,支援多方批准工作流程。

Q:收件者需要使用 GroupDocs 來驗證簽章嗎?
A:不需要。已簽署的活頁簿可在 Microsoft Excel、LibreOffice 或 Google Sheets 開啟,內建的簽章檢視器即可驗證。

結論

您現在已掌握使用 Java 與 GroupDocs.Signature 為 add digital signature excel 的完整生產就緒方法。從載入憑證、處理常見錯誤到效能最佳化,您可以保護任何業務所依賴的試算表。

Next steps:

  • 試驗可見與隱形簽章的差異。
  • 將相同模式擴展至 PDF、Word 與影像檔案。
  • 建立接受上傳工作簿、簽署並回傳已簽署版本的 REST 端點。
  • 實作稽核追蹤日誌以符合合規報告需求。

資源


最後更新: 2026-06-01
測試環境: GroupDocs.Signature 23.12 for Java
作者: GroupDocs

相關教學