在文檔屬性中,可以設定諸多關于文檔的資訊,如建立時間、作者、機關、類别、關鍵詞、備注等摘要資訊以及一些自定義的文檔屬性。下面将通過Java程式來示範如何設定,同時對文檔内的已有資訊,也可以實作讀取和删除等操作。
示例大綱:
1. 添加文檔屬性
1.1 添加摘要資訊
1.2 添加自定義文檔資訊
2. 讀取文檔屬性
3. 删除文檔資訊
3.1 删除所有摘要資訊、自定義文檔屬性
3.2 删除指定摘要資訊、自定義文檔屬性
使用工具:Spire.XLS for Java
擷取方法:通過官網下載下傳包。下載下傳後,解壓檔案,并将lib檔案夾下的jar檔案導入java程式;或者通過Maven倉庫下載下傳導入。Jar導入效果如下:
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnL5IzN4QDM3EzNx0iN1MDM1ATNzETMyIDMwIDMy0CM5AjNwczLcJDMwIDMy8CXwkDM2AzNvwlbv1WbvN2Lc12bj5ycn9Gbi52YugTMwIzZtl2Lc9CX6MHc0RHaiojIsJye.png)
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();
}
}
生成的文檔可檢視屬性删除效果。
(本文完)