zoukankan      html  css  js  c++  java
  • 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文件:

    如下导入结果:

    二、创建应用获取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);
        }
    }

    单元格设置效果前后对比:

    设置前:

    设置后:

    (完)

  • 相关阅读:
    Roce ofed 环境搭建与测试
    Ubuntu 1804 搭建NFS服务器
    Redhat 8.0.0 安装与网络配置
    Centos 8.1 安装与网络配置
    SUSE 15.1 系统安装
    VSpare ESXi 7.0 基本使用(模板、iso、SRIOV)
    VSpare ESXi 7.0 服务器安装
    open SUSE leap 15.1 安装图解
    KVM虚拟机网卡连接网桥
    GitHub Action一键部署配置,值得拥有
  • 原文地址:https://www.cnblogs.com/Yesi/p/13099197.html
Copyright © 2011-2022 走看看