本文介绍如何使用Spire.Cloud.Excel提供的PropertiesApi接口添加和删除Excel文档属性。
步骤一:创建一个Maven程序,通过Maven仓库安装Spire.Cloud.SDK,详细步骤可参考这篇文章。
步骤二:通过冰蓝云官网(https://cloud.e-iceblue.cn/)注册账号并登陆,在“我的应用”版块创建应用程序,获得App ID及App Key。
步骤三:上传Excel文档至冰蓝云官网的“文档管理”版块。为了便于文档管理,您也可以先创建“input”文件夹,然后将需要编辑的Excel文档上传至该文件夹下。本教程将示例文档(sample.xlsx)上传到了input文件夹下。
步骤四:在Maven程序中编写测试类操作input文件夹下的文档。
示例1、添加文档属性
import spire.cloud.excel.sdk.ApiException;
import spire.cloud.excel.sdk.Configuration;
import spire.cloud.excel.sdk.api.PropertiesApi;
import spire.cloud.excel.sdk.model.DocumentProperties;
import spire.cloud.excel.sdk.model.DocumentProperty;
import java.util.ArrayList;
public class SetDocumenmtProperties {
static String appId = "App ID
static String appKey = "App Key";
static String baseUrl = "https://api.e-iceblue.cn";
//配置App ID和App Key
static Configuration configuration = new Configuration(appId, appKey, baseUrl);
//初始化PropertiesApi对象
static PropertiesApi propertiesApi = new PropertiesApi(configuration);
public static void main(String[] args) throws ApiException {
//指定源文档名称
String name = "sample.xlsx";
//创建DocumentProperties对象
DocumentProperties properties = new DocumentProperties();
//设置文档属性
DocumentProperty property1 = new DocumentProperty("Title", "示例文档", true);
DocumentProperty property2 = new DocumentProperty("Subject", "Word文档处理", true);
DocumentProperty property3 = new DocumentProperty("Keywords", "Word;属性", true);
DocumentProperty property4 = new DocumentProperty("Category", "教程", true);
DocumentProperty property5 = new DocumentProperty("Comments", "无评论", true);
DocumentProperty property6 = new DocumentProperty("Author", "Spire.Cloud.SDK", true);
DocumentProperty property7 = new DocumentProperty("Company", "冰蓝云", true);
properties.setList(new ArrayList());
properties.getList().add(property1);
properties.getList().add(property2);
properties.getList().add(property3);
properties.getList().add(property4);
properties.getList().add(property5);
properties.getList().add(property6);
properties.getList().add(property7);
//指定源文档打开密码,无密码则为null
String password = null;
//指定源文档存放文件夹
String folder = "input";
//指定存储空间,使用冰蓝云默认存储空间,设置为null
String storage = null;
//调用setDocumentProperties方法添加属性,并保存到原路径
propertiesApi.setDocumentProperties(name, properties, password, folder, storage);
}
}
示例2、删除特定文档属性
import spire.cloud.excel.sdk.ApiException;
import spire.cloud.excel.sdk.Configuration;
import spire.cloud.excel.sdk.api.PropertiesApi;
public class DeleteSpecificProperty {
static String appId = "App ID";
static String appKey = "App Key";
static String baseUrl = "https://api.e-iceblue.cn";
//配置App ID和App Key
static Configuration configuration = new Configuration(appId, appKey, baseUrl);
//初始化PropertiesApi对象
static PropertiesApi propertiesApi = new PropertiesApi(configuration);
public static void main(String[] args) throws ApiException {
//指定源文档名称
String name = "sample.xlsx";
//指定要移除的文档属性名
String propertyName = "Title";
//指定源文档打开密码,无密码则为null
String password = null;
//指定源文档存放文件夹
String folder = "input";
//指定存储空间,使用冰蓝云默认存储空间,设置为null
String storage = null;
//调用deleteDocumentProperty方法删除特定属性,并保存到原路径
propertiesApi.deleteDocumentProperty(name, propertyName, password, folder, storage);
}
}
示例3、删除所有文档属性
import spire.cloud.excel.sdk.ApiException;
import spire.cloud.excel.sdk.Configuration;
import spire.cloud.excel.sdk.api.PropertiesApi;
public class DeleteProperties {
static String appId = "App ID";
static String appKey = "App Key";
static String baseUrl = "https://api.e-iceblue.cn";
//配置App ID和App Key
static Configuration configuration = new Configuration(appId, appKey, baseUrl);
//初始化PropertiesApi对象
static PropertiesApi propertiesApi = new PropertiesApi(configuration);
public static void main(String[] args) throws ApiException {
//指定源文档名称
String name = "sample.xlsx";
//指定源文档打开密码,无密码则为null
String password = null;
//指定源文档存放文件夹
String folder = "input";
//指定存储空间,使用冰蓝云默认存储空间,设置为null
String storage = null;
//调用deleteDocumentProperties方法删除属性,并保存到原路径
propertiesApi.deleteDocumentProperties(name, password, folder, storage);
}
}