天天看點

Java 設定Excel單元格格式—基于Spire.Cloud.SDK for Java

本文介紹使用Spire.Cloud.SDK for Java來設定Excel單元格格式,包括字型、字号、單元格背景、字型下滑線、字型加粗、字型傾斜、字型顔色、單元格對齊方式、單元格邊框等

一、下載下傳SDK及導入jar

1. 建立Maven項目程式(程式使用的IDEA,如果是Eclipse,參照這裡的方法),并在pom.xml檔案中配置 Maven 倉庫路徑:

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>cloud</name>
        <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>      

2. 在 pom.xml 檔案中指定 Spire.cloud.sdk的 Maven 依賴:

<dependencies>
        <dependency>
            <groupId> cloud </groupId>
            <artifactId>spire.cloud.sdk</artifactId>
            <version>3.5.0</version>
        </dependency>
        <dependency>
        <groupId> com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.1</version>
        </dependency>
        <dependency>
            <groupId> com.squareup.okhttp</groupId>
            <artifactId>logging-interceptor</artifactId>
            <version>2.7.5</version>
        </dependency>
        <dependency>
            <groupId> com.squareup.okhttp </groupId>
            <artifactId>okhttp</artifactId>
            <version>2.7.5</version>
        </dependency>
        <dependency>
            <groupId> com.squareup.okio </groupId>
            <artifactId>okio</artifactId>
            <version>1.6.0</version>
        </dependency>
        <dependency>
            <groupId> io.gsonfire</groupId>
            <artifactId>gson-fire</artifactId>
            <version>1.8.0</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.5.18</version>
        </dependency>
        <dependency>
            <groupId> org.threeten </groupId>
            <artifactId>threetenbp</artifactId>
            <version>1.3.5</version>
        </dependency>
</dependencies>      

配置完成後,在 IDEA 中,點選”Import Changes”即可導入所需要的所有jar檔案:

如下導入結果:

Java 設定Excel單元格格式—基于Spire.Cloud.SDK for Java

二、建立應用擷取ID和Key

方法參考如下圖中的步驟:

Java 設定Excel單元格格式—基于Spire.Cloud.SDK for Java

三、文檔路徑

程式使用的文檔路徑是“文檔管理”目錄下的檔案夾路徑,冰藍雲提供的2G的免費存儲空間。

Java 設定Excel單元格格式—基于Spire.Cloud.SDK for Java

四、Java 代碼示例

import spire.cloud.excel.sdk.Configuration;
import spire.cloud.excel.sdk.api.CellsApi;
import spire.cloud.excel.sdk.model.Border;
import spire.cloud.excel.sdk.model.Color;
import spire.cloud.excel.sdk.model.Font;
import spire.cloud.excel.sdk.model.Style;

import java.util.ArrayList;
import java.util.List;

public class SetCellStyle {
    //配置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 = "newtest.xlsx";//用于測試的excel文檔
        String folder = "input";//雲端excel檔案所在檔案夾
        String storage = null;//使用冰藍雲配置的2G存儲空間,可設定為null
        String sheetName = "Sheet1";//指定Excel中的工作表
        String cellName = "C3";//指定單元格

        Style style = new Style();//建立Style
        Font font = new Font();//建立字型
        font.setIsBold(true);//字型加粗
        font.setIsItalic(true);//字型傾斜
        font.setUnderline("Single");//下劃線樣式
        font.setName("楷體");//字型名稱
        font.setSize(15);//字型大小
        font.setColor(new Color(120,220,20,60));//字型顔色
        style.setFont(font);//應用字型樣式

        style.setBackgroundColor(new Color(138,43,226,252));//設定單元格背景顔色
        style.setHorizontalAlignment("Center");//水準對齊方式(居中對齊)
        style.setVerticalAlignment("Center");//垂直對齊方式(居中對齊)

        //添加邊框
        List<Border> list1= new ArrayList<Border>();//建立數組
        Border border1 = new Border();//建立邊框1
        border1.setLineStyle("Medium");//邊框線條樣式
        border1.setColor(new Color(255, 252, 39, 4));//邊框顔色
        border1.setBorderType("EdgeTop");//邊框類型
        list1.add(border1);//添加邊框1到數組

        Border border2 = new Border();//建立邊框2
        border2.setLineStyle("DashDot");
        border2.setColor(new Color(255, 252, 39, 4));
        border2.setBorderType("EdgeRight");
        list1.add(border2);//添加邊框2到數組
        style.setBorderCollection(list1);//設定邊框到單元格樣式

        //調用方法設定單元格樣式
        cellsApi.setCellStyle(name, sheetName, cellName, style, folder, storage);
    }
}      

單元格設定效果前後對比:

設定前:

Java 設定Excel單元格格式—基于Spire.Cloud.SDK for Java

設定後:

Java 設定Excel單元格格式—基于Spire.Cloud.SDK for Java

(完)