Java 設置Excel單元格格式—基於Spire.Cloud.SDK for Java

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

一、下載SDK及導入jar

1. 下載地址

2. 下載後,創建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>

在 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文件:

如下導入結果:

二、創建應用獲取ID和Key

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

三、文檔路徑

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

四、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);
    }
}

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

設置前:

設置後:

 

(完)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章