C# 提取word指定内容到excel
时间: 2023-09-09 09:14:06 浏览: 127
可以使用 Microsoft.Office.Interop.Word 和 Microsoft.Office.Interop.Excel 库来实现此功能。下面是一个简单的示例代码:
```csharp
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.Excel;
// 打开 Word 文档
Application wordApp = new Application();
Document wordDoc = wordApp.Documents.Open(@"C:\path\to\your\word\file.docx");
// 新建 Excel 文件
Application excelApp = new Application();
Workbook excelWorkbook = excelApp.Workbooks.Add(Type.Missing);
// 获取 Excel 工作表
Worksheet excelWorksheet = (Worksheet)excelWorkbook.ActiveSheet;
// 查找 Word 文档中的指定内容
string searchTerm = "指定内容";
Range range = wordDoc.Content;
range.Find.Execute(searchTerm);
// 将找到的内容复制到 Excel 中
if (range != null)
{
string foundText = range.Text;
excelWorksheet.Cells[1, 1] = foundText;
}
// 保存 Excel 文件
excelWorkbook.SaveAs(@"C:\path\to\your\excel\file.xlsx");
// 关闭 Word 和 Excel 应用程序
wordDoc.Close();
wordApp.Quit();
excelWorkbook.Close();
excelApp.Quit();
```
请注意,此示例仅提供了基本的功能代码,可能需要根据实际情况进行修改和完善。
阅读全文
相关推荐















