Word控件Spire.Doc 【Table】教程(16):C#/VB.NET:在 Word 表格中插入或提取图像

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

E-iceblue 功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式

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

Microsoft Word 中的表格可以包含多种元素,包括文本、图像等等。有时,您可能希望将图像插入表格中以显示一些信息或从表格中提取图像以用于其他文档。本文将教您如何使用 Spire.Doc for .NET在 C# 和 VB.NET 中的 Word 文档中的表格中插入或提取图像。

安装适用于 .NET 的 Spire.Doc

PM> Install-Package Spire.Doc

在 C# 和 VB.NET 中将图像插入到 Word 文档的表格中

在 Microsoft Word 中,您可以将两个或多个相邻的单元格水平或垂直合并为一个更大的单元格。在 Spire.Doc 中,您可以使用Table.ApplyHorizontalMerge()Table.ApplyVerticalMerge()方法实现相同的目的。以下是详细步骤:

  • 初始化Document类的一个实例。
  • 使用Document.LoadFromFile()方法加载 Word 文档。
  • 通过Document.Sections[int]属性通过索引获取文档中的特定部分。
  • 使用Section.AddTable()方法将表格添加到该部分。
  • 使用Table.ResetCells()方法指定表格的行数和列数。
  • 使用Table.ApplyHorizontalMerge()方法水平合并表格中的特定单元格。
  • 使用Table.ApplyVerticalMerge()方法垂直合并表格中的特定单元格。
  • 向表中添加一些数据。
  • 将样式应用于表格。
  • 使用Document.SaveToFile()方法保存结果文档。

[ C# ]

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

namespace MergeTableCells
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document document = new Document();
//Load a Word document
document.LoadFromFile("Input.docx");

//Get the first section
Section section = document.Sections[0];

//Add a 4 x 4 table to the section
Table table = section.AddTable();
table.ResetCells(4, 4);

//Horizontally merge cells 1, 2, 3, and 4 in the first row
table.ApplyHorizontalMerge(0, 0, 3);
//Vertically merge cells 3 and 4 in the first column
table.ApplyVerticalMerge(0, 2, 3);

//Add some data to the table
for (int row = 0; row < table.Rows.Count; row++)
{
for (int col = 0; col < table.Rows[row].Cells.Count; col++)
{
TableCell cell = table[row, col];
cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle;
Paragraph paragraph = cell.AddParagraph();
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center;
paragraph.Text = "Text";
}
}

//Apply a style to the table
table.ApplyStyle(DefaultTableStyle.LightGridAccent1);

//Save the result document
document.SaveToFile("MergeCells.docx", FileFormat.Docx2013);
}
}
}

【VB.NET】

Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace MergeTableCells
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a Document instance
Dim document As Document = New Document()
'Load a Word document
document.LoadFromFile("Input.docx")

'Get the first section
Dim section As Section = document.Sections(0)

'Add a 4 x 4 table to the section
Dim table As Table = section.AddTable()
table.ResetCells(4, 4)

'Horizontally merge cells 1, 2, 3, and 4 in the first row
table.ApplyHorizontalMerge(0, 0, 3)
'Vertically merge cells 3 and 4 in the first column
table.ApplyVerticalMerge(0, 2, 3)

'Add some data to the table
For row As Integer = 0 To table.Rows.Count - 1
For col As Integer = 0 To table.Rows(row).Cells.Count - 1
Dim cell As TableCell = table(row, col)
cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle
Dim paragraph As Paragraph = cell.AddParagraph()
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center
paragraph.Text = "Text"
Next
Next

'Apply a style to the table
table.ApplyStyle(DefaultTableStyle.LightGridAccent1)

'Save the result document
document.SaveToFile("MergeCells.docx", FileFormat.Docx2013)
End Sub
End Class
End Namespace

使用 C# 和 VB.NET 在 Word 中拆分表格单元格

Spire.Doc for .NET 提供了TableCell.SplitCell()方法,使您能够将 Word 表格中的一个单元格拆分为两个或多个单元格。以下是详细步骤:

  • 初始化Document类的一个实例。
  • 使用Document.LoadFromFile()方法加载 Word 文档。
  • 通过Document.Sections[int]属性通过索引获取文档中的特定部分。
  • 通过Section.Tables[int]属性根据其索引获取节中的特定表。
  • 通过Table.Rows[int].Cells[int]属性获取要拆分的表格单元格。
  • 使用TableCell.SplitCell()方法将单元格拆分为特定数量的列和行。
  • 使用Document.SaveToFile()方法保存结果文档。

[C#]

using Spire.Doc;

namespace SplitTableCells
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document document = new Document();
//Load a Word Document
document.LoadFromFile("MergeCells.docx");

//Get the first section
Section section = document.Sections[0];

//Get the first table in the section
Table table = section.Tables[0] as Table;

//Get the 4th cell in the 4th row
TableCell cell1 = table.Rows[3].Cells[3];
//Split the cell into 2 columns and 2 rows
cell1.SplitCell(2, 2);

//save the result document
document.SaveToFile("SplitCells.docx", FileFormat.Docx2013);
}
}
}

【VB.NET】

Imports Spire.Doc

Namespace SplitTableCells
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a Document instance
Dim document As Document = New Document()
'Load a Word Document
document.LoadFromFile("MergeCells.docx")

'Get the first section
Dim section As Section = document.Sections(0)

'Get the first table in the section
Dim table As Table = TryCast(section.Tables(0), Table)

'Get the 4th cell in the 4th row
Dim cell1 As TableCell = table.Rows(3).Cells(3)
'Split the cell into 2 columns and 2 rows
cell1.SplitCell(2, 2)

'save the result document
document.SaveToFile("SplitCells.docx", FileFormat.Docx2013)
End Sub
End Class
End Namespace

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
表格控件总览:   在VB开发环境表格控件在界面开发元素占有重要的地位。它不仅有外观整洁、表达形式规范的优点,而且更重要的是它较高的信息表现率(就是相对于其他控件来说能够表达更多的信息),随着信息时代的到来,它的应用将更加的广泛。    那么在VB平台下,如何操作这一功能强大的控件元素呢?事实上我们知道,VB平台下面的表格控件是相当丰富的,总结下来提供了4种类型:Microsoft Data Bound grid Control、Microsoft Datagrid Control、Microsoft Flexgrid Control、Microsoft Hierarchial Flexgrid Control. 这四种表格各有其特点,下面我们一一讨论。   1. Microsoft Data Bound grid Control   此控件主要用于数据绑定(可以不绑定,但是因为不提供对单个cell的控制支持,所以实际上这样做是不经济的)。具体说来,就是数据源比较固定的情况下可以使用这种控件。   具体使用方法是设定此控件的datasource属性,就可以不用编写任何代码就可以显示该数据源所指向的记录数据。比如可以将datasource设置为一个data控件,而data控件又指向数据库"成绩"的 一个"语文"表,那么当程序运行时,它就自动显示此"语文"表的数据。 基本步骤如下: 1、向工程上添加Microsoft Data Bound Grid Control控件,即DBGrid控件。 2、在窗体上添加DBGrid控件DBGrid1和Data控件Data1。 3、设置Data1的DatabaseName属性为你要看的数据库的名字。 4、设置Data1的RecordSource属性为表的名字或合法的SQL Select语句。 5、设置DBGrid1的DataSource属性为Data1。   从上面的分析看得出来,这个控件虽然操作方便,但是它也有局限性,就是不能对显示的数据进行灵活的控制操作,比如有些数据允许用户修改,有些数据却不允许用户修改,而此控件却只能提供全部数据源的原子操作:即要么全部允许修改,要么就全部不允许。还有另外此控件也不提供对单个单元格(cell)进行操作,所以提供不了更为个性化的界面风格。   2. Microsoft Datagrid Control   此控件跟前面介绍的Data Bound grid Control控件很相似,也是主要进行绑定操作,而缺点和Data Bound grid Control完全一样。但是它跟Data Bound grid Control不一样的是其数据源的驱动引擎只能是OLEDB形式的,不能是DAO,比如就可以使用ADO数据控件,不能使用DATA控件。而Data Bound grid Control却刚好和它相反,这一点在开发一定要注意。   3. Microsoft Flexgrid Control与Microsoft Hierarchial Flexgrid Control.   这里重点介绍这两种控件,因为在实际开发,这两种控件应用的场合更多一些,它不仅能够反映数据,而且也能把数据的修改信息反映到数据库去,所以弥补了上述两种控件的不足。   如果数据不需要修改,那么可以进行绑定操作,其方法跟前面介绍的完全一样,就是通过设置DataSource属性来完成数据的显示工作。但是实际开发,需要对整个表格控件更为灵活的显示控制。   在给出例程之前,有必要对这一控件进行比较详尽的认识:这种控件允许将文本或者图象放置于每个单元格之控件的Row与Col属性允许用户在代码指定当前行和列,当然也可通过操作鼠标和键盘来改变这两个属性,而text属性指明当前单元格的文本。如果单元格的文本太长而不能完全显示出来的话,可以通过将WordWrap属性设置为true来达到显示的目的。下面将比较重要的属性小结如下:   DataSource---------用来指定需要绑定的数据源,比如data控件。   Cellpicture----------用来设定当前单元格的图象,便于显示该图象。此属性不能在设计时使用。   Col,Row---------------设定当前列和当前行,注意它们均是从0开始的,如果同时设定它们,可以指定当前的单元格。设计时也不能使用。   Cols,Rows---------------设置表格控件总的列数和行数。   Hwnd---------------表格句柄,可以结合Windows API对表格控件进行更高级的控制。   Text---------------指定当前的单元格的文本内容。   TextMatrix(i,j)-------此属性比较重要,它用来指定第I行和第j列所确定的单元格的文本内容。它等价于下面的代码: MSHFlexGrid1.Rows =i MSHFlexGrid1.Cols =j MSHFlexGrid1.Text =指定的字符串   WordWrap-------为ture时可以在当前单元格换行显示,否则如果要显示的字符的长度超过列宽,那么就不能显示完全。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值