在日常工作和学习中,我们经常需要处理包含各种表格数据的 Word 文档。这些表格可能包含重要的统计数据、财务信息或其他关键内容。有时我们需要从 Word 文档中提取这些表格,以便进行进一步的数据分析、整理或与他人共享。在这篇文章中,我们将介绍如何使用 Spire.Doc for .NET 和 C# 从 Word 文档中提取表格。
安装 Spire.Doc for .NET
首先,您需要将 Spire.Doc for.NET 包含的 DLL 文件作为引用添加到您的 .NET 项目中。DLL 文件可以从此链接下载,也可以通过 NuGet 安装。
PM> Install-Package Spire.DocC# 从 Word 文档中提取表格
在 Spire.Doc for .NET 中,Section.Tables 属性用于访问 Word 文档特定节中包含的表格。该属性返回一个 ITable 对象的集合,每个对象代表该节中的一个独立表格。获取 ITable 对象后,就可以遍历它们的行和单元格,并使用 cell.Paragraphs[index].Text 属性来提取每个单元格的文本内容。
从 Word 文档中提取表格的详细步骤如下:
- 创建 Document 类的对象,并使用 Document.LoadFromFile() 方法加载 Word 文档。
- 遍历文档中的各个节,通过 Section.Tables 属性获取每个节中的表格集合。
- 遍历每个节中的表格,为每个表格创建一个 string 对象。
- 遍历每个表格的行和行中的单元格,然后通过 TableCell.Paragraphs[index].Text 属性获取每个单元格的文本,并将其添加到 string 对象中。
- 将每个 string 对象保存为一个文本文件。
- C#
using Spire.Doc;
using Spire.Doc.Collections;
using Spire.Doc.Interface;
using System.IO;
using System.Text;
namespace ExtractWordTable
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 创建 Document 类的对象
            Document doc = new Document();
            // 加载 Word 文档
            doc.LoadFromFile("表格.docx");
            // 遍历文档中的各个节
            for (int sectionIndex = 0; sectionIndex < doc.Sections.Count; sectionIndex++)
            {
                // 获取当前节
                Section section = doc.Sections[sectionIndex];
                // 获取当前节中的表格集合
                TableCollection tables = section.Tables;
                // 遍历节中的各个表格
                for (int tableIndex = 0; tableIndex < tables.Count; tableIndex++)
                {
                    // 获取当前表格
                    ITable table = tables[tableIndex];
                    // 初始化一个string对象来存储表格数据
                    string tableData = "";
                    // 遍历表格中的行
                    for (int rowIndex = 0; rowIndex < table.Rows.Count; rowIndex++)
                    {
                        // 获取当前行
                        TableRow row = table.Rows[rowIndex];
                        // 遍历行中的单元格
                        for (int cellIndex = 0; cellIndex < row.Cells.Count; cellIndex++)
                        {
                            // 获取当前单元格
                            TableCell cell = table.Rows[rowIndex].Cells[cellIndex];
                            // 获取单元格中的文本
                            string cellText = "";
                            for (int paraIndex = 0; paraIndex < cell.Paragraphs.Count; paraIndex++)
                            {
                                cellText += (cell.Paragraphs[paraIndex].Text.Trim() + " ");
                            }
                            // 将文本添加到string对象中
                            tableData += cellText.Trim();
                            if (cellIndex < table.Rows[rowIndex].Cells.Count - 1)
                            {
                                tableData += "\t";
                            }
                        }
                        // 添加一个新行
                        tableData += "\n";
                    }
                    // 将表格数据保存到文本文件中
                    string filePath = Path.Combine("表格", $"Section{sectionIndex + 1}_Table{tableIndex + 1}.txt");
                    File.WriteAllText(filePath, tableData, Encoding.UTF8);
                }
            }
            doc.Close();
        }
    }
}
C# 从 Word 文档中提取表格并保存到 Excel 表格
除了将提取的表格数据保存到文本文件之外,你还可以使用 Spire.XLS for .NET 库直接将数据写入 Excel 工作表。但在使用 Spire.XLS 之前,你需要通过 NuGet 安装它:
Install-Package Spire.XLS从 Word 文档提取表格数据并保存到 Excel 工作表的详细步骤如下:
- 创建 Document 类的对象,并使用 Document.LoadFromFile() 方法加载 Word 文档。
- 创建 Workbook 类的对象,并使用 Workbook.Worksheets.Clear() 方法清除默认的工作表。
- 遍历文档中的各个节,通过 Section.Tables 属性获取每个节中的表格集合。
- 遍历每个节中的表格,并使用 Workbook.Worksheets.Add() 方法为每个表格添加一个工作表。
- 遍历每个表格的行和行中的单元格,然后通过 TableCell.Paragraphs[index].Text 属性获取每个单元格的文本,并使用 Worksheet.SetCellValue() 方法将其写入工作表。
- 使用 Workbook.SaveToFile() 方法将结果文件保存为一个 Excel 文件。
- C#
using Spire.Doc;
using Spire.Doc.Interface;
using Spire.Xls;
namespace ExtractWordTableToExcel
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 创建 Document 类的对象
            Document doc = new Document();
            // 加载 Word 文档
            doc.LoadFromFile("表格.docx");
            // 创建 Workbook 类的对象
            Workbook wb = new Workbook();
            // 删除默认工作表
            wb.Worksheets.Clear();
            // 遍历文档中的各个节
            for (int sectionIndex = 0; sectionIndex < doc.Sections.Count; sectionIndex++)
            {
                // 获取当前节
                Section section = doc.Sections[sectionIndex];
                // 遍历节中的各个表格
                for (int tableIndex = 0; tableIndex < section.Tables.Count; tableIndex++)
                {
                    // 获取当前表格
                    ITable table = section.Tables[tableIndex];
                    // 添加一个工作表到工作簿中
                    Worksheet ws = wb.Worksheets.Add($"Section{sectionIndex + 1}_Table{tableIndex + 1}");
                    // 遍历表格中的行
                    for (int rowIndex = 0; rowIndex < table.Rows.Count; rowIndex++)
                    {
                        // 获取当前行
                        TableRow row = table.Rows[rowIndex];
                        // 遍历行中的单元格
                        for (int cellIndex = 0; cellIndex < row.Cells.Count; cellIndex++)
                        {
                            // 获取当前单元格
                            TableCell cell = row.Cells[cellIndex];
                            // 获取单元格中的文本
                            string cellText = "";
                            for (int paraIndex = 0; paraIndex < cell.Paragraphs.Count; paraIndex++)
                            {
                                cellText += (cell.Paragraphs[paraIndex].Text.Trim() + " ");
                            }
                            // 将单元格文本写入工作表
                            ws.SetCellValue(rowIndex + 1, cellIndex + 1, cellText);
                        }
                        // 自动调整工作表中列的宽度
                        ws.Range.AutoFitColumns();
                    }
                }
            }
            // 将工作簿保存为 Excel 文件
            wb.SaveToFile("表格/Word表格转Excel.xlsx", ExcelVersion.Version2016);
            doc.Close();
            wb.Dispose();
        }
    }
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
 



 
					



