在当今数据驱动的时代,高效地将数据转化为直观的视觉信息变得至关重要。PowerPoint 图表是数据分析和报告中不可或缺的组成部分,但手动创建和更新大量图表既耗时又容易出错。本文将深入探讨如何利用 Spire.Presentation for Java 库,以编程方式自动化创建和美化 PowerPoint 图表,从而大幅提升您的工作效率,实现真正的自动化 PPT。

一、库介绍与安装:Spire.Presentation for Java 概述

Spire.Presentation for Java 是一款功能强大的 Java API,专为创建、读取、写入和修改 PowerPoint 演示文稿而设计。它支持 PPT、PPTX 等多种格式,提供了丰富的对象模型,允许开发者以编程方式操作演示文稿的各个元素,包括幻灯片、形状、文本、图片、表格以及最重要的——图表。Spire.Presentation for Java 的优势在于其无需安装 Microsoft PowerPoint 即可独立运行,且 API 接口直观易用,是实现 Java 图表自动化生成的理想选择。

Maven 依赖配置

要在您的 Java 项目中引入 Spire.Presentation 库,最便捷的方式是使用 Maven 或 Gradle。以下是 Maven 的配置示例:


    
        com.e-iceblue
        e-iceblue
        https://repo.e-iceblue.cn/repository/maven-public/
    


    
        e-iceblue
        spire.presentation
        10.10.2
    

二、在 PowerPoint 中创建条形图

条形图是显示不同类别数据之间比较的常用图表类型。以下是如何使用 Spire.Presentation for Java 创建一个简单的条形图的步骤和代码示例。

2.1 创建条形图的步骤

  • 创建一个新的演示文稿对象。
  • 添加一张幻灯片。
  • 在幻灯片上插入一个条形图,并指定其位置和大小。
  • 获取图表的数据表,并填充数据。
  • 设置图表的标题、图例等属性。
  • 将演示文稿保存为 PPTX 文件。

2.2 Java 代码示例:创建条形图

import com.spire.presentation.*;
import com.spire.pdf.tables.table.*;
import com.spire.presentation.charts.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.geom.Rectangle2D;
import java.lang.Object;
public class CreateChart {
    public static void main(String[] args) throws Exception {
        //实例化一个Presentation对象
        Presentation presentation = new Presentation();
        //插入柱形图
        Rectangle2D.Double rect = new Rectangle2D.Double(40, 100, 550, 320);
        IChart chart = null;
        chart = presentation.getSlides().get(0).getShapes().appendChart(ChartType.COLUMN_CLUSTERED, rect);
        //添加表名
        chart.getChartTitle().getTextProperties().setText("销售报表");
        chart.getChartTitle().getTextProperties().isCentered(true);
        chart.getChartTitle().setHeight(30);
        chart.hasTitle(true);
        //创建后台数据表
        DataTable dataTable = new DataTable();
        dataTable.getColumns().add(new DataColumn("销售额", DataTypes.DATATABLE_STRING));
        dataTable.getColumns().add(new DataColumn("谷物", DataTypes.DATATABLE_INT));
        dataTable.getColumns().add(new DataColumn("粮油", DataTypes.DATATABLE_INT));
        dataTable.getColumns().add(new DataColumn("百货", DataTypes.DATATABLE_INT));
        DataRow row1 = dataTable.newRow();
        row1.setString("销售额", "门店1");
        row1.setInt("谷物", 250);
        row1.setInt("粮油", 150);
        row1.setInt("百货", 99);
        DataRow row2 = dataTable.newRow();
        row2.setString("销售额", "门店2");
        row2.setInt("谷物", 270);
        row2.setInt("粮油", 150);
        row2.setInt("百货", 99);
        DataRow row3 = dataTable.newRow();
        row3.setString("销售额", "门店3");
        row3.setInt("谷物", 310);
        row3.setInt("粮油", 120);
        row3.setInt("百货", 49);
        DataRow row4 = dataTable.newRow();
        row4.setString("销售额", "门店4");
        row4.setInt("谷物", 330);
        row4.setInt("粮油", 120);
        row4.setInt("百货", 49);
        DataRow row5 = dataTable.newRow();
        row5.setString("销售额", "门店5");
        row5.setInt("谷物", 360);
        row5.setInt("粮油", 150);
        row5.setInt("百货", 141);
        DataRow row6 = dataTable.newRow();
        row6.setString("销售额", "门店6");
        row6.setInt("谷物", 380);
        row6.setInt("粮油", 150);
        row6.setInt("百货", 135);
        dataTable.getRows().add(row1);
        dataTable.getRows().add(row2);
        dataTable.getRows().add(row3);
        dataTable.getRows().add(row4);
        dataTable.getRows().add(row5);
        dataTable.getRows().add(row6);
        //将数据写入图表
        for (int c = 0; c < dataTable.getColumns().size(); c++) {
            chart.getChartData().get(0, c).setText(dataTable.getColumns().get(c).getColumnName());
        }
        for (int r = 0; r < dataTable.getRows().size(); r++) {
            Object[] datas = dataTable.getRows().get(r).getArrayList();
            for (int c = 0; c < datas.length; c++) {
                chart.getChartData().get(r + 1, c).setValue(datas[c]);
            }
        }
        //设置系列标签
        chart.getSeries().setSeriesLabel(chart.getChartData().get("B1", "D1"));
        //设置类别标签
        chart.getCategories().setCategoryLabels(chart.getChartData().get("A2", "A7"));
        //为各个系列赋值
        chart.getSeries().get(0).setValues(chart.getChartData().get("B2", "B7"));
        chart.getSeries().get(1).setValues(chart.getChartData().get("C2", "C7"));
        chart.getSeries().get(2).setValues(chart.getChartData().get("D2", "D7"));
        chart.getSeries().get(2).getFill().setFillType(FillFormatType.SOLID);
        chart.getSeries().get(2).getFill().getSolidColor().setKnownColor(KnownColors.LIGHT_BLUE);
        //设置系列重叠
        chart.setOverLap(-50);
        //设置类别间距
        chart.setGapDepth(200);
        //保存文档
        presentation.saveToFile("output/CreateChart.pptx", FileFormat.PPTX_2010);
    }
}

三、在 PowerPoint 中创建折线图

折线图常用于展示数据随时间变化的趋势。以下是如何使用 Spire.Presentation for Java 创建一个折线图的详细步骤和代码。

3.1 创建折线图的步骤

  • 创建一个新的演示文稿对象。
  • 添加一张幻灯片。
  • 在幻灯片上插入一个折线图,并指定其位置和大小。
  • 获取图表的数据表,并填充数据。
  • 设置图表的标题、图例、轴标签等属性。
  • 将演示文稿保存为 PPTX 文件。

3.2 Java 代码示例:创建折线图

import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
import com.spire.presentation.SlideSizeType;
import com.spire.presentation.charts.ChartLegendPositionType;
import com.spire.presentation.charts.ChartType;
import com.spire.presentation.charts.IChart;
import java.awt.geom.Rectangle2D;
public class LineChart {
    public static void main(String[] args) throws Exception {
        //创建Presentation对象
        Presentation presentation = new Presentation();
        presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);
        //插入折线图
        Rectangle2D.Double rect = new   Rectangle2D.Double(100, 50, 600, 430);
        IChart chart = presentation.getSlides().get(0).getShapes().appendChart(ChartType.LINE, rect);
        //设置图表标题
        chart.getChartTitle().getTextProperties().setText("产品月销量趋势");
        chart.getChartTitle().getTextProperties().isCentered(true);
        chart.getChartTitle().setHeight(30);
        chart.hasTitle(true);
        //设置轴标题
        chart.getPrimaryCategoryAxis().getTitle().getTextProperties().setText("月份");
        chart.getPrimaryCategoryAxis().hasTitle(true);
        chart.getPrimaryValueAxis().getTitle().getTextProperties().setText("销量");
        chart.getPrimaryValueAxis().hasTitle(true);
        //写入图表数据
        chart.getChartData().get(0,0).setText("月份");
        chart.getChartData().get(1,0).setText("一月");
        chart.getChartData().get(2,0).setText("二月");
        chart.getChartData().get(3,0).setText("三月");
        chart.getChartData().get(4,0).setText("四月");
        chart.getChartData().get(5,0).setText("五月");
        chart.getChartData().get(6,0).setText("六月");
        chart.getChartData().get(0,1).setText("台式机");
        chart.getChartData().get(1,1).setNumberValue(80);
        chart.getChartData().get(2,1).setNumberValue(45);
        chart.getChartData().get(3,1).setNumberValue(25);
        chart.getChartData().get(4,1).setNumberValue(20);
        chart.getChartData().get(5,1).setNumberValue(10);
        chart.getChartData().get(6,1).setNumberValue(5);
        chart.getChartData().get(0,2).setText("笔记本");
        chart.getChartData().get(1,2).setNumberValue(30);
        chart.getChartData().get(2,2).setNumberValue(25);
        chart.getChartData().get(3,2).setNumberValue(35);
        chart.getChartData().get(4,2).setNumberValue(50);
        chart.getChartData().get(5,2).setNumberValue(45);
        chart.getChartData().get(6,2).setNumberValue(55);
        chart.getChartData().get(0,3).setText("平板");
        chart.getChartData().get(1,3).setNumberValue(10);
        chart.getChartData().get(2,3).setNumberValue(15);
        chart.getChartData().get(3,3).setNumberValue(20);
        chart.getChartData().get(4,3).setNumberValue(35);
        chart.getChartData().get(5,3).setNumberValue(60);
        chart.getChartData().get(6,3).setNumberValue(95);
        //设置系列标签
        chart.getSeries().setSeriesLabel(chart.getChartData().get("B1", "D1"));
        //设置分类标签
        chart.getCategories().setCategoryLabels(chart.getChartData().get("A2", "A7"));
        //设置系列数据区域
        chart.getSeries().get(0).setValues(chart.getChartData().get("B2", "B7"));
        chart.getSeries().get(1).setValues(chart.getChartData().get("C2", "C7"));
        chart.getSeries().get(2).setValues(chart.getChartData().get("D2", "D7"));
        //在数据标签中显示数据
        chart.getSeries().get(0).getDataLabels().setLabelValueVisible(true);
        chart.getSeries().get(1).getDataLabels().setLabelValueVisible(true);
        chart.getSeries().get(2).getDataLabels().setLabelValueVisible(true);
        //设置图例位置
        chart.getChartLegend().setPosition(ChartLegendPositionType.TOP);
        //保存文档
        presentation.saveToFile("LineChart.pptx", FileFormat.PPTX_2013);
    }
}

结论

通过本文的介绍和代码示例,我们展示了如何利用 Spire.Presentation for Java 这一强大的库,在 PowerPoint 中自动化创建出美观且富有洞察力的图表。无论是复杂的条形图还是展示趋势的折线图,Spire.Presentation for Java 都能助您轻松实现。掌握这项技术,您将能够显著提升数据可视化和报告生成的效率,更好地应对自动化办公的挑战。现在就尝试使用 Spire.Presentation for Java,开启您的自动化 PowerPoint 图表之旅吧!