Edit Excel file in C#, VB.NET
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 edit Excel files in C# or VB.NET.
While working with Excel templates or existing Excel workbooks, it is necessary to edit Excel files and modify data in the existing Excel files. This article explains how to add or modify data in an existing workbook and edit Excel files with an example.
Steps to edit Excel file 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 applications 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: Add the following code snippet to edit Excel files in C# or VB.NET.
C#
// Instantiate the spreadsheet creation engine using (ExcelEngine excelEngine = new ExcelEngine()) { // Initialize application IApplication application = excelEngine.Excel; // Open existing workbook with data entered Assembly assembly = typeof(Program).GetTypeInfo().Assembly; Stream fileStream = assembly.GetManifestResourceStream("EditExcelFile_CS.Sample.xlsx"); IWorkbook workbook = application.Workbooks.Open(fileStream, ExcelOpenType.Automatic); IWorksheet worksheet = workbook.Worksheets[0]; // Add text data worksheet.Range["A1"].Text = "Employee No"; worksheet.Range["B1"].Text = "Employee Name"; worksheet.Range["C1"].Text = "Birth Date"; // Add DateTime data worksheet.Range["C2"].DateTime = new DateTime(1976, 1, 10); worksheet.Range["C3"].DateTime = new DateTime(1973, 2, 10); worksheet.Range["C4"].DateTime = new DateTime(1980, 3, 10); // Apply number format for date value cells C2 to C4 worksheet.Range["C2:C4"].NumberFormat = "mmmm, yyyy"; // Auto-size the columns to fit the content worksheet.AutofitColumn(1); worksheet.AutofitColumn(2); worksheet.AutofitColumn(3); // Add numeric data worksheet.Range["A2"].Number = 68878; worksheet.Range["A3"].Number = 71550; worksheet.Range["A4"].Number = 72808; // Save the edited workbook to disk in xlsx format workbook.SaveAs("Output.xlsx"); }
VB.NET
'Instantiate the spreadsheet creation engine Using excelEngine As ExcelEngine = New ExcelEngine() 'Initialize application Dim application As IApplication = excelEngine.Excel 'Open existing workbook with data entered Dim assembly As Assembly = GetType(Module1).GetTypeInfo.Assembly Dim fileStream As Stream = assembly.GetManifestResourceStream("EditExcelFile_VB.Sample.xlsx") Dim workbook As IWorkbook = application.Workbooks.Open(fileStream, ExcelOpenType.Automatic) Dim worksheet As IWorksheet = workbook.Worksheets(0) 'Add text data worksheet.Range("A1").Text = "Employee No" worksheet.Range("B1").Text = "Employee Name" worksheet.Range("C1").Text = "Birth Date" 'Add DateTime data worksheet.Range("C2").DateTime = New DateTime(1976, 1, 10) worksheet.Range("C3").DateTime = New DateTime(1973, 2, 10) worksheet.Range("C4").DateTime = New DateTime(1980, 3, 10) 'Apply number format for date value cells C2 to C4 worksheet.Range("C2:C4").NumberFormat = "mmmm, yyyy" 'Auto-size the columns to fit the content worksheet.AutofitColumn(1) worksheet.AutofitColumn(2) worksheet.AutofitColumn(3) 'Add numeric data worksheet.Range("A2").Number = 68878 worksheet.Range("A3").Number = 71550 worksheet.Range("A4").Number = 72808 'Save the edited workbook to disk in xlsx format workbook.SaveAs("Output.xlsx") End Using
A complete working example to edit Excel file along with input file used to edit can be downloaded from Edit Excel File.zip.
By executing the program, you will get the edited output Excel file as shown below.
Output Excel document
Take a moment to peruse the documentation, where you can find basic worksheet data manipulation options along with features like Conditional Formatting, worksheet calculations through Formulas, adding Charts in worksheet or workbook, organizing and analyzing data through Tables and Pivot Tables, appending multiple records to worksheet using Template Markers, and most importantly PDF and Image conversions with code examples.
Refer here to explore the rich set of Syncfusion® Excel (XlsIO) library features.
An online sample link to generate Excel file.
See Also:
Read Excel from Code in C#, VB.NET
Create Excel file in Azure platform
How to convert file to CSV in C#, VB.NET?
Create Excel from DataTable in C#, VB.NET
Note:
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 how to edit Excel file in C#, VB.NET.
You can refer to our WinForms Excel 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 WinForms Excel 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!