趣文网 > 作文大全

Java 添加 读取 删除Excel文档属性

2020-12-02 04:15:01
相关推荐

在文档属性中,可以设置诸多关于文档的信息,如创建时间、作者、单位、类别、关键词、备注等摘要信息以及一些自定义的文档属性。下面将通过Java程序来演示如何设置,同时对文档内的已有信息,也可以实现读取和删除等操作。

示例大纲:

1. 添加文档属性

1.1 添加摘要信息

1.2 添加自定义文档信息

2. 读取文档属性

3. 删除文档信息

3.1 删除所有摘要信息、自定义文档属性

3.2 删除指定摘要信息、自定义文档属性

使用工具:Spire.XLS for Java

获取方法1:通过e-iceblue官网下载包。下载后,解压文件,并将lib文件夹下的jar文件导入java程序;或者通过Maven仓库下载导入。Jar导入效果如下:

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();

}

}

生成的文档可查看属性删除效果。

阅读剩余内容
网友评论
相关内容
延伸阅读
小编推荐

大家都在看

我的成长作文 反思作文 故事作文 难忘的小学生活作文600字六年级 我想对您说作文400字 我最喜欢的一本书作文 20年后的家乡作文 我的家乡二年级作文 荡秋千作文 桥的作文 四年级上册作文 借景抒情的作文 坚持不懈的作文 美丽的校园作文 关于老师的作文 家乡变化的作文 日出作文 我的朋友 作文 初中作文素材 四年级下册第三单元作文 我最敬佩的一个人作文 我的同桌作文500字 写人的作文800字 我的朋友作文500字 自命题作文 我的妈妈作文400 有关青春的作文 关于生命的作文 写狗的作文 倡议书作文