Friday, 12 August 2016

Get Or Retrieve Excel File Sheet Name Using ADO.Net in Asp.net Using C#

8/12/2016 - By Pranav Singh 0


This article will show you how you can get or retrieve excel file sheet name using ADO.Net in asp.net using c#.net.


So for this article first we will create a new excel sheet and check the sheets present in the excel.


After creating excel we will create an asp.net page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ImportExcel.aspx.cs" Inherits="WebApplication7.ImportExcel" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Get Or Retrieve Excel File Sheet Name Using ADO.Net in Asp.net Using C#</title>
</head>
<body>
    <form id="form1" runat="server">
    <div id="divSheetname" runat="server">
   
    </div>
    </form>
</body>
</html>

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 file sheets.

  DataTable dtExcelSheet = new DataTable();
            dtExcelSheet = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
   for (int i = 0; i < dtExcelSheet.Rows.Count; i++)
            {
                sheetname = sheetname + dtExcelSheet.Rows[i]["TABLE_NAME"].ToString() + "<br/>";
            }
            divSheetname.InnerHtml = sheetname;

Above code after reading all the sheets I have bind it the div control to display it. 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
            string sheetname = "";
            DataTable dtExcelSheet = new DataTable();
            dtExcelSheet = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            for (int i = 0; i < dtExcelSheet.Rows.Count; i++)
            {
                sheetname = sheetname + dtExcelSheet.Rows[i]["TABLE_NAME"].ToString() + "<br/>";
            }
            divSheetname.InnerHtml = sheetname;

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



Tags: , ,
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

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


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top