Spire.Cloud.Excel provides developers with the CellsApi interface to manipulate cells in Excel document. This article will demonstrate how to merge excel cells and set cell format by using Spire.Cloud.Excel API.
The original Excel document is shown as below:
Merge Excel Cells
import spire.cloud.excel.sdk.ApiException;
import spire.cloud.excel.sdk.Configuration;
import spire.cloud.excel.sdk.api.CellsApi;
public class MergeCells {
static String appId = "APP ID";
static String appKey = "APP Key";
static String baseUrl = "https://api.cloudxdocs.com";
//Create a Configuration object based on App ID, App Key and base Url
static Configuration configuration = new Configuration(appId, appKey, baseUrl);
//Create an instance of CellsApi
static CellsApi cellsApi = new CellsApi(configuration);
public static void main(String[] args) throws ApiException {
//Specify the name of the input document
String name = "test.xlsx";
//Specify the first worksheet
String sheetName = "Sheet1";
//Specify the folder where the input document is stored, set it to null if there's none
String folder = "input";
//Store the document in the 2G default storage space offered by E-iceblue Cloud
String storage = null;
//Merge cells from row 2 to row 5 in column 3 with the mergeCells method
int startRow = 2;
int startColumn = 3;
int totalRows = 4;
int totalColumns = 1;
cellsApi.mergeCells(name, sheetName, startRow, startColumn, totalRows, totalColumns, folder, storage);
//Repeat the above steps and Merge cells from row 6 to row 8 in column 3
startRow = 6;
startColumn = 3;
totalRows = 3;
totalColumns = 1;
cellsApi.mergeCells(name, sheetName, startRow, startColumn, totalRows, totalColumns, folder, storage);
}
}
Set Cell Format
import spire.cloud.excel.sdk.ApiException;
import spire.cloud.excel.sdk.Configuration;
import spire.cloud.excel.sdk.api.CellsApi;
import spire.cloud.excel.sdk.model.*;
public class FormatCell {
static String appId = "APP ID";
static String appKey = "App Key";
static String baseUrl = "https://api.cloudxdocs.com";
//Create a Configuration object based on App ID, App Key and base Url
static Configuration configuration = new Configuration(appId, appKey, baseUrl);
//Create an instance of CellsApi
static CellsApi cellsApi = new CellsApi(configuration);
public static void main(String[] args) throws ApiException {
//Specify the name of the input document
String name = "test.xlsx";
//Specify the worksheet
String sheetName = "Sheet1";
//Specify the Excel cell
String cellName = "C2";
//Set the font style of cell C2
Style style = new Style();
Font font = new Font();
font.setIsBold(true);
font.setUnderline("Single");
font.setName("Times New Roman");
font.setSize(11);
style.setFont(font);
//Set the background color of cell C2
style.setBackgroundColor(new Color(255,169,208,142));
//Set the alignment of cell C2
style.setHorizontalAlignment("Left");
//Specify the folder where the input document is stored, set it to null if there's none
String folder = "input";
//Store the document in the 2G default storage space offered by E-iceblue Cloud
String storage = null;
//Set cell format with the setCellStyle method
cellsApi.setCellStyle(name, sheetName, cellName, style, folder, storage);
}
}