前言
Spire.Cloud.Excel为开发人员提供了WorksheetsApi接口用于操作Excel工作表。本文将介绍如何应用该接口来为Excel文档增加新的工作表,及暗藏、删除文档中已有的工作表。
操作步骤:
步骤一:通过冰蓝云官网(https://cloud.e-iceblue.cn/)注册账号并登陆,在"我的利用"版块创立应用程序,以取得App ID及App Key。
步骤二:点击导航栏"文档治理",上传Excel示例文档至"我的文档"。
步骤三:创立Maven应用程序,在pom.xml文件中配置 Maven 仓库门路及增加Spire.Cloud.sdk的Maven依赖。
<repositories> <repository> <id>com.e-iceblue</id> <name>cloud</name> <url>http://repo.e-iceblue.cn/repository/maven-public/</url> </repository></repositories><dependencies> <dependency> <groupId> cloud </groupId> <artifactId>spire.cloud.sdk</artifactId> <version>3.5.0</version> </dependency> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> <version>1.5.18</version> </dependency> <dependency> <groupId>com.squareup.okhttp</groupId> <artifactId>okhttp</artifactId> <version>2.7.5</version> </dependency> <dependency> <groupId>com.squareup.okhttp</groupId> <artifactId>logging-interceptor</artifactId> <version>2.7.5</version> </dependency> <dependency> <groupId> com.squareup.okio </groupId> <artifactId>okio</artifactId> <version>1.6.0</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.1</version> </dependency> <dependency> <groupId>io.gsonfire</groupId> <artifactId>gson-fire</artifactId> <version>1.8.0</version> </dependency> <dependency> <groupId>org.threeten</groupId> <artifactId>threetenbp</artifactId> <version>1.3.5</version> </dependency></dependencies>
配置实现后,在 IDEA 中,点击"Import Changes"即可导入 JAR 包;在 Eclipse 中,则需点击"Save"按钮。
步骤四:在Maven程序中编写代码调用WorksheetsApi接口来增加、暗藏及删除Excel工作表。
增加新的工作表至Excel文档:
import spire.cloud.excel.sdk.*;import spire.cloud.excel.sdk.api.WorksheetsApi;public class AddNewWorksheet { static String appId = "App ID"; static String appKey = "App Key"; static String baseUrl = "https://api.e-iceblue.cn"; public static void main(String[] args) throws ApiException { //配置App ID和App Key Configuration configuration = new Configuration(appId, appKey, baseUrl); //初始化WorksheetsApi对象 WorksheetsApi WorksheetsApi = new WorksheetsApi(configuration); //指定示例Excel文档 String name = "AddNewWorksheet.xlsx"; //寄存示例文档的文件夹,如果没有文件夹则为null String folder = null; //应用冰蓝云配置的2G空间存贮文档,可设置为null String storage = null; //设置工作表类型 String sheetType = "NormalWorksheet";//Available value: NormalWorksheet, ChartSheet //通过索引指定增加新工作表的地位 int index = 2; //设置新增加工作表名称 String newSheetName = "NewWorksheet"; //调用insertNewWorksheet办法来增加新的工作表至Excel文档 WorksheetsApi.insertNewWorksheet(name, newSheetName, index, sheetType, folder, storage); }}
增加成果:
暗藏工作表
import spire.cloud.excel.sdk.ApiException;import spire.cloud.excel.sdk.Configuration;import spire.cloud.excel.sdk.api.WorksheetsApi;public class HideWorksheet { static String appId = "App ID"; static String appKey = "App Key"; static String baseUrl = "https://api.e-iceblue.cn"; public static void main(String[] args) throws ApiException { //配置App ID和App Key Configuration configuration = new Configuration(appId, appKey, baseUrl); //初始化WorksheetsApi对象 WorksheetsApi WorksheetsApi = new WorksheetsApi(configuration); //指定示例Excel文档 String name = "HideWorksheet.xlsx"; //寄存示例文档的文件夹,如果没有文件夹则为null String folder = null; //应用冰蓝云配置的2G空间存贮文档,可设置为null String storage = null; //指定被暗藏的工作表 String sheetName = "Sheet1"; //设置暗藏 boolean isVisible = false; //设置显示 //boolean isVisible = true; //调用changeVisibilityWorksheet办法来暗藏Excel文档中的第一个工作表 WorksheetsApi.changeVisibilityWorksheet(name, sheetName, isVisible, folder, storage); }}
暗藏成果:
删除工作表
import spire.cloud.excel.sdk.ApiException;import spire.cloud.excel.sdk.Configuration;import spire.cloud.excel.sdk.api.WorksheetsApi;public class DeleteWorksheet { static String appId = "App ID"; static String appKey = "App Key"; static String baseUrl = "https://api.e-iceblue.cn"; public static void main(String[] args) throws ApiException { //配置App ID和App Key Configuration configuration = new Configuration(appId, appKey, baseUrl); //初始化WorksheetsApi对象 WorksheetsApi WorksheetsApi = new WorksheetsApi(configuration); //指定示例Excel文档 String name = "DeleteWorksheet.xlsx"; //寄存示例文档的文件夹,如果没有文件夹则为null String folder = null; //应用冰蓝云配置的2G空间存贮文档,可设置为null String storage = null; //指定需被删除的工作表 String sheetName = "Sheet2"; //调用deleteWorksheet办法来删除Excel文档中的第二个工作表 WorksheetsApi.deleteWorksheet(name, sheetName, folder, storage); }}
删除成果:
(本文完)