您的位置 首页 java

Java 给Excel图表设置背景颜色和背景图片

在前文中,我介绍过如何给Excel工作表设置背景色,其中包括三部分:给工作表中 所有数据的单元格区域 设置背景色、给工作表中的 指定单元格区域 设置背景色以及给工作表的 设置 交替背景色 。本篇教程将演示如何给Excel图表设置背景色和背景图片,其中设置区域可为整个图表区域,也可是图表中的绘图区域。

使用工具: Free Spire.XLS for Java (免费版)

在运行代码前,在E-iceblue中文官网上下载产品包,然后手动将Spire.Xls.jar导入IDEA,或在Maven仓库下的pom.xml文件中引入以下配置来进行导入。

 <repositories>
        <repository>
            <id>com.e-iceblue</id>
            <url>
        </repository>
    </repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.xls.free</artifactId>
        <version>3.9.1</version>
    </dependency>
</dependencies>  

以下截图为用于测试的Excel图表:

代码演示

 import com.spire.xls.*;
import java.awt.*;

public class BackgroundOfChart {
    public static void main(String[] args) {
        //创建实例,加载测试文档
        Workbook wb = new Workbook();
        wb.loadFromFile("C:\Users\Test1\Desktop\Sample.xlsx");

        //获取工作表
        Worksheet sheet = wb.getWorksheets().get(0);

        //获取图表
        Chart chart = sheet.getCharts().get(0);

        //设置图表区域颜色填充
        chart.getChartArea().getFill().setForeColor(new Color(255,228,225));
        //设置图表绘图区域颜色填充
        //chart.getPlotArea().getFill().setForeColor(new Color(221,160,221));

        //设置图表区域图片填充
        //chart.getChartArea().getFill().customPicture("C:\Users\Test1\Desktop\Image.jpg");
        //chart.getChartArea().getFill().setTransparency(0.5);
        //设置图表绘图区域图片填充
        //chart.getPlotArea().getFill().customPicture("C:\Users\Test1\Desktop\Picture.jpg");
        //chart.getPlotArea().getFill().setTransparency(0.5);

        //保存结果文档
        wb.saveToFile("output/BackgroundOfChart.xlsx");
        wb.dispose();
    }
}  

背景颜色设置效果

整个图表区域

绘图区域

背景图片设置效果

整个图表区域

绘图区域

文章来源:智云一二三科技

文章标题:Java 给Excel图表设置背景颜色和背景图片

文章地址:https://www.zhihuclub.com/174407.shtml

关于作者: 智云科技

热门文章

网站地图