Word控件Spire.Doc 【Table】教程(5):创建表格并在 Doc 文档中设置其边框

Spire.Doc for .NET是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。

Spire.Doc for.NET 最新下载(qun:767755948)icon-default.png?t=MBR7https://www.evget.com/product/3368/download

如果您正在处理提案请求、标准操作程序文件或规定格式的其他文档类型,则 Word 文档可能是一页又一页的纯文本。你的文件会很麻烦而且不清楚。所以你的文件需要一个表格来记录你需要显示的信息。表格不仅提供了信息的可视化分组,也使编写者更方便地修改和查询表格中的数据。特别是当您的表格有丰富多彩的边框时,您的文档会令人惊叹。

Spire.Doc for .NET,专业的.NET Word组件,操作Word文档,提供了一个Table类来对表格执行任务,例如,包括行、列、单元格、边框和表格布局操作。本指南介绍了一种使用数据创建 Word 表格并通过 Spire.Doc for .NET 在 C#.NET 中设置其边框的解决方案。要完成本指南,您应该首先下载并安装 Spire.Doc for .NET。

下面将通过一个具体的例子来逐步创建Word表格并设置其边框。下面的屏幕截图显示了此示例的结果。

我们假设您在本示例之前已经完成教程 Spire.Doc 快速入门。

  • 首先,创建一个新的 Document 实例。调用 AddTable 方法在 Section 中创建一个表,传递一个参数值 true 以指示新表具有边框。调用 ResetCells 方法设置表格的行数和列数。
  • 其次,设置表格的标题和单元格格式。
  • 第三,设置表格的边框格式。int 这一步,你可以设置底部、顶部、左、右边框的格式。比如你可以设置Color,LineWidth,border是否存在,BorderType,你可以选择Dot,Double,Hairline等多种类型。
  • 最后,您可以设置垂直和水平边框,如步骤 3。

完整代码

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace Table
{
Public class table
{
static void Main(string[] args)
{
//Open a blank word document as template
Document document = new Document(Blank.doc");
addTable(document.Sections[0]);

//Save doc file.
document.SaveToFile("Sample.doc",FileFormat.Doc);

//Launching the MS Word file.
System.Diagnostics.Process.Start ("Sample.doc");
}
private void addTable(Section section)
{
//create a table with border
Spire.Doc.Table table = section.AddTable(true);
String[] header = { "Name", "Capital", "Continent", "Area", "Population" };
String[][] data =
{ new String[]{"Argentina", "Buenos Aires", "South America", "2777815", "32300003"},
new String[]{"Bolivia", "La Paz", "South America", "1098575", "7300000"},
new String[]{"Brazil", "Brasilia", "South America", "8511196", "150400000"},
new String[]{"Canada", "Ottawa", "North America", "9976147", "26500000"},
new String[]{"Paraguay", "Asuncion", "South America", "406576", "4660000"},
new String[]{"Peru", "Lima", "South America", "1285215", "21600000"},
new String[]{"United States of America", "Washington", "North America", "9363130", "249200000"},
};

table.ResetCells(data.Length + 1, header.Length);

// ***************** First Row *************************
TableRow Frow = table.Rows[0];
Frow.IsHeader = true;
Frow.Height = 20; //unit: point, 1point = 0.3528 mm
Frow.HeightType = TableRowHeightType.Exactly;
Frow.RowFormat.BackColor = Color.Pink;
for (int i = 0; i < header.Length; i++)
{
Frow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
Paragraph p = Frow.Cells[i].AddParagraph();
p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Left;
TextRange txtRange = p.AppendText(header[i]);
txtRange.CharacterFormat.Bold = true;
}
for (int r = 0; r < data.Length; r++)
{
TableRow dataRow = table.Rows[r + 1];
dataRow.Height = 20;
dataRow.HeightType = TableRowHeightType.Exactly;
dataRow.RowFormat.BackColor = Color.Empty;
for (int c = 0; c < data[r].Length; c++)
{
dataRow.Cells[c].CellFormat.VerticalAlignment =VerticalAlignment.Middle;
dataRow.Cells[c].AddParagraph().AppendText(data[r][c]);
}
}

//set right border of table
table.TableFormat.Borders.Right.BorderType = Spire.Doc.Documents.BorderStyle.Hairline;
table.TableFormat.Borders.Right. LineWidth = 2.0F;
table.TableFormat.Borders.Right.Color = Color.GreenYellow;
//set top border of table
table.TableFormat.Borders.Top.BorderType = Spire.Doc.Documents.BorderStyle.Hairline;
table.TableFormat.Borders.Top.LineWidth = 4.0F;
table.TableFormat.Borders.Top.Color = Color.GreenYellow;
//set left border of table
table.TableFormat.Borders.Left.BorderType = Spire.Doc.Documents.BorderStyle.Hairline;
table.TableFormat.Borders.Left.LineWidth = 2.0F;
table.TableFormat.Borders.Left.Color = Color.GreenYellow;
//set bottom border is none
table.TableFormat.Borders.Bottom.BorderType = Spire.Doc.Documents.BorderStyle.None;
//set vertical and horizontal border
table.TableFormat.Borders.Vertical.BorderType = Spire.Doc.Documents.BorderStyle.Dot;
table.TableFormat.Borders.Horizontal.BorderType = Spire.Doc.Documents.BorderStyle.None;
table.TableFormat.Borders.Vertical.Color = Color.Orange;

}
}
}

以上便是如何在C#、VB.NET中设置Word表格样式,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用-iceblue:spire.doc.free:5.2.0获取指定内容在doc文档的第几页,可以按照以下步骤进行操作: 1. 打开需要处理的doc文件 ```java Document document = new Document(); document.loadFromFile("test.doc"); ``` 2. 获取doc文件的所有段落 ```java SectionCollection sections = document.getSections(); ParagraphCollection paragraphs = sections.get(0).getParagraphs(); ``` 3. 遍历所有段落,查找需要查找的内容所在的段落,记录下该段落的索引 ```java int pageIndex = -1; for (int i = 0; i < paragraphs.getCount(); i++) { Paragraph paragraph = paragraphs.get(i); String text = paragraph.getText(); if (text.contains("需要查找的内容")) { pageIndex = i; break; } } ``` 4. 如果找到了需要查找的内容所在的段落,则计算该段落所在的页面数 ```java if (pageIndex != -1) { DocumentObject obj = paragraphs.get(pageIndex); int page = document.getPageNumber(obj); System.out.println("需要查找的内容所在的页数为:" + page); } ``` 完整的代码如下: ```java import com.spire.doc.*; public class GetPageIndex { public static void main(String[] args) { //加载文档 Document document = new Document(); document.loadFromFile("test.doc"); //获取第一个节的所有段落 SectionCollection sections = document.getSections(); ParagraphCollection paragraphs = sections.get(0).getParagraphs(); //查找内容所在的段落 int pageIndex = -1; for (int i = 0; i < paragraphs.getCount(); i++) { Paragraph paragraph = paragraphs.get(i); String text = paragraph.getText(); if (text.contains("需要查找的内容")) { pageIndex = i; break; } } //计算内容所在的页数 if (pageIndex != -1) { DocumentObject obj = paragraphs.get(pageIndex); int page = document.getPageNumber(obj); System.out.println("需要查找的内容所在的页数为:" + page); } } } ``` 注意:以上代码使用的是Spire.Doc Free版本,如果使用的是Spire.Doc付费版本,则需要引入对应的jar包,并且需要授权才能使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值