如何用C#或Open XML程序化将Word文档内容插入Excel单元格?
Absolutely—this is totally achievable, and even straightforward with the right tools, especially if you’re working in .NET (which your username hints at!). Here’s a step-by-step breakdown of how to implement this:
Step 1: Read Content from DOCX Files
First, you’ll need to extract text (or formatted content) from your Word documents. For .NET apps, two great options are:
- DocX Library: A beginner-friendly wrapper around Microsoft’s OpenXML SDK, perfect for simple text extraction.
- OpenXML SDK: Microsoft’s official low-level library, ideal if you need to preserve complex formatting.
Example: Extract Plain Text with DocX
First, install the Xceed.Document.NET NuGet package. Then use this code:
using Xceed.Document.NET; using System.IO; public string ExtractDocxText(string docxFilePath) { if (!File.Exists(docxFilePath)) throw new FileNotFoundException("Word document not found.", docxFilePath); using (var doc = DocX.Load(docxFilePath)) { return doc.Text; } }
Step 2: Insert Content into Excel
Next, you’ll need to write the extracted content into an Excel file. Popular .NET libraries for this are:
- EPPlus: Widely used, supports .xlsx files and rich text formatting.
- ClosedXML: A clean, intuitive API for Excel manipulation.
Example: Insert Text into Excel with EPPlus
Install the EPPlus NuGet package first. Then use this snippet:
using OfficeOpenXml; using System.IO; public void InsertTextIntoExcel(string excelFilePath, string content, int targetRow, int targetColumn) { // Set license context (required for EPPlus 5+) ExcelPackage.LicenseContext = LicenseContext.NonCommercial; FileInfo excelFile = new FileInfo(excelFilePath); using (var package = new ExcelPackage(excelFile)) { // Get or create the first worksheet var worksheet = package.Workbook.Worksheets.FirstOrDefault() ?? package.Workbook.Worksheets.Add("Employee Data"); // Insert content into the target cell worksheet.Cells[targetRow, targetColumn].Value = content; // Optional: Auto-adjust column width to fit content worksheet.Column(targetColumn).AutoFit(); package.Save(); } }
Step 3: Combine the Two Processes
Put it all together to read a DOCX, insert its content into Excel, and save:
string docxPath = @"C:\Docs\EmployeeBio.docx"; string excelPath = @"C:\Excel\EmployeeData.xlsx"; // Extract text from Word doc string docContent = ExtractDocxText(docxPath); // Insert into row 3, column 4 (D3) of the Excel file InsertTextIntoExcel(excelPath, docContent, 3, 4);
Bonus: Preserve Rich Text Formatting
If you need to keep bold, italics, or other formatting from the DOCX in Excel, both libraries support rich text cells. For example, with EPPlus you can build formatted content using worksheet.Cells[row, column].RichText (you’ll need to parse formatting from the DOCX first using OpenXML or DocX’s advanced features).
Key Notes
- Always handle exceptions (file not found, permission issues) in production code.
- Make sure to install the required NuGet packages for the libraries you choose.
内容的提问来源于stack exchange,提问作者dontnetnewbie




