Java 合並、拆分Excel單元格(基於Spire.Cloud.SDK for Java)


Spire.Cloud.SDK for Java 是Spire.Cloud雲產品系列中,用於處理WordExcelPowerPoint以及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);
    }
}

拆分結果:

拆分前

拆分后

 

(完)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM