Exporting / Importing Excel data into SQL Server Database using ASP.net

No.of Views3157
Bookmarked1 times
Downloads 
Votes0
By  Prajeesh   On  22 May 2010 06:05:35
Tag : ASP.NET , General
Exporting / Importing Excel data into SQL Server Database using ASP.NET.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In certain occasions you may need to export / import large excel spreadsheet to SQL server data base. In this small and simple article i would like to demonstrate the process of exporting / importing Excel Data into SQL server database.

Step 1:

I am assuming you have created a folder and uploaded your Microsoft Excel worksheet in that folder.

Step 2:

You can create class for creating an export function

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.OleDb;
/// /// Class for Exporting Excel Data to SQL server/// public class clsExcelToSqlServer
{
public clsExcelToSqlServer()
{
//// TODO: Add constructor logic here//}
private string _FilePath;
public String FilePath
{
get { return _FilePath; }
set { _FilePath = value; }
}
public DataTable getDataFromExcelSheet()
{
try{
//File path of Excel Spread sheetFilePath = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath) +
"/ExcelFolder/ExcelAppliance.xls";
//Connection string to connect Excel datastring strConnectionString = string.Empty; strConnectionString =
@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ FilePath + @";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1""";
OleDbConnection cnCSV = new OleDbConnection(strConnectionString);
cnCSV.Open();
//Selecting all rows from excel sheetOleDbCommand cmdSelect = new OleDbCommand(@"SELECT * FROM [Sheet1$]", cnCSV);
OleDbDataAdapter daCSV = new OleDbDataAdapter();
daCSV.SelectCommand = cmdSelect;
DataTable dtCSV = new DataTable();
//Filling excel data into data tabledaCSV.Fill(dtCSV);
cnCSV.Close();
daCSV = null;
return dtCSV;
}
catch (Exception ex)
{
return null;
}
finally { }
}
}

 getDataFromExcelSheet() function returns a Data Table and you can use this Data table to export data into to SQL Server.

 
Sign Up to vote for this article
 
About Author
 
Prajeesh
Occupation-Not Provided
Company-Not Provided
Member Type-Fresh
Location-India
Joined date-15 May 2010
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
    Auto Growing TextBox or TextArea in ASP.NET
    Published Date : 08/May/2010
    In this code snippet, you will learn how to bind DropdownList within the ListView in ASP.NET. The ListView is powerful control and fully customizable using templates.
    Published Date : 10/Oct/2010
    In this codesnippet, i will show How to Delete Row in GridView using JQuery in ASP.NET.
    Published Date : 20/Jul/2011
    In this snippet, I will show how to format a cell and apply style in gridview using JQuery. Sometimes we may need to apply the format for a particular cell based on the cell value; it can be done in within DataRowBound event in asp.net.
    Published Date : 05/Jan/2011
    In this snippet I will explain how to add controls dynamically in asp.net and register events for the controls and make it work events perfectly. Last week I have read the forums many readers asking about add controls dynamically in asp.net giving problems and also it not working properly with events
    Published Date : 03/Jan/2011
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top