網站首頁 編程語言 正文
我們在使用Word制作表格時,由于表格較為復雜,只是簡單的插入行、列并不能滿足我們的需要。要做一個完整的表格,很多時候需要將單元格進行拆分或者合并,才能達到我們想要的效果。那么具體要如何操作呢?別擔心,本文將詳細為您介紹在Word表格中拆分或合并單元格的思路及方法。
- 在Word表格中合并單元格
- 在Word表格中拆分單元格
程序環境
本次測試時,在程序中引入Free Spire.Doc for .NET。可通過以下方法引用 Free Spire.Doc.dll文件:
方法1:將?Free Spire.Doc for .NET下載到本地,解壓,安裝。安裝完成后,找到安裝路徑下BIN文件夾中的 Spire.Doc.dll。然后在Visual Studio中打開“解決方案資源管理器”,鼠標右鍵點擊“引用”,“添加引用”,將本地路徑BIN文件夾下的dll文件添加引用至程序。
方法2:通過NuGet安裝。可通過以下2種方法安裝:
(1)可以在Visual Studio中打開“解決方案資源管理器”,鼠標右鍵點擊“引用”,“管理NuGet包”,然后搜索“Free Spire.Doc”,點擊“安裝”。等待程序安裝完成。
(2)將以下內容復制到PM控制臺安裝。
Install-Package FreeSpire.Doc -Version 10.8.0
在Word表格中合并單元格
合并單元格,指的是將兩個或多個位于同一行或者同一列的單元格合并成一個單元格。具體步驟如下:
- 初始化 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)
{
//初始化 Document類的實例
Document document = new Document();
//加載Word文檔
document.LoadFromFile("測試文檔.docx");
//獲取特定節
Section section = document.Sections[0];
//添加一個 4 x 4 表格到該節
Table table = section.AddTable();
table.ResetCells(4, 4);
//水平合并表中的特定單元格
table.ApplyHorizontalMerge(0, 0, 3);
//垂直合并表中的特定單元格
table.ApplyVerticalMerge(0, 2, 3);
//將數據添加到表格中
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 = "文本";
}
}
//將樣式應用于表
table.ApplyStyle(DefaultTableStyle.LightGridAccent1);
//保存結果文檔
document.SaveToFile("合并單元格.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())
'初始化 Document類的實例
Dim document As Document = New Document()
'加載Word文檔
document.LoadFromFile("測試文檔.docx")
'獲取特定節
Dim section As Section = document.Sections(0)
'添加一個 4 x 4 表格到該節
Dim table As Table = section.AddTable()
table.ResetCells(4, 4)
'水平合并表中的特定單元格
table.ApplyHorizontalMerge(0, 0, 3)
'垂直合并表中的特定單元格
table.ApplyVerticalMerge(0, 2, 3)
'將數據添加到表格中
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 = "文本"
Next
Next
'將樣式應用于表
table.ApplyStyle(DefaultTableStyle.LightGridAccent1)
'保存結果文檔
document.SaveToFile("合并單元格.docx", FileFormat.Docx2013)
End Sub
End Class
End Namespace
效果圖
在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)
{
//初始化Document類的實例
Document document = new Document();
//加載Word文檔
document.LoadFromFile("合并單元格.docx");
//獲取文檔中的特定節
Section section = document.Sections[0];
//在該節獲取特定表格
Table table = section.Tables[0] as Table;
//獲取要拆分的表格單元格
TableCell cell1 = table.Rows[3].Cells[3];
//將單元格分為特定數量的列和行
cell1.SplitCell(2, 2);
//保存結果文檔
document.SaveToFile("拆分單元格.docx", FileFormat.Docx2013);
}
}
}
VB.NET
Imports Spire.Doc
Namespace SplitTableCells
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'初始化Document類的實例
Dim document As Document = New Document()
'加載Word文檔
document.LoadFromFile("合并單元格.docx")
'獲取文檔中的特定節
Dim section As Section = document.Sections(0)
'在該節獲取特定表格
Dim table As Table = TryCast(section.Tables(0), Table)
'獲取要拆分的表格單元格
Dim cell1 As TableCell = table.Rows(3).Cells(3)
'將單元格分為特定數量的列和行
cell1.SplitCell(2, 2)
'保存結果文檔
document.SaveToFile("拆分單元格.docx", FileFormat.Docx2013)
End Sub
End Class
End Namespace
效果圖
原文鏈接:https://www.cnblogs.com/Carina-baby/p/16998042.html
相關推薦
- 2023-06-04 Kotlin比較與解釋Lazy與Lateinit的用法_Android
- 2022-03-23 如何使用Redis實現電商系統的庫存扣減_Redis
- 2022-10-16 Python計算標準差之numpy.std和torch.std的區別_python
- 2022-10-31 Python實現將DNA序列存儲為tfr文件并讀取流程介紹_python
- 2023-02-15 Python函數常見幾種return返回值類型_python
- 2023-02-15 用Python編寫個解釋器實現方法接受_python
- 2024-01-16 linux查看外網ip的5種方法
- 2022-07-25 Android開發之Fragment懶加載的幾種方式及性能對比_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支