)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Edit Excel file in C#, VB.NET

Platform: WinForms |
Control: XlsIO |
Published Date: August 6, 2018 |
Last Revised Date: June 12, 2019

Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files. Using this library, you can edit Excel file in C#, VB.NET.

While working with Excel templates or existing Excel workbooks, it is necessary to edit Excel file and modify data in the existing Excel file. This article explains how to add or modify data in the existing workbook and edit Excel file with an example.

Steps to edit Excel file programmatically:

Step 1: Create a new C# console application project.

Create C# Console Application

Create a new C# console application

Step 2: Install Syncfusion.XlsIO.WinForms NuGet package as a reference to your .NET Framework applications from the NuGet.org.

Install NuGet Package

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 file in C#, 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 after editing Excel file

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

Create Excel file in WPF

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 trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
Alina Riva
Jan 10, 2020

You can use ZetExcel for empowering you to build cross-platform applications having the ability to generate, modify, convert, render and print spreadsheets without using Microsoft Excel.

Reply

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon