图表能把成组的数据变成一眼可读的图形,让演示文稿中的占比、趋势和对比关系更直观。手工在 PowerPoint 里插入图表,需要逐项选择类型、粘贴数据、调整标题和标签,重复几次就很花时间;而在浏览器端直接生成带图表的演示文稿,可以把这一步并入自动化的文档处理流程。
本文使用 Spire.Presentation for JavaScript,在 React 项目中通过 WebAssembly 新建 PowerPoint 演示文稿并插入图表,整个处理过程在用户设备本地完成,无需安装 Office,也不依赖后端服务,演示文稿不必离开浏览器。该产品支持柱状图、折线图、饼状图、条形图、散点图、气泡图等多种图表类型;本教程从中选取柱状图、饼状图和折线图三种常用类型作为说明。
本文介绍三个核心功能点:
有关安装和项目配置,请参考 React 项目中集成 Spire.Presentation for JavaScript。以下示例默认 Spire.Presentation for JavaScript 已安装、WebAssembly 模块已初始化。
创建柱状图
柱状图适合并排比较多个分类下的若干个系列,每个分类都有一组高度不同的柱形,高低对比一目了然。要生成这样的图表,先把各分类的数值写进图表的数据表,再为每个系列绑定对应的数值区域,Spire.Presentation 会据此在幻灯片上绘制出柱形。
| 写法 | 得到的结果 |
|---|---|
slide.Shapes.AppendChart({ type: ChartType.ColumnClustered, rectangle, init }) |
在幻灯片指定位置插入一个簇状柱形图 |
chart.ChartTitle.TextProperties.Text 与 chart.HasTitle = true |
设置并显示图表标题 |
chart.ChartData.get_Item({ row, column }).Text |
写入系列名称与分类名称 |
chart.ChartData.get_Item({ row, column }).NumberValue |
写入各分类对应的数值 |
chart.Series.SeriesLabel |
指定系列名称所在的区域 |
chart.Categories.CategoryLabels |
指定分类标签所在的区域 |
chart.Series.get_Item(i).Values |
为第 i 个系列绑定数值区域 |
具体操作步骤如下:
- 获取 Spire.Presentation 的 WASM 模块并确认已就绪,载入字体文件,再用
new Presentation()新建空白演示文稿,取得第一张幻灯片。 - 用
RectangleF.FromLTRB()指定图表的矩形区域,调用Shapes.AppendChart()并传入ChartType.ColumnClustered,插入簇状柱形图。 - 设置
ChartTitle.TextProperties.Text并把HasTitle置为true,让图表显示标题。 - 通过
ChartData.get_Item()的Text写入系列名称与分类名称,用NumberValue写入每个分类下的数值。 - 用
Series.SeriesLabel与Categories.CategoryLabels绑定系列名和分类标签,再用Series.get_Item(i).Values为每个系列绑定数值区域。 - 调用
Presentation.SaveToFile()保存结果文件,用Dispose()释放演示文稿,再从 VFS 读出文件并触发下载。
创建柱状图的代码如下:
function App() {
// 创建柱状图
const createColumnChart = async () => {
// 获取 Spire.Presentation WASM 模块
const pptModule = window.wasmModule?.spirepresentation;
// 检查模块是否就绪
if (!pptModule) {
alert('Spire.Presentation is not ready yet');
return;
}
// 载入字体,保证图表中的文字正常渲染
await window.spire.FetchFileToVFS('simsun.ttc', '/Library/Fonts/', `${process.env.PUBLIC_URL}/font/`);
// 新建空白演示文稿,取得第一张幻灯片
const ppt = new pptModule.Presentation();
const slide = ppt.Slides.get_Item(0);
// 在幻灯片上插入簇状柱形图
const rect = pptModule.RectangleF.FromLTRB(90, 100, 640, 420);
const chart = slide.Shapes.AppendChart({ type: pptModule.ChartType.ColumnClustered, rectangle: rect, init: false });
// 设置图表标题
chart.ChartTitle.TextProperties.Text = '各季度销售额';
chart.ChartTitle.TextProperties.IsCentered = true;
chart.ChartTitle.Height = 30;
chart.HasTitle = true;
// 准备两个系列的数据
const quarters = ['第一季度', '第二季度', '第三季度', '第四季度'];
const online = [128, 156, 142, 180];
const offline = [96, 88, 110, 124];
// 写入系列名称
chart.ChartData.get_Item({ row: 0, column: 1 }).Text = '线上渠道';
chart.ChartData.get_Item({ row: 0, column: 2 }).Text = '线下渠道';
// 写入分类名称与各分类的数值
for (let i = 0; i < quarters.length; i++) {
chart.ChartData.get_Item({ row: i + 1, column: 0 }).Text = quarters[i];
chart.ChartData.get_Item({ row: i + 1, column: 1 }).NumberValue = online[i];
chart.ChartData.get_Item({ row: i + 1, column: 2 }).NumberValue = offline[i];
}
// 绑定系列名称、分类标签与各系列的数值区域
chart.Series.SeriesLabel = chart.ChartData.get_Item({ name: 'B1', endCellName: 'C1' });
chart.Categories.CategoryLabels = chart.ChartData.get_Item({ name: 'A2', endCellName: 'A5' });
chart.Series.get_Item(0).Values = chart.ChartData.get_Item({ name: 'B2', endCellName: 'B5' });
chart.Series.get_Item(1).Values = chart.ChartData.get_Item({ name: 'C2', endCellName: 'C5' });
// 保存结果文件
const outputFileName = 'ColumnChart_result.pptx';
ppt.SaveToFile({ file: outputFileName, fileFormat: pptModule.FileFormat.Pptx2013 });
ppt.Dispose();
// 从 VFS 读取生成的文件,触发下载
downloadFile(outputFileName);
};
// 从 VFS 读取生成的文件,触发下载
const downloadFile = (outputFileName) => {
const fileArray = window.dotnetRuntime.Module.FS.readFile(outputFileName);
const blob = new Blob([fileArray], { type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = outputFileName;
a.click();
URL.revokeObjectURL(url);
};
return (
<div style={{ textAlign: 'center', height: '300px' }}>
<h1>在 PowerPoint 中创建图表</h1>
<button onClick={createColumnChart}>创建柱状图</button>
</div>
);
}
export default App;
创建柱状图后的演示文稿:

创建饼状图
饼状图用扇区的大小表示各部分在整体中的占比,适合展示只有一组数值的分类数据。除了插入图形,还可以为每个扇区分别设置颜色,并让标签同时显示数值和百分比,使各部分的构成关系更清楚。
| 写法 | 得到的结果 |
|---|---|
slide.Shapes.AppendChart({ type: ChartType.Pie, rectangle, init }) |
在幻灯片指定位置插入一个饼图 |
chart.ChartData.get_Item({ row, column }).Text / NumberValue |
写入单一系列的分类与数值 |
chart.Series.get_Item(i).DataPoints.Add(cdp) |
为系列逐个创建数据点 |
point.Fill.FillType = FillFormatType.Solid 与 point.Fill.SolidColor.Color |
为各扇区填充指定的纯色 |
chart.Series.get_Item(i).DataLabels.LabelValueVisible |
在扇区上显示数值 |
chart.Series.get_Item(i).DataLabels.PercentValueVisible |
在扇区上显示百分比 |
具体操作步骤如下:
- 获取 Spire.Presentation 的 WASM 模块并确认已就绪,载入字体文件,再用
new Presentation()新建空白演示文稿,取得第一张幻灯片。 - 用
RectangleF.FromLTRB()指定图表的矩形区域,调用Shapes.AppendChart()并传入ChartType.Pie,插入饼图。 - 设置图表标题,再通过
ChartData.get_Item()写入分类名称、系列名称与各组数值。 - 用
Series.SeriesLabel、Categories.CategoryLabels与Series.get_Item(0).Values绑定系列名、分类标签与数值区域。 - 遍历数值个数,用
new ChartDataPoint()配合DataPoints.Add()为每个扇区建立数据点,再设置Fill.FillType与Fill.SolidColor.Color,让各扇区颜色不同。 - 把
DataLabels.LabelValueVisible与DataLabels.PercentValueVisible置为true,显示数值和百分比。 - 调用
Presentation.SaveToFile()保存结果文件,用Dispose()释放演示文稿,再从 VFS 读出文件并触发下载。
创建饼状图的代码如下:
function App() {
// 创建饼状图
const createPieChart = async () => {
// 获取 Spire.Presentation WASM 模块
const pptModule = window.wasmModule?.spirepresentation;
// 检查模块是否就绪
if (!pptModule) {
alert('Spire.Presentation is not ready yet');
return;
}
// 载入字体,保证图表中的文字正常渲染
await window.spire.FetchFileToVFS('simsun.ttc', '/Library/Fonts/', `${process.env.PUBLIC_URL}/font/`);
// 新建空白演示文稿,取得第一张幻灯片
const ppt = new pptModule.Presentation();
const slide = ppt.Slides.get_Item(0);
// 在幻灯片上插入饼图
const rect = pptModule.RectangleF.FromLTRB(90, 100, 640, 420);
const chart = slide.Shapes.AppendChart({ type: pptModule.ChartType.Pie, rectangle: rect, init: false });
// 设置图表标题
chart.ChartTitle.TextProperties.Text = '各地区销售占比';
chart.ChartTitle.TextProperties.IsCentered = true;
chart.ChartTitle.Height = 30;
chart.HasTitle = true;
// 准备分类与数值
const regions = ['华东', '华北', '华南', '西南'];
const shares = [35, 25, 22, 18];
// 写入分类名称与数值
chart.ChartData.get_Item({ row: 0, column: 0 }).Text = '地区';
chart.ChartData.get_Item({ row: 0, column: 1 }).Text = '占比';
for (let i = 0; i < regions.length; i++) {
chart.ChartData.get_Item({ row: i + 1, column: 0 }).Text = regions[i];
chart.ChartData.get_Item({ row: i + 1, column: 1 }).NumberValue = shares[i];
}
// 绑定系列名称、分类标签与数值区域
chart.Series.SeriesLabel = chart.ChartData.get_Item({ name: 'B1', endCellName: 'B1' });
chart.Categories.CategoryLabels = chart.ChartData.get_Item({ name: 'A2', endCellName: 'A5' });
chart.Series.get_Item(0).Values = chart.ChartData.get_Item({ name: 'B2', endCellName: 'B5' });
// 为每个扇区建立数据点并填充不同颜色
const colors = [
pptModule.Color.get_RosyBrown(),
pptModule.Color.get_LightBlue(),
pptModule.Color.get_LightPink(),
pptModule.Color.get_MediumPurple(),
];
for (let i = 0; i < shares.length; i++) {
const cdp = new pptModule.ChartDataPoint(chart.Series.get_Item(0));
cdp.Index = i;
chart.Series.get_Item(0).DataPoints.Add(cdp);
const point = chart.Series.get_Item(0).DataPoints.get_Item(i);
point.Fill.FillType = pptModule.FillFormatType.Solid;
point.Fill.SolidColor.Color = colors[i];
}
// 在扇区上显示数值与百分比
chart.Series.get_Item(0).DataLabels.LabelValueVisible = true;
chart.Series.get_Item(0).DataLabels.PercentValueVisible = true;
// 保存结果文件
const outputFileName = 'PieChart_result.pptx';
ppt.SaveToFile({ file: outputFileName, fileFormat: pptModule.FileFormat.Pptx2013 });
ppt.Dispose();
// 从 VFS 读取生成的文件,触发下载
downloadFile(outputFileName);
};
// 从 VFS 读取生成的文件,触发下载
const downloadFile = (outputFileName) => {
const fileArray = window.dotnetRuntime.Module.FS.readFile(outputFileName);
const blob = new Blob([fileArray], { type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = outputFileName;
a.click();
URL.revokeObjectURL(url);
};
return (
<div style={{ textAlign: 'center', height: '300px' }}>
<h1>在 PowerPoint 中创建图表</h1>
<button onClick={createPieChart}>创建饼状图</button>
</div>
);
}
export default App;
创建饼状图后的演示文稿:

创建折线图
折线图把同一系列在多个分类上的数值连成一条线,用来观察数值随顺序变化的走向。采用带数据标记的折线图时,每个数值处还会额外标出一个点,转折位置更醒目,便于逐点读数。
| 写法 | 得到的结果 |
|---|---|
slide.Shapes.AppendChart({ type: ChartType.LineMarkers, rectangle, init }) |
在幻灯片指定位置插入一个带数据标记的折线图 |
chart.ChartData.get_Item({ row, column }).Text / NumberValue |
写入系列名称、分类名称与数值 |
chart.Series.SeriesLabel 与 chart.Categories.CategoryLabels |
绑定系列名称与分类标签 |
chart.Series.get_Item(i).Values |
为第 i 个系列绑定数值区域 |
具体操作步骤如下:
- 获取 Spire.Presentation 的 WASM 模块并确认已就绪,载入字体文件,再用
new Presentation()新建空白演示文稿,取得第一张幻灯片。 - 用
RectangleF.FromLTRB()指定图表的矩形区域,调用Shapes.AppendChart()并传入ChartType.LineMarkers,插入带数据标记的折线图。 - 设置图表标题,再通过
ChartData.get_Item()写入系列名称、分类名称与各分类的数值。 - 用
Series.SeriesLabel与Categories.CategoryLabels绑定系列名和分类标签,再用Series.get_Item(i).Values为每个系列绑定数值区域。 - 调用
Presentation.SaveToFile()保存结果文件,用Dispose()释放演示文稿,再从 VFS 读出文件并触发下载。
创建折线图的代码如下:
function App() {
// 创建折线图
const createLineChart = async () => {
// 获取 Spire.Presentation WASM 模块
const pptModule = window.wasmModule?.spirepresentation;
// 检查模块是否就绪
if (!pptModule) {
alert('Spire.Presentation is not ready yet');
return;
}
// 载入字体,保证图表中的文字正常渲染
await window.spire.FetchFileToVFS('simsun.ttc', '/Library/Fonts/', `${process.env.PUBLIC_URL}/font/`);
// 新建空白演示文稿,取得第一张幻灯片
const ppt = new pptModule.Presentation();
const slide = ppt.Slides.get_Item(0);
// 在幻灯片上插入带数据标记的折线图
const rect = pptModule.RectangleF.FromLTRB(90, 100, 640, 420);
const chart = slide.Shapes.AppendChart({ type: pptModule.ChartType.LineMarkers, rectangle: rect, init: false });
// 设置图表标题
chart.ChartTitle.TextProperties.Text = '各季度销售趋势';
chart.ChartTitle.TextProperties.IsCentered = true;
chart.ChartTitle.Height = 30;
chart.HasTitle = true;
// 准备两个系列的数据
const quarters = ['第一季度', '第二季度', '第三季度', '第四季度'];
const online = [128, 156, 142, 180];
const offline = [96, 88, 110, 124];
// 写入系列名称
chart.ChartData.get_Item({ row: 0, column: 1 }).Text = '线上渠道';
chart.ChartData.get_Item({ row: 0, column: 2 }).Text = '线下渠道';
// 写入分类名称与各分类的数值
for (let i = 0; i < quarters.length; i++) {
chart.ChartData.get_Item({ row: i + 1, column: 0 }).Text = quarters[i];
chart.ChartData.get_Item({ row: i + 1, column: 1 }).NumberValue = online[i];
chart.ChartData.get_Item({ row: i + 1, column: 2 }).NumberValue = offline[i];
}
// 绑定系列名称、分类标签与各系列的数值区域
chart.Series.SeriesLabel = chart.ChartData.get_Item({ name: 'B1', endCellName: 'C1' });
chart.Categories.CategoryLabels = chart.ChartData.get_Item({ name: 'A2', endCellName: 'A5' });
chart.Series.get_Item(0).Values = chart.ChartData.get_Item({ name: 'B2', endCellName: 'B5' });
chart.Series.get_Item(1).Values = chart.ChartData.get_Item({ name: 'C2', endCellName: 'C5' });
// 保存结果文件
const outputFileName = 'LineChart_result.pptx';
ppt.SaveToFile({ file: outputFileName, fileFormat: pptModule.FileFormat.Pptx2013 });
ppt.Dispose();
// 从 VFS 读取生成的文件,触发下载
downloadFile(outputFileName);
};
// 从 VFS 读取生成的文件,触发下载
const downloadFile = (outputFileName) => {
const fileArray = window.dotnetRuntime.Module.FS.readFile(outputFileName);
const blob = new Blob([fileArray], { type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = outputFileName;
a.click();
URL.revokeObjectURL(url);
};
return (
<div style={{ textAlign: 'center', height: '300px' }}>
<h1>在 PowerPoint 中创建图表</h1>
<button onClick={createLineChart}>创建折线图</button>
</div>
);
}
export default App;
创建折线图后的演示文稿:

常见问题
图表标题设置了却不显示
原因:只给 ChartTitle.TextProperties.Text 赋值还不够,图表的标题开关没有打开时,标题不会绘制出来。
解决:在设置文本的同时把 chart.HasTitle 置为 true,标题才会显示在图表上方:
chart.ChartTitle.TextProperties.Text = '各季度销售额';
chart.HasTitle = true;
分类标签或数值显示为空
原因:图表数据写入后,如果没有把分类标签区域与各系列的数值区域绑定到图表,图形就缺少可用的分类和数值。
解决:先用 ChartData.get_Item() 写入单元格,再分别用 Categories.CategoryLabels 与 Series.get_Item(i).Values 绑定对应的区域,二者都用 ChartData.get_Item({ name, endCellName }) 给出起止单元格。
如何创建带数据标记的折线图
原因:折线图有多种类型,普通折线图只连线不标点,需要显式选择带标记的类型。
解决:调用 Shapes.AppendChart() 时把类型设为 ChartType.LineMarkers,每个数值处就会绘制一个数据标记点。
获取免费许可证
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。







