Java 添加、讀取、刪除Excel文檔屬性

在文檔屬性中,可以設置諸多關於文檔的信息,如創建時間、作者、單位、類別、關鍵詞、備註等摘要信息以及一些自定義的文檔屬性。下面將通過Java程序來演示如何設置,同時對文檔內的已有信息,也可以實現讀取和刪除等操作。

示例大綱:

1. 添加文檔屬性

  1.1 添加摘要信息

  1.2 添加自定義文檔信息

2. 讀取文檔屬性

3. 刪除文檔信息

  3.1 刪除所有摘要信息、自定義文檔屬性

  3.2 刪除指定摘要信息、自定義文檔屬性

 

使用工具:Spire.XLS for Java

獲取方法:通過官網下載包。下載後,解壓文件,並將lib文件夾下的jar文件導入java程序;或者通過Maven倉庫下載導入。Jar導入效果如下:

Java 代碼示例

【示例1】添加Excel文檔屬性

import com.spire.xls.*;
import java.util.Date;

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

        //給文檔設置標題、主題、作者等內置文檔屬性
        wb.getDocumentProperties().setTitle("設置文檔屬性");
        wb.getDocumentProperties().setSubject("A類");
        wb.getDocumentProperties().setAuthor("Bubble");
        wb.getDocumentProperties().setManager("July");
        wb.getDocumentProperties().setCompany("Alibaba");
        wb.getDocumentProperties().setCategory("內部");
        wb.getDocumentProperties().setKeywords("文檔、草稿");

        //給文檔添加自定義文檔屬性
        wb.getCustomDocumentProperties().add("_MarkAsFinal", true);
        wb.getCustomDocumentProperties().add("編輯", "Administrator");
        wb.getCustomDocumentProperties().add("聯繫電話", 12345678);
        wb.getCustomDocumentProperties().add("更新日期", new Date());

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

生成的文檔可查看屬性添加效果。

 

【示例2】讀取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());
    }
}

文檔屬性讀取結果:

【示例3】刪除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();
    }
}

生成的文檔可查看屬性刪除效果。

(本文完)

發佈了237 篇原創文章 · 獲贊 93 · 訪問量 51萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章