SIGN UP MEMBER LOGIN:    
ARTICLE

Read and Write Excel Data Using C#

Posted by Karthikeyan Anbarasan Articles | C# Language June 17, 2011
In this article we are going to see how to use an Excel sheet as a source to load data.
Reader Level:

Introduction:

In this article we are going to see how to use an Excel sheet as a source to load data in windows application or a web application and use the same to export back to the Excel sheet using C# and VB.net. This requirement is straight-forward and used in day-to-day activity, but is often messed up in some section of the code.

Overview:

Normally when there is a requirement for accessing data from an Excel sheet, we first choose to start with the office interop assemblies (Office Automation Assemblies) and make a connection to the Excel sheet and start processing but that has some disadvantages such as using it over the web (Check this link for more details Issues). So the alternate option is to use the OLEDB Provider to read the data and use it for the front end with an additional parameter of passing the connection string to the Excel sheet.

Now we consider that we have things ready to export data from and import data to the Excel sheet and the data is available in a dataset or a data table. We can use the following code snippets to get the data passed to and from Excel.
The following code snippet will be used to export the data from local to Excel Sheet.

Code : Writing data to Excel sheet

 
string connectionString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=D:\MySamplefile.xls; Extended Properties=Excel 8.0;"
 
using(OleDbConnection Connection = new OleDbConnection(connectionString))
{
Connection.Open()
using(OleDbCommand command = new OleDbCommand())
{
command.Connection = Connection;
command.CommandText = "CREATE TABLE [EmpTable$](EmpFirstName Char(100), EmpLastName char(100), EmpDept char(250))";
command.ExecuteNonQuery();
}
//Add values to the table (EMPTable) in the Worksheet
using(OleDbCommand command = new OleDbCommand())
{
command.Connection = Connection;
command.CommandText = "INSERT INTO TABLE [EmpTable$](EmpFirstName ,EmpLastName ,EmpDept ) VALUES('Karthik','Anbu','karthik.Anbu@xyz.com')";
command.ExecuteNonQuery();
command.CommandText = "INSERT INTO TABLE [EmpTable$](EmpFirstName ,EmpLastName ,EmpDept ) VALUES('Arun','Kumar','Arun.Kumar@xyz.com')";
command.ExecuteNonQuery();
}

Code : Reading data from Excel sheet

DataTable dt; 
string connectionString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=D:\MySamplefile.xls; Extended Properties=Excel 8.0;"
using(OleDbConnection Connection = new OleDbConnection(connectionString))
{
Connection.Open()
using(OleDbCommand command = new OleDbCommand())
{
command.Connection = Connection;
command.CommandText = "SELECT * FROM [EmpTable]";
using(OleDbDataAdapter adapter =new OleDbDataAdapter())
{
adapter.SelectCommand = command;
adapter.Fill(dt);
}
}
}

Conclusion:

So in this article we have seen how to do a small manipulation of reading and writing Excel data using C# which we normally require in our day-to-day coding.

Login to add your contents and source code to this article
share this article :
post comment
 

Advance Thanks.

Posted by sumanth d Feb 23, 2012

hi Please i try this code but i have this error "Server Error in '/' Application. The Microsoft Jet database engine cannot open the file ''. It is already opened exclusively by another user, or you need permission to view its data. "

Posted by eehab soft Dec 29, 2011

GOod

Posted by byun sukhyun Jun 22, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor