设置Word表格的样式
# 设置Word表格的样式
- 查看本示例演示效果
- 本示例关键代码的编写位置,请参考“开始 - 快速上手”里您所使用的开发语言框架的最简集成代码
注意
本文中展示的代码均为关键代码,复制粘贴到您的项目中,按照实际的情况,例如文档路径,用户名等做适当修改即可使用。
PageOffice不仅可以给Word表格中的单元格赋值文本内容,还可以设置表格的边框样式及字体样式。
注意
Word中的table是要借助数据区域(DataRegion)实现的,要求数据区域完整的包含了整个Table的内容,这样才可以通过数据区域控制和操作table。template1.docx中的表格包含在名称为:PO_regTable的数据区域中。
# 后端代码
PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
WordDocumentWriter doc = new WordDocumentWriter();
//打开数据区域
DataRegionWriter dataRegion = doc.openDataRegion("PO_regTable");
//打开table,openTable(index)方法中的index代表Word文档中table位置的索引,从1开始
WordTableWriter table = dataRegion.openTable(1);
//给table中的单元格赋值, openCellRC(int,int)中的参数分别代表第几行、第几列,从1开始
table.openCellRC(3, 1).setValue("A公司");
table.openCellRC(3, 2).setValue("开发部");
table.openCellRC(3, 3).setValue("李清");
//插入一行,insertRowAfter方法中的参数代表在哪个单元格下面插入一个空行
table.insertRowAfter(table.openCellRC(3, 3));
table.openCellRC(4, 1).setValue("B公司");
table.openCellRC(4, 2).setValue("销售部");
table.openCellRC(4, 3).setValue("张三");
//设置表格行的高度
table.setRowsHeight(30.5f);
//设置表格的边框
WordBorder border = table.getBorder();
// 设置边框的类型
border.setBorderType(WdBorderType.wdFullGrid);//包含内边框
//设置边框的颜色
border.setLineColor(Color.red);
//设置边框的线条样式
border.setLineStyle(WdLineStyle.wdLineStyleDot);
//设置边框的粗细
border.setLineWidth(WdLineWidth.wdLineWidth150pt);
//设置表格内字体样式
WordFont font = dataRegion.getFont();
//设置字体的是否加粗
font.setBold(true);
//设置字体的颜色
font.setColor(Color.blue);
//设置字体是否为斜体
font.setItalic(true);
//设置字体名称
font.setName("宋体");
//设置字体大小
font.setSize(15.5f);
poCtrl.setWriter(doc);
//打开文件
poCtrl.webOpen("D:\\template1.docx", OpenModeType.docNormalEdit, "张三");
PageOfficeNetCore.PageOfficeCtrl poCtrl = new PageOfficeNetCore.PageOfficeCtrl(Request);
PageOfficeNetCore.Word.WordDocumentWriter doc = new PageOfficeNetCore.Word.WordDocumentWriter();
//获取Table所在的数据区域对象
PageOfficeNetCore.Word.DataRegionWriter dataRegion = doc.OpenDataRegion("PO_regTable");
//打开table,OpenTable(index)方法中的index代表Word文档中table位置的索引,从1开始
PageOfficeNetCore.Word.WordTableWriter table = dataRegion.OpenTable(1);
//给table中的单元格赋值, OpenCellRC(行, 列)
table.OpenCellRC(3, 1).Value = "A公司";
table.OpenCellRC(3, 2).Value = "开发部";
table.OpenCellRC(3, 3).Value = "李清";
//插入一空行,InsertRowAfter方法中的参数表示在哪个单元格下面插入一行
table.InsertRowAfter(table.OpenCellRC(3, 3));
table.OpenCellRC(4, 1).Value = "B公司";
table.OpenCellRC(4, 2).Value = "销售部";
table.OpenCellRC(4, 3).Value = "张三";
//设置表格行的高度
table.SetRowsHeight(30.5f);
//设置表格的边框样式
PageOfficeNetCore.Word.WordBorder border = table.Border;
// 设置边框的类型
border.BorderType = PageOfficeNetCore.Word.WdBorderType.wdFullGrid;//包含内边框
//设置边框的颜色
border.LineColor = Color.Red;
//设置边框的线条样式
border.LineStyle = PageOfficeNetCore.Word.WdLineStyle.wdLineStyleDot;
//设置边框的粗细
border.LineWidth = PageOfficeNetCore.Word.WdLineWidth.wdLineWidth150pt;
//设置表格字体样式
PageOfficeNetCore.Word.WordFont font = dataRegion.Font;
//设置字体的是否加粗
font.Bold = true;
//设置字体的颜色
font.Color = Color.Blue;
//设置字体是否为斜体
font.Italic = true;
//设置字体名称
font.Name = "宋体";
//设置字体大小
font.Size = 15.5f;
poCtrl.SetWriter(doc);
//打开Word文档
poCtrl.WebOpen("doc/test.doc", PageOfficeNetCore.OpenModeType.docNormalEdit, "tom");
// Make sure to add code blocks to your code group
# 前端代码
本示例无前端关键代码。
上次更新: 2025/08/07, 13:40:45