本文介紹使用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檔案:
如下匯入結果:

二、創建應用獲取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); } }
單元格設定效果前后對比:
設定前:

設定后:

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