博客 / 詳情

返回

Java 讀取或刪除 Excel 文件文檔屬性: Java 實用指南

Excel文件不僅僅是數據表格,它們還承載着重要的元數據,即“文檔屬性”。這些屬性(如作者、標題、公司、創建日期等)在文件管理、信息溯源和數據合規性方面扮演着關鍵角色。然而,在Java應用中如何高效地讀取或刪除這些屬性,常常是開發者面臨的痛點。本文將深入探討如何利用功能強大的Spire.XLS for Java庫,輕鬆實現Excel文檔屬性的讀寫與刪除操作,為你的數據處理工作提供實用解決方案。

Spire.XLS for Java 庫簡介與安裝

Spire.XLS for Java 是一個專業的Java Excel API,專注於提供高性能、高質量的Excel文件處理能力。它支持各種Excel操作,包括創建、讀取、寫入、轉換和打印Excel文檔,且無需依賴Microsoft Office。其直觀的API設計使得開發者可以便捷地操作Excel的各種元素,包括單元格、行、列、圖表、圖片以及我們今天要討論的文檔屬性。

要將Spire.XLS for Java引入你的項目,只需在Maven配置文件中添加相應的依賴即可。

Maven 依賴:

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.xls</artifactId>
        <version>15.11.3</version>
    </dependency>
</dependencies>

Java 獲取 Excel 文件的文檔屬性

Excel文檔屬性分為兩類:內置屬性 (Built-in Properties) 和 自定義屬性 (Custom Properties)。內置屬性是Excel預定義的,如標題、主題、作者、公司、創建日期等。自定義屬性則允許用户根據需要添加額外的信息。

以下代碼示例展示瞭如何使用Spire.XLS for Java來讀取Excel文件的這些文檔屬性:

import com.spire.xls.*;

public class ReadProperties {
    public static void main(String[] args) {
        //加載Excel文檔
        Workbook wb = new Workbook();
        wb.loadFromFile("AddProperties.xlsx");

        //讀取Excel內置文檔屬性
        System.out.println("標題: " + wb.getDocumentProperties().getTitle());
        System.out.println("主題: " + wb.getDocumentProperties().getSubject());
        System.out.println("作者: " + wb.getDocumentProperties().getAuthor());
        System.out.println("單位: " + wb.getDocumentProperties().getCompany());
        System.out.println("主管: " + wb.getDocumentProperties().getManager());
        System.out.println("類別: " + wb.getDocumentProperties().getCategory());
        System.out.println("關鍵字: " + wb.getDocumentProperties().getKeywords());

        //獲取Excel自定義文檔屬性
        DocumentProperty property = (DocumentProperty) wb.getCustomDocumentProperties().get(0);
        //讀取第一個自定義文檔屬性的名稱和值
        System.out.println("名稱: " + property.getName());
        System.out.println("值: " + property.getValue());
    }
}

代碼説明:

  • Workbook.loadFromFile():加載指定的Excel文件。
  • Workbook.getDocumentProperties().get():獲取文檔屬性對象。
  • Workbook.getCustomDocumentProperties().get():獲取自定義文檔屬性集合。

Java 刪除 Excel 的文檔屬性

刪除文檔屬性的場景通常是為了保護隱私信息、清理不必要的元數據或確保文件合規性。Spire.XLS for Java 提供了靈活的方式來刪除特定的自定義屬性或清空所有自定義屬性。

以下代碼示例展示瞭如何刪除Excel文件的文檔屬性:

import com.spire.xls.*;

public class RemoveProperties {
    public static void main(String[] args) {
        //加載Excel文檔
        Workbook wb = new Workbook();
        wb.loadFromFile("AddProperties.xlsx");

        //通過將對應文檔屬性的值設置為空來刪除該內置屬性
        wb.getDocumentProperties().setTitle("");
        wb.getDocumentProperties().setSubject("");
        wb.getDocumentProperties().setAuthor("");
        wb.getDocumentProperties().setCompany("");
        wb.getDocumentProperties().setManager("");
        wb.getDocumentProperties().setCategory("");
        wb.getDocumentProperties().setKeywords("");
        wb.getDocumentProperties().setComments("");

        //根據自定義文檔屬性的名稱來移除該自定義文檔屬性
        wb.getCustomDocumentProperties().remove("編輯");
        wb.getCustomDocumentProperties().remove("聯繫電話");

        //保存文檔
        wb.saveToFile("RemoveProperties.xlsx", ExcelVersion.Version2010);
        wb.dispose();
    }
}

代碼説明:

  • Workbook.getCustomDocumentProperties().remove():通過屬性名稱刪除指定的自定義屬性。
  • 對於內置屬性,通常通過setXXX("")或setXXX(null)來將其值置空,達到“刪除”的效果。
  • Workbook.saveToFile("...", ExcelVersion.Version2013):將修改後的工作簿保存到新的Excel文件,並指定Excel版本。

結論

通過本文的介紹,你已經掌握瞭如何使用Spire.XLS for Java庫來讀取和刪除Excel文件的文檔屬性。無論是獲取重要的元數據,還是進行敏感信息的清理,Spire.XLS for Java都提供了簡潔高效的API支持。它極大地簡化了Java開發者在處理Excel文檔屬性時的複雜性,是Java生態中處理Excel文件的強大工具。鼓勵你進一步探索Spire.XLS for Java的更多高級功能,以滿足更復雜的Excel操作需求。

user avatar
0 位用戶收藏了這個故事!

發佈 評論

Some HTML is okay.