Spire.Cloud.SDK for Java 合併、拆分Excel單元格
- 2020 年 6 月 9 日
- 筆記
- excel, jar, JAVA, maven, SDK, Spire.Cloud.Excel, Spire.Cloud.SDK for Java, 單元格, 合併, 拆分
Spire.Cloud.SDK for Java 是Spire.Cloud雲產品系列中,用於處理Word、Excel、PowerPoint以及PDF文檔的JAR文件,可執行文檔編輯、轉換、保存等操作。本文以操作Excel單元格實現單元格合併、拆分功能為例,介紹如何創建程式並獲取程式ID和key來配置程式帳號資訊,並調用介面提供的方法來實現單元格合併和拆分。具體可參考以下步驟:
一、下載SDK及導入jar
下載後,創建Maven項目程式,並在pom.xml文件中配置 Maven 倉庫路徑,指定 spire.cloud.sdk的 Maven 依賴,導入程式需要的所有jar文件。如下導入結果:
二、創建應用獲取ID和Key
三、文檔路徑
程式使用的文檔路徑是「文檔管理」目錄下的文件夾路徑,冰藍雲提供的2G的免費存儲空間。
四、Java 程式碼
1. 合併單元格
import spire.cloud.excel.sdk.Configuration; import spire.cloud.excel.sdk.api.CellsApi; public class MergeCells { //配置App ID和App Key等應用帳號資訊 static String appId = "App ID"; static String appKey = "App Key"; static String baseUrl = "//api.e-iceblue.cn"; static Configuration configuration = new Configuration(appId, appKey, baseUrl); static CellsApi cellsApi = new CellsApi(configuration); public static void main(String[] args) throws Exception { String name = "test.xlsx";//Excel測試文檔 String folder = "input";//Excel文檔所在的雲端文件夾 String storage = null;//使用冰藍雲配置的2G存儲空間,可設置為null String sheetName = "Sheet2";//指定Excel中的工作表 //指定合併的起始行和列,並指定需要合併的行數和列數 int startRow = 1; int startColumn = 2; int totalRows = 4; int totalColumns = 3; //調用介面提供的方法合併單元格(運行程式後,可在雲端的源文檔中查看單元格合併效果) cellsApi.mergeCells(name, sheetName, startRow, startColumn, totalRows, totalColumns, folder, storage); } }
單元格合併前/後效果:
合併前
合併後
2. 拆分單元格
import spire.cloud.excel.sdk.Configuration; import spire.cloud.excel.sdk.api.CellsApi; public class UnmergeCell { //配置App ID和App Key等應用帳號資訊 static String appId = "App ID"; static String appKey = "App Key"; static String baseUrl = "//api.e-iceblue.cn"; static Configuration configuration = new Configuration(appId, appKey, baseUrl); static CellsApi cellsApi = new CellsApi(configuration); public static void main(String[] args) throws Exception{ String name = "test.xlsx";//Excel測試文檔 String folder = "input";//Excel文檔所在的雲端文件夾 String storage = null;//使用冰藍雲配置的2G存儲空間,可設置為null String sheetName = "Sheet3";//指定Excel中的工作表 int startRow = 3; int startColumn = 3; int totalRows = 1; int totalColumns =1; //調用方法拆分單元格(運行程式後,可在雲端的源文檔中查看單元格拆分效果) cellsApi.unmergeCells(name, sheetName, startRow, startColumn, totalRows, totalColumns, folder, storage); } }
拆分結果:
拆分前
拆分後
(完)