How to find and replace a text with the Excel content in ASP.NET Core Word document?
Syncfusion Essential DocIO is a .NET Core Word Library used to create, read, edit, and convert Word documents programmatically without Microsoft Word or interop dependencies. Syncfusion Essential XlsIO is a .NET Core Excel Library used to create, read, edit, and convert Excel files programmatically without Microsoft Office COM libraries & Microsoft Office. Using this library, you can find and replace a text with the Excel content in a Word document using C#.
Steps to find and replace a text with the Excel content in a Word document using C#:
- Create a new C# Console application project.
- Install the Syncfusion.DocIO.Net.Core and Syncfusion.XlsIO.Net.Core NuGet package as a reference to your project from Nuget.org.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion license key in your application to use the components without trail message.
- Include the following namespace in the Program.cs file.
C#
using Syncfusion.XlsIO;
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
using Syncfusion.Drawing;
- Use the following code snippet in Program.cs to find and replace a text with the Excel content in a Word document.
C#
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"../../../Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
//Opens an existing Word document.
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
{
//Create a new table
WTable table = new WTable(document);
//Resizes the table to fit the contents respect to the contents
table.AutoFit(AutoFitType.FitToContent);
//Get the Excel content
ExtractExcelContent(table);
//Replaces the table placeholder text with a new table.
TextBodyPart bodyPart = new TextBodyPart(document);
bodyPart.BodyItems.Add(table);
document.Replace("<<ExcelPlaceHolder>>", bodyPart, true, true, false);
//Load the file into stream
FileStream outputStream = new FileStream("../../../Data/Output.docx", FileMode.Create, FileAccess.Write);
//Save the Word document.
document.Save(outputStream, FormatType.Docx);
}
}
- Use the following method to extract the Excel content to an empty table in a Word document.
C#
void ExtractExcelContent(WTable table)
{
//Open the Excel file
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
//Load the file into stream
FileStream inputExcelStream = new FileStream("../../../Data/Sample.xlsx", FileMode.Open, FileAccess.Read);
IWorkbook workbook = application.Workbooks.Open(inputExcelStream);
//Get the first worksheet
IWorksheet worksheet = workbook.Worksheets[0];
//Get the number of rows used.
int rows = worksheet.Rows.Length;
//Get the number of columns used.
int columns = worksheet.Columns.Length;
//Create the rows and columns based on the excel values.
table.ResetCells(rows, columns);
//Set the border style
table.TableFormat.Borders.BorderType = BorderStyle.Single;
table.TableFormat.Borders.LineWidth = 1;
table.TableFormat.Borders.Color = Color.Black;
//Iterate through the excel rows
for (int rowIndex = 0; rowIndex < rows; rowIndex++)
{
//Get the row range from excel
IRange rowRange = worksheet.Rows[rowIndex];
//Iterate through the excel columns
for (int cellIndex = 0; cellIndex < columns; cellIndex++)
{
//Get the cell from the excel
IRange cell = rowRange.Cells[cellIndex];
//Add a paragraph
WParagraph paragraph = table[rowIndex, cellIndex].AddParagraph() as WParagraph;
//Get the content of the cell
WTextRange textRange = paragraph.AppendText(cell.DisplayText) as WTextRange;
//Set the bold and italic format
textRange.CharacterFormat.Bold = cell.CellStyle.Font.Bold;
textRange.CharacterFormat.Italic = cell.CellStyle.Font.Italic;
//Get the font size
textRange.CharacterFormat.FontSize = (float)cell.CellStyle.Font.Size;
//Get the font name
textRange.CharacterFormat.FontName = cell.CellStyle.Font.FontName;
}
}
}
You can download a complete working sample to find and replace a text with the Excel content in a Word document from GitHub.
Take a moment to peruse the [documentation](https where you can find basic Word document processing options along with the features like mail merge, merge, split, and compare documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.
Conclusion
I hope you enjoyed learning about how to find and replace a text with the Excel content in a Word document using .NET Core Word Library.
You can refer to our ASP.NET Core DocIO feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core DocIO example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!