表格中若干行共用同一个标签,或是一行需要一个横跨多列的说明时,合并单元格能让层次更分明;表头或内容调整之后,过大的单元格又要拆回若干小格。逐个单元格手工合并、拆分,在一张表上反复拖动鼠标既费时,也容易把相邻格子牵连进去,处理多份演示文稿时更难保证结果一致。
本文使用 Spire.Presentation for JavaScript 在浏览器中合并与拆分 PowerPoint 表格的单元格。整个处理过程基于 WebAssembly,在用户设备本地完成,无需安装 Office,也不依赖后端服务,演示文稿不必离开浏览器。
本文介绍两个核心功能点:
有关安装和项目配置,请参考 React 项目中集成 Spire.Presentation for JavaScript。以下示例默认 Spire.Presentation for JavaScript 已安装、WebAssembly 模块已初始化。
合并 PowerPoint 表格中的单元格
合并把一个矩形区域内的单元格并成一格:纵向合并可以让一个标签覆盖多行,横向合并可以让一句说明横跨整行。MergeCells 接受区域的两个对角单元格,一次划定要合并的范围;合并后的单元格跨越的行数与列数可以从 RowSpan、ColSpan 读出,用来核对结果是否符合预期。
| 写法 | 得到的结果 |
|---|---|
slide.Shapes.get_Item(index) |
按索引取得幻灯片上的形状 |
shape instanceof pptModule.ITable |
判断形状是否为表格 |
table.get_Item(columnIndex, rowIndex) |
按列、行索引取得单元格 |
table.MergeCells(cell1, cell2, allowSplitting) |
合并由两个单元格划定的区域 |
cell.RowSpan / cell.ColSpan |
合并后单元格跨越的行数与列数 |
cell.FirstRowIndex / cell.FirstColumnIndex |
合并区域左上角单元格的行、列索引 |
具体操作步骤如下:
- 获取 Spire.Presentation 的 WASM 模块并确认已就绪,将演示文稿载入 VFS,创建
Presentation对象并调用LoadFromFile()加载,再用Slides.get_Item(0)取得第一张幻灯片。 - 遍历形状集合,用
shape instanceof pptModule.ITable找到幻灯片上的表格。 - 纵向合并时,把同一列中要合并的首尾两个单元格交给
table.MergeCells(),示例合并第一列的第 2、3 行。 - 横向合并时,把同一行中要合并的首尾两个单元格交给
table.MergeCells(),示例合并最后一行的全部单元格。 - 调用
Presentation.SaveToFile()保存结果文件,并用Dispose()释放演示文稿,随后从 VFS 读出文件并触发下载。
合并 PowerPoint 表格中的单元格的代码如下:
function App() {
const mergeCells = async () => {
// 获取 Spire.Presentation WASM 模块
const pptModule = window.wasmModule?.spirepresentation;
// 检查模块是否就绪
if (!pptModule) {
alert('Spire.Presentation is not ready yet');
return;
}
// 将演示文稿载入 VFS
const inputFileName = 'cn_TableCells.pptx';
await window.spire.FetchFileToVFS(inputFileName, '', `${process.env.PUBLIC_URL}/data/`);
// 加载演示文稿并取得第一张幻灯片
const ppt = new pptModule.Presentation();
ppt.LoadFromFile(inputFileName);
const slide = ppt.Slides.get_Item(0);
// 找到幻灯片上的表格
let table = null;
for (let i = 0; i < slide.Shapes.Count; i++) {
const shape = slide.Shapes.get_Item(i);
if (shape instanceof pptModule.ITable) {
table = shape;
}
}
// 纵向合并:第一列的第 2、3 行
table.MergeCells(table.get_Item(0, 1), table.get_Item(0, 2), false);
// 横向合并:最后一行的全部单元格
table.MergeCells(table.get_Item(0, 4), table.get_Item(3, 4), true);
// 保存结果文件
const outputFileName = 'MergeCells_result.pptx';
ppt.SaveToFile({ file: outputFileName, fileFormat: pptModule.FileFormat.Pptx2013 });
ppt.Dispose();
// 从 VFS 读取生成的文件,触发下载
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={mergeCells}>合并单元格</button>
</div>
);
}
export default App;
合并单元格后的演示文稿:

拆分 PowerPoint 表格中的单元格
拆分把一个单元格所在的区域按指定的行数与列数重新划分,原来的一格变成若干小格。表格随之扩充对应的行与列,与拆分区域相邻的单元格自动跨过新增的分隔线,保持原来的内容;拆分出的新单元格为空白,可以继续写入文字。这样既能把过大的格子还原成细分的栏目,也能在固定版式的表格中腾出新的填写位置。
| 写法 | 得到的结果 |
|---|---|
table.get_Item(columnIndex, rowIndex) |
按列、行索引取得要拆分的单元格 |
cell.Split(rowCount, columnCount) |
把单元格拆分为指定行数与列数 |
cell.RowSpan / cell.ColSpan |
拆分后各单元格的跨度 |
具体操作步骤如下:
- 获取 Spire.Presentation 的 WASM 模块并确认已就绪,将演示文稿载入 VFS,创建
Presentation对象并加载演示文稿,再取得第一张幻灯片。 - 遍历形状集合,用
shape instanceof pptModule.ITable找到表格。 - 用
table.get_Item()取得要拆分的单元格,调用Split()传入行数与列数,示例拆分为 2 行 2 列。 - 调用
Presentation.SaveToFile()保存结果文件,并用Dispose()释放演示文稿,随后从 VFS 读出文件并触发下载。
拆分 PowerPoint 表格中的单元格的代码如下:
function App() {
const splitCell = async () => {
// 获取 Spire.Presentation WASM 模块
const pptModule = window.wasmModule?.spirepresentation;
// 检查模块是否就绪
if (!pptModule) {
alert('Spire.Presentation is not ready yet');
return;
}
// 将演示文稿载入 VFS
const inputFileName = 'cn_TableCells.pptx';
await window.spire.FetchFileToVFS(inputFileName, '', `${process.env.PUBLIC_URL}/data/`);
// 加载演示文稿并取得第一张幻灯片
const ppt = new pptModule.Presentation();
ppt.LoadFromFile(inputFileName);
const slide = ppt.Slides.get_Item(0);
// 找到幻灯片上的表格
let table = null;
for (let i = 0; i < slide.Shapes.Count; i++) {
const shape = slide.Shapes.get_Item(i);
if (shape instanceof pptModule.ITable) {
table = shape;
}
}
// 把第 3 行第 2 列的单元格拆分为 2 行 2 列
table.get_Item(1, 2).Split(2, 2);
// 保存结果文件
const outputFileName = 'SplitCell_result.pptx';
ppt.SaveToFile({ file: outputFileName, fileFormat: pptModule.FileFormat.Pptx2013 });
ppt.Dispose();
// 从 VFS 读取生成的文件,触发下载
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={splitCell}>拆分单元格</button>
</div>
);
}
export default App;
拆分单元格后的演示文稿:

常见问题
合并后的单元格如何还原为多个单元格
原因:合并只改变单元格的跨度,原来几个格的内容都留在合并后的单元格里,需要把跨度重新设回单格。
解决:对合并后的单元格调用 SplitBySpan(),它按当前跨度把区域还原成独立的单元格,表格的行列数不变:
table.get_Item(0, 1).SplitBySpan();
合并单元格后,各个格子里的文字都保留吗
原因:合并后的单元格只保留一段文字,区域内的原有文字需要重新组织。
解决:合并时,区域内各单元格的文字会按段落拼接到合并后的单元格中,空单元格不影响结果。若只希望合并后显示一条标签,可以在合并前清空不需要保留的单元格文字:
table.get_Item(0, 2).TextFrame.Text = '';
table.MergeCells(table.get_Item(0, 1), table.get_Item(0, 2), false);
如何判断某个单元格是否属于已合并的区域
原因:合并区域内的每个位置读出的都是同一个单元格对象,仅凭文字无法判断它是否跨越了多行或多列。
解决:读取 RowSpan 与 ColSpan,任一大于 1 即为合并单元格;FirstRowIndex 与 FirstColumnIndex 给出合并区域左上角的位置:
const cell = table.get_Item(col, row);
if (cell.RowSpan > 1 || cell.ColSpan > 1) {
console.log(cell.FirstRowIndex, cell.FirstColumnIndex);
}
获取免费许可证
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。







