Spire.Cloud.SDK for Java 是Spire.Cloud云產品系列中,用于處理Word、Excel、PowerPoint以及PDF檔案的JAR檔案,可執行檔案編輯、轉換、保存等操作,本文以操作Excel單元格實作單元格合并、拆分功能為例,介紹如何創建程式并獲取程式ID和key來配置程式賬號資訊,并呼叫介面提供的方法來實作單元格合并和拆分,具體可參考以下步驟:
一、匯入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 = "https://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 = "https://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); } }
拆分結果:
拆分前

拆分后

(完)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/167800.html
標籤:Java
