connectseekers.com

Wednesday, 10 August 2016

Excel Sheet Import and Read Data using ADO.Net in Asp.net Using C#

8/10/2016 - By Pranav Singh 0

This article will show you how you can import the excel sheet and read it’s data using ADO.Net in asp.net using c#.net.


So for this article first we will create a new excel sheet.



Now first we will create the connection string to read the excel sheet.

string excelFilePath = @"ExcelFile\StudentData.xls";
            String strExcelConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(excelFilePath) + ";Extended Properties='Excel 8.0;HDR=Yes'";

            OleDbConnection con = new OleDbConnection(strExcelConn);
            con.Open();

In above code I have defined the path of the excel sheet and prepared the connectionstring. I have passed the connection string to OleDbConnection provider to establish the connection. In above code one of the most important thing is that you must use Server.MapPath to read the file.

After making the connection I have read the excel sheets. So that we can prepare the query to read the data of specific sheet.

  DataTable dtExcelSheet = new DataTable();
            dtExcelSheet = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            string sheetname = dtExcelSheet.Rows[0]["TABLE_NAME"].ToString();

Above code will return the sheet names present in the excel file. Now we will read the data of excel sheet.

//Read data of the selected excel sheet
            DataSet ds = new DataSet();
            OleDbDataAdapter da = new OleDbDataAdapter("SELECT * From [" + sheetname + "]", con);
            da.Fill(ds);
            con.Close();

In above code I have used the OleDbDataAdapter to read the data in the dataset.

Here is the complete code.

string excelFilePath = @"ExcelFile\StudentData.xls";
            String strExcelConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(excelFilePath) + ";Extended Properties='Excel 8.0;HDR=Yes'";

            OleDbConnection con = new OleDbConnection(strExcelConn);
            con.Open();
            //Read data sheet name
            DataTable dtExcelSheet = new DataTable();
            dtExcelSheet = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            string sheetname = dtExcelSheet.Rows[0]["TABLE_NAME"].ToString();

            //Read data of the selected excel sheet
            DataSet ds = new DataSet();
            OleDbDataAdapter da = new OleDbDataAdapter("SELECT * From [" + sheetname + "]", con);
            da.Fill(ds);
            con.Close();

Now we Have done run the page to check the output.

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top