程序生成的表格往往只带默认细线,列宽也按内容平均分配,读起来不如手工排版的表格清楚。把手动调整边框、列宽和样式的步骤固定到代码里,导出的文档才能与业务模板保持一致,也省去每次生成后在 Word 中逐张表格返工。
本文用 Spire.Doc for JavaScript 在浏览器中直接设置表格边框、列宽与表格样式。整个过程基于 WebAssembly 在浏览器端完成,无需安装 Office,也不依赖任何后端,文件始终留在用户的设备上。
本文介绍三个核心功能点:
三者的作用范围可以概括为下表。直接格式优先于表格样式,因此三者的设置顺序不同,最终外观也会不同:
| 设置项 | 主要 API | 作用范围 |
|---|---|---|
| 表格边框 | SetBorders / SetBorder / CellFormat.Borders |
表格外框、内部网格线与单条边线 |
| 列宽 | SetColumnWidth / Cell.Width |
各列的宽度与排版 |
| 表格样式 | ApplyStyle + StyleOptions |
底纹、边框与字体的预设组合 |
有关安装和项目配置,请参考 React 项目中集成 Spire.Doc for JavaScript。下面的示例假定 Spire.Doc 已安装、WebAssembly 模块已初始化,示例文档为一份两页的渠道销售报告 TableFormattingReport.docx,三个示例都以其中的第一张表格为处理对象。
设置表格边框
设置表格边框可以使用 Table.SetBorders() 与 Table.SetBorder()。前者一次性写入表格的四边与内部网格线,适合统一外观;后者只处理表格的某一条外边线。线宽以磅为单位,颜色使用 Color 对象:
| 方法 | 作用 |
|---|---|
table.SetBorders(style, width, color) |
同时设置四边与内部网格线 |
table.SetBorder(position, style, width, color, override) |
只设置指定的表格边线 |
cell.CellFormat.Borders.Bottom |
单独控制某个单元格的边线 |
具体操作步骤如下:
- 获取 Spire.Doc 的 WASM 模块,确认已就绪。
- 读入示例文档,创建
Document对象并加载该文件。 - 取得第一节正文中的第一张表格。
- 使用
Table.SetBorders()设置统一网格线,再为表头单元格加粗下边线。 - 使用
Document.SaveToFile()保存结果文件,并从 VFS 读出后触发下载。
设置表格边框的代码如下:
function App() {
const setTableBorders = async () => {
// 获取 Spire.Doc 的 WASM 模块
const docModule = window.wasmModule?.spiredoc;
if (!docModule) {
alert('Spire.Doc is not ready yet');
return;
}
// 读入示例文档
await window.spire.FetchFileToVFS('TableFormattingReport.docx', '', `${process.env.PUBLIC_URL}/data/`);
// 创建 Document 实例并加载文档
const doc = new docModule.Document();
doc.LoadFromFile('TableFormattingReport.docx');
const body = doc.Sections.get_Item(0).Body;
const table = body.Tables.get_Item(0);
const navy = docModule.Color.FromArgb(255, 0, 82, 155);
// 四边与内部网格线:单线、1 磅、深蓝色
table.SetBorders(docModule.BorderStyle.Single, 1, navy);
// 表头下边线单独加粗到 2 磅
const header = table.Rows.get_Item(0);
for (let c = 0; c < header.Cells.Count; c++) {
const border = header.Cells.get_Item(c).CellFormat.Borders.Bottom;
border.BorderType = docModule.BorderStyle.Single;
border.LineWidth = 2;
border.Color = navy;
}
// 保存结果文件
const outputFileName = 'TableFormattingBordered.docx';
doc.SaveToFile({ fileName: outputFileName, fileFormat: docModule.FileFormat.Docx2013 });
doc.Dispose();
// 从 VFS 读取生成的文件,触发下载
const fileArray = window.dotnetRuntime.Module.FS.readFile(outputFileName);
const blob = new Blob([fileArray], {
type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
});
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>设置表格边框</h1>
<button onClick={setTableBorders}>应用</button>
</div>
);
}
export default App;
设置表格边框前后的对比如下。左侧是原始表格,只有表头下方一条细线;右侧补齐全套 1 磅深蓝网格线,并把表头下边线加粗到 2 磅:

只给表头加下边线
SetBorder() 面向的是表格的外边线,把 BorderPositions.Bottom 设为 2 磅,加粗的是整张表格的底边,而不是表头下方那条线。要强调表头与内容的分界,需要逐格设置第一行单元格的下边线:
// 加粗表头行的下边线,表格底边保持不变
const header = table.Rows.get_Item(0);
for (let c = 0; c < header.Cells.Count; c++) {
const border = header.Cells.get_Item(c).CellFormat.Borders.Bottom;
border.BorderType = docModule.BorderStyle.Single;
border.LineWidth = 2;
border.Color = docModule.Color.FromArgb(255, 0, 82, 155);
}
去掉表格左右外框
需要让表格“贴版心”排版时,可以只保留内部竖线,把左右两条外框单独关闭。在 JavaScript 版本中,Table.SetBorder() 传入 BorderStyle.None 不会改写表格边框,可靠的做法是直接修改表格格式中的左右边线:
// 关闭左右外框,其余边线与内部网格线不受影响
table.Format.Borders.Left.BorderType = docModule.BorderStyle.None;
table.Format.Borders.Right.BorderType = docModule.BorderStyle.None;
单元格边框优先于表格边框
单元格可以带有自己的边框,显示时优先于表格级边框。设置 SetBorders() 后若个别单元格仍保留原来的细线,通常是这些单元格此前被单独设置过边框;可以逐格清除单元格边框,再把表格边框重新写入一次。
调整表格列宽
调整表格列宽由 Table.SetColumnWidth() 完成,按列下标设置宽度。方法的第二个参数是宽度值,第三个参数 WidthType 决定宽度的保存方式:
| 枚举值 | 宽度参数的写法 | 说明 |
|---|---|---|
WidthType.Twip |
磅 | 绝对宽度,保存时按缇换算(1 磅 = 20 缇) |
WidthType.Percentage |
百分比 | 相对表格宽度计算,列宽随表格等比缩放 |
WidthType.Auto |
由内容决定 | 交给 Word 自动排版 |
调用前需要先关闭自动适应,否则 Word 会按内容重新分配列宽,把手工设置的值覆盖掉。
具体操作步骤如下:
- 获取 Spire.Doc 的 WASM 模块,确认已就绪。
- 读入示例文档,创建
Document对象并加载该文件。 - 取得目标表格,关闭
Format.AllowAutoFit。 - 使用
Table.SetColumnWidth()逐列设置宽度。 - 保存结果文件,并从 VFS 读出后触发下载。
调整表格列宽的代码如下:
function App() {
const setColumnWidth = async () => {
// 获取 Spire.Doc 的 WASM 模块
const docModule = window.wasmModule?.spiredoc;
if (!docModule) {
alert('Spire.Doc is not ready yet');
return;
}
// 读入示例文档
await window.spire.FetchFileToVFS('TableFormattingReport.docx', '', `${process.env.PUBLIC_URL}/data/`);
// 创建 Document 实例并加载文档
const doc = new docModule.Document();
doc.LoadFromFile('TableFormattingReport.docx');
const body = doc.Sections.get_Item(0).Body;
const table = body.Tables.get_Item(0);
// 关闭自动适应,列宽才不会被内容重新分配
table.Format.AllowAutoFit = false;
// 按磅设置五列的宽度
table.SetColumnWidth(0, 90, docModule.WidthType.Twip);
table.SetColumnWidth(1, 120, docModule.WidthType.Twip);
table.SetColumnWidth(2, 70, docModule.WidthType.Twip);
table.SetColumnWidth(3, 70, docModule.WidthType.Twip);
table.SetColumnWidth(4, 120, docModule.WidthType.Twip);
// 保存结果文件
const outputFileName = 'TableFormattingSized.docx';
doc.SaveToFile({ fileName: outputFileName, fileFormat: docModule.FileFormat.Docx2013 });
doc.Dispose();
// 从 VFS 读取生成的文件,触发下载
const fileArray = window.dotnetRuntime.Module.FS.readFile(outputFileName);
const blob = new Blob([fileArray], {
type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
});
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>调整表格列宽</h1>
<button onClick={setColumnWidth}>应用</button>
</div>
);
}
export default App;
调整表格列宽前后的对比如下。左侧是原始的按内容分配,右侧关闭自动适应后重新分配五列,第一列加宽,末列收窄:

设置后列宽没有变化
最常见的原因是表格仍处于自动适应状态。Format.AllowAutoFit 为 true 时,Word 会按内容重新分配列宽,覆盖手工设置的值,因此要先关闭自动适应,再设置列宽。另一个原因是各列宽度之和超过表格可用宽度,Word 会等比压缩所有列,使结果与设定值不一致。
按百分比还是按磅
百分比列宽相对表格宽度计算,适合需要随版心等比缩放的场景;磅是绝对宽度,适合与固定版心对齐。使用百分比时,表格本身需要先有确定宽度,否则 Word 可能把整个表格压缩得很窄。同一张表格内不建议混用两种单位:表格宽度变化后,按磅设置的列不变,按百分比设置的列会变,比例关系随之失衡。
逐格设置单元格宽度
除了按列设置,也可以给每个单元格单独指定宽度。Cell.Width 的单位是磅,适合列内需要不同宽度的版式:
// 关闭自动适应后,逐格指定宽度(单位:磅)
table.Format.AllowAutoFit = false;
for (let r = 0; r < table.Rows.Count; r++) {
const row = table.Rows.get_Item(r);
row.Cells.get_Item(0).Width = 90;
row.Cells.get_Item(4).Width = 120;
}
逐格设置与按列设置可以配合使用:先用 SetColumnWidth() 确定整体比例,再对个别单元格用 Cell.Width 微调。
套用表格样式
套用表格样式使用 Table.ApplyStyle() 与 Table.Format.StyleName。内置样式把边框、底纹和字体颜色打包为一套预设,StyleOptions 决定预设中的首行强调、隔行底纹等选项是否生效:
| 成员 | 说明 |
|---|---|
table.ApplyStyle({ builtinTableStyle }) |
套用内置表格样式,需使用对象写法 |
table.Format.StyleOptions |
组合 FirstRow、RowStripe 等选项 |
table.Format.StyleName |
读取当前样式的名称 |
table.Format.ClearFormatting() |
清除直接格式,让样式能够生效 |
具体操作步骤如下:
- 获取 Spire.Doc 的 WASM 模块,确认已就绪。
- 读入示例文档,创建
Document对象并加载该文件。 - 取得目标表格,清除表格上的直接格式。
- 使用
Table.ApplyStyle()套用目标样式,再设置StyleOptions。 - 保存结果文件,并从 VFS 读出后触发下载。
套用表格样式的代码如下:
function App() {
const applyTableStyle = async () => {
// 获取 Spire.Doc 的 WASM 模块
const docModule = window.wasmModule?.spiredoc;
if (!docModule) {
alert('Spire.Doc is not ready yet');
return;
}
// 读入示例文档
await window.spire.FetchFileToVFS('MSYH.TTC', '/Library/Fonts/', `${process.env.PUBLIC_URL}/font/`);
await window.spire.FetchFileToVFS('TableFormattingReport.docx', '', `${process.env.PUBLIC_URL}/data/`);
// 创建 Document 实例并加载文档
const doc = new docModule.Document();
doc.LoadFromFile('TableFormattingReport.docx');
const body = doc.Sections.get_Item(0).Body;
const table = body.Tables.get_Item(0);
// 清除手工设置过的底纹与边框,避免直接格式覆盖样式
table.Format.ClearFormatting();
// 套用内置样式:用对象写法指定样式来源
table.ApplyStyle({
builtinTableStyle: docModule.DefaultTableStyle.LightShadingAccent1,
});
// 启用首行、首列与隔行底纹
table.Format.StyleOptions = docModule.TableStyleOptions.Default;
// 保存结果文件
const outputFileName = 'TableFormattingStyled.docx';
doc.SaveToFile({ fileName: outputFileName, fileFormat: docModule.FileFormat.Docx2013 });
doc.Dispose();
// 从 VFS 读取生成的文件,触发下载
const fileArray = window.dotnetRuntime.Module.FS.readFile(outputFileName);
const blob = new Blob([fileArray], {
type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
});
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>套用表格样式</h1>
<button onClick={applyTableStyle}>应用</button>
</div>
);
}
export default App;
套用表格样式前后的对比如下。左侧是清除直接格式前的原始表格,右侧套用 LightShadingAccent1 后出现首行强调与隔行底纹:

样式套用后没有任何变化
样式不生效通常有两个原因。第一,表格上存在直接设置的底纹或边框,直接格式优先于样式,需要先用 Format.ClearFormatting() 清除,或逐项清除对应格式。第二,ApplyStyle() 直接接收枚举会触发重载歧义,应改用 { builtinTableStyle: ... } 的对象写法。此外,所选样式与 StyleOptions 的组合决定了首行、隔行底纹是否出现,套用后可以先检查 Format.StyleName 是否已切换为目标样式。
样式与自定义边框的取舍
内置样式便于统一风格,但可调整的空间有限。正式报表通常采用“样式打底、边框微调”的做法:先套用样式获得底纹与字体,再用 SetBorders() 或 SetBorder() 调整关键分隔线。这样样式负责整体观感,边框只覆盖需要强调的线条,调整范围更小,也便于后续统一修改。
三个功能点的设置顺序
| 步骤 | 调用 | 目的 |
|---|---|---|
| 套用样式 | ApplyStyle + StyleOptions |
底纹、字体与整体风格 |
| 固定列宽 | AllowAutoFit = false + SetColumnWidth |
保证版心对齐 |
| 调整边框 | SetBorders / SetBorder |
强调分隔线 |
通常按先样式、后列宽、再边框的顺序设置,覆盖关系最简单。若表格由程序新建,边框与列宽宜在填充数据之后设置,因为写入内容可能触发表格重新布局。
常见问题
设置表格边框后,单元格之间仍有原来的细线
原因:SetBorders() 设置的是表格级边框,单元格自身带有的边框会优先显示。示例文档中的表格如果此前被单独设置过单元格边框,就会保留原来的线条。
解决:逐格清除单元格边框,再重新写入表格边框;也可以直接设置目标单元格的 CellFormat.Borders,让单元格边框与表格边框保持一致。
为什么调整列宽后表格没有任何变化
原因:表格仍处于自动适应状态,或各列宽度之和超过了表格可用宽度。前者会让 Word 按内容重新分配列宽,后者会触发等比压缩。
解决:先设置 table.Format.AllowAutoFit = false,再调用 SetColumnWidth();需要精确控制时,改用逐格设置 Cell.Width,并确认总宽度不超过版心。
套用内置样式后,表格外观和预期不一致
原因:表格上存在直接设置的底纹、边框或单元格宽度,直接格式优先于样式,导致样式中的首行强调与隔行底纹没有显示出来。
解决:先用 Format.ClearFormatting() 清除直接格式,再用 { builtinTableStyle: ... } 的对象写法套用样式,并设置 StyleOptions = TableStyleOptions.Default;套用后可以通过 Format.StyleName 确认样式名称已经切换。
可以只给表头或某一行加边框吗
可以。SetBorder() 面向整张表格的外边线,而表头或某一行的边框属于单元格级设置。遍历目标行的单元格,修改 CellFormat.Borders 中对应的边线即可,例如只加粗表头行的下边线。
设置边框、列宽和样式会修改表格里的文字吗
这些方法只修改排版属性。本文涉及的 SetBorders、SetColumnWidth、ApplyStyle 处理的是边框、列宽与样式,不涉及单元格中的文字内容、段落结构或数据顺序。
获取免费许可证
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。







