How to create spreadsheet from existing template in WinForms Excel?
Syncfusion® Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. It also, converts Excel documents to PDF files. Using this library, you can create a spreadsheet from an existing template in C#, VB.NET.
You can create spreadsheet from existing template by using the ‘Workbooks.Open’ method. A template is a workbook whose formatting will be inherited by the new workbook created. The template may be designed either through the code or through the Microsoft Excel designer. It is even easier to create the template using the Microsoft Excel designer.
Steps to create spreadsheet from existing template, programmatically:
Step 1: Create a new C# console application project.
Create a new C# console application
Step 2: Install the Syncfusion.XlsIO.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org.
Install NuGet package
Step 3: Include the following namespaces in the Program.cs file.
C#
using Syncfusion.XlsIO; using System.IO; using System.Reflection;
VB.NET
Imports Syncfusion.XlsIO Imports System.IO Imports System.Reflection
Step 4: Use the following code snippet to create spreadsheet from existing template in C#, VB.NET.
C#
// Create Excel engine using (ExcelEngine excelEngine = new ExcelEngine()) { // Instantiate the application object IApplication application = excelEngine.Excel; // Create spreadsheet from existing template IWorkbook workbook = application.Workbooks.Open("Template.xlsx"); // The first worksheet object in the worksheets collection is accessed IWorksheet worksheet = workbook.Worksheets[0]; // A text is set in the first cell of the worksheet worksheet[1, 1].Text = "Syncfusion Essential Studio"; // Save the spreadsheet workbook.SaveAs("Output.xlsx"); }
VB.NET
'Create Excel engine Using excelEngine As ExcelEngine = New ExcelEngine() 'Instantiate the application object Dim application As IApplication = excelEngine.Excel 'Create spreadsheet from existing template Dim workbook As IWorkbook = application.Workbooks.Open("Template.xlsx") 'The first worksheet object in the worksheets collection is accessed Dim worksheet As IWorksheet = workbook.Worksheets(0) 'A text is set in the first cell of the worksheet worksheet(1, 1).Text = "Syncfusion Essential Studio" 'Save the spreadsheet workbook.SaveAs("Output.xlsx") End Using
A complete Windows Forms working example of how to create a spreadsheet from an existing template in C# or VB.NET can be downloaded from create-spreadsheet-from-existing-template.zip.
By executing the program, you will get the output as below.
Output Excel document
Refer here to explore the rich set of Syncfusion® Excel (XlsIO) library features.
See Also:
Create simple spreadsheet in C#, VB.NET using XlsIO
Does the XlsIO component support Excel spreadsheets that have VBA code in them?
How to create an Excel file in Windows Forms
How do I protect certain cells in a spreadsheet?
How do I create a workbook with the specified number of worksheets?
How to edit an existing Excel file in C#, VB.NET
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 trial message.
Conclusion
I hope you enjoyed learning about how to replace an existing image in Excel worksheet using C#,VB.NET.
You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion® components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, Support Tickets, or feedback portal. We are always happy to assist you!