How to find and replace text in a Word document with text from database?
Syncfusion® Essential DocIO is a .NET Core Word library used to create, read, and edit Word documents programmatically without Microsoft Word or Interop dependencies. Using this library, you can find and replace text in a Word document with text from a database in C#.
Steps to find and replace text in a Word document with text from a database:
- Create a new C# .NET Core console application project.
- Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your .NET Core applications from NuGet.org and the System.Data.SqlClient NugGet package.
- Create a table in the database with columns "TextToFind" and "ReplacementText" as shown in the screenshot below:
- Include the following namespaces in the Program.cs file:
C#
using Syncfusion.DocIO; using Syncfusion.DocIO.DLS; using System.Data; using System.Data.SqlClient;
- Use the following code example to find and replace text in a Word document with text from a database in C#:
C#
using (FileStream inputStream = new FileStream(Path.GetFullPath(@"../../../Input.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { // Set datasource and database values. string datasource = ""; string database = ""; // Load the file stream into a Word document. using (WordDocument document = new WordDocument(inputStream, FormatType.Automatic)) { // Create SQL connection to get data from the database. SqlConnection sqlConnection = new SqlConnection("Data Source=" + datasource + ";Initial Catalog=" + database + ";Integrated Security=True"); // Retrieve data from the table 'FindReplace' using SqlCommand. SqlCommand sqlCommand = new SqlCommand("Select * from FindReplace"); sqlCommand.Connection = sqlConnection; // Load the data into DataTable using SqlDataAdapter. SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlCommand); dataAdapter.SelectCommand.CommandTimeout = 0; DataTable dataTable = new DataTable(); dataAdapter.Fill(dataTable); // Iterate DataRow to find and replace text with other text from the database. foreach (DataRow row in dataTable.Rows) { // First column contains text to find. // Second column contains replacement text. document.Replace(row[dataTable.Columns[0]] as string, row[dataTable.Columns[1]] as string, false, false); } // Create a file stream. using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Sample.docx"), FileMode.Create, FileAccess.ReadWrite)) { // Save the Word document to the file stream. document.Save(outputFileStream, FormatType.Docx); } } }
A complete working sample to find and replace in a Word document with text from database in C# can be downloaded from GitHub.
By executing the program, you will get the Output document as follows:
Take a moment to peruse the documentation, where you can find basic Word document processing options along with features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly, PDF and image conversions with code examples.
Explore more about the rich set of Syncfusion® Word Framework features.
See Also:
How to replace the particular text with a hyperlink in a Word document
How to replace text in a Word document with HTML
How to find and replace text inside table in Word document
How to find and replace text in headers and footers of Word document
How to find and replace placeholder with page break in a Word document
How to find and replace line break in a Word document as paragraph mark?
How to find and replace text with content control in a Word document?
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trail message.