• Signup
  • Blog Posting Services
  • Hosting Plans
  • Newsroom
  • Explore Us
  • Login
  • In Twitter
  • Blog Posting Services
  • In LinkedIn
  • Our Code Repository
J
HARAPHULA
OneStop Shop of Information

Updates

  • The Role of Gaming in Education: How Games are used to Teach and Engage
    •
  • Mobile Repairing Course in Rawalpindi – Learn to Fix iPhones and Android Phones
    •
  • The Role of Pricing Procedures in SAP SD and their Configuration
    •
  • Top-rated best Commerce Coaching in Patna for 11 and 12
    •
  • BCA Distance Education in India – Best Universities, Fees and Career Guide
    •
  • Why Pursuing a Cisco Certification Course Can Transform your IT Career?
    •
  • The 9 Things you must know to Prepare for IIT JEE
    •
  • CMA US – Your Pathway to a Rewarding Career in Management Accounting
    •
  • Top 8 Advantages of Taking the IIT JAM Exam
    •
  • How LMS Tools Empower Collaborative Learning Experiences?
    •
  • Explore the Leading Computer Teacher Training Diploma Courses in 2025
    •
  • How to Choose the best ADCA Institute for your Career Goals?
    •
  • Is CCA the Right Course for Non-IT Background Students?
    •
  • Your Guide to MBBS in Russia – Eligibility, Fees, and Admission
    •
  • Understanding the Core Concepts of ADCA
    •

How to display Excel File records in an ASP.NET Gridview?

Microsoft Technologies
December 7, 2016
3.7 (3 votes)
How to display Excel File records in an ASP.NET Gridview? How to display Excel File records in an ASP.NET Gridview?
3 5 72
Making you Earn. Developing Web2 Blogs with CPM Ads

Due to the popularity of MS Excel, Still today more than 75% of Organizations prefers to maintain their data in MS-Excel. Whether it’s the matter of project management or a budget plan, MS-Excel is an awesome tool. I noticed in my organization more then 80% percent records are in Excel. One day my boss come to me with a plan that he need to update “Utilization_Reports.xlsx” using User Interface. For him I had taken the ownership to develop this tool. During this development phases I found how to fetch data from an Excel File like a database table using SQL Queries. This stay interesting for me. Sharing the same for your reference.

Gridview

<asp:GridView ID="grdRecords" runat="server" AutoGenerateColumns="false" AllowPaging="true" PageSize="10" Font-Names="Arial" Font-Size="11pt" AlternatingRowStyle-BackColor="#C2D69B" HeaderStyle-BackColor="green" HeaderStyle-ForeColor="#FFFFFF" OnPageIndexChanging="OnPaging" AutoGenerateEditButton="true" Width="100%">
<Columns>
<asp:TemplateField  HeaderText="Employee Name" SortExpression="Name">
<ItemTemplate>
<asp:Label ID="lblEmpName" runat="server" Text='<%# Bind("Name")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employee ID" SortExpression="ID">
<ItemTemplate>
<asp:Label ID="lblEmpID" runat="server" Text='<%# Bind("ID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Status:
<asp:DropDownList ID="ddlStatus" runat="server" OnSelectedIndexChanged="CountryChanged" AutoPostBack="true" AppendDataBoundItems="true">
<asp:ListItem Text = "ALL" Value = "ALL"></asp:ListItem>
</asp:DropDownList>
</HeaderTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlRowStatus" runat="server" AutoPostBack="true">
<asp:ListItem>thisis</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<%# Eval("Status") %>
</ItemTemplate>
</asp:TemplateField>                    
</Columns>
</asp:GridView>

As we mentioned above here I want to fetch an excel file like a database table. So first let us declare the connection string for MS-Excel. That’s what I did in below web.config file.

Web.Config

<appSettings>
<add key="FolderPath" value="Files/"/>
</appSettings>
<connectionStrings>
<!--<add name ="Excel03ConString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}; Extended Properties='Excel 8.0'" />-->
<add name ="ConnString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties='Excel 8.0'"/>
</connectionStrings>

To bind data to the Gridview in Codebehind I am with the below Subroutine. In BindGrid I am passing FilePath and File Extention as the parameters. These are simple string varibales. Declare as global variables. To fetch data from Excel like a database First I am traking the 0th Sheet of Excel. Then running a SQL Query over the table of my Excel File. Finally using a ASP.NET DataAdapter I am filling data to the gridview.

BindGrid

Private Sub BindGrid(ByVal FilePath As String, ByVal Extension As String)

ConStr = String.Format(ConStr, FilePath)

Dim connExcel As New OleDbConnection(ConStr)
Dim cmdExcel As New OleDbCommand()
Dim oda As New OleDbDataAdapter()
Dim dtbl As New DataTable()

cmdExcel.Connection = connExcel

'Get the name of First Sheet 
connExcel.Open()

Dim dtExcelSchema As DataTable
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
Dim SheetName As String = dtExcelSchema.Rows(0)("TABLE_NAME").ToString()

connExcel.Close()

'Read Data from First Sheet 
connExcel.Open()

If ViewState("Filter").ToString() = "ALL" Then
cmdExcel.CommandText = "SELECT * From [" &amp; SheetName &amp; "]"
Else
cmdExcel.CommandText = "SELECT * From [" &amp; SheetName &amp; "] WHERE Status='" &amp; ViewState("Filter").ToString() &amp; "'"
End If

oda.SelectCommand = cmdExcel
oda.Fill(dtbl)
connExcel.Close()

'Bind Data to GridView 
grdRecords.DataSource = dtbl
grdRecords.DataBind()

End Sub

The above subroutine is responsible to Fill the Gridview. But to display data in Gridview we need to Call this during page load. That’s what I am doing in below. Inside the not page.ispostback event I am calling the Function with required parameters.

In Page Load

Dim FilePath as String = "C:/demo.xlsx"
Dim Extension as String = ".xlsx"

If Not Page.IsPostBack Then
ViewState("Filter") = "ALL"
BindGrid(FilePath, Extension)
End If

Tags:ASP.NET Gridview, ASP.NET Gridview Example, Display Excel File records, MS-Excel like Table, Page.IsPostBack, Records in an ASP.NET Gridview
Email Marketing plan for Small/Medium Size Businesses
How to add Search Filter in the Column Header of Gridview?
Causes and Symptoms of Miscarriage – Tips for Safe Pregnancy
FREE PHP Widgets to boost Conversion and low Bounce Rates
demo

Related Posts

  • List of frequently used common SQL Queries with Example
    List of frequently used common SQL Queries with Example
  • Microsoft ASP.NET Interview Questions with Answers
    Microsoft ASP.NET Interview Questions with Answers
  • How to update records in a Gridview using Auto Generate Edit Button?
    How to update records in a Gridview using Auto Generate Edit Button?
  • How to implement Paging & Sorting in ASP.NET Gridview Example?
    How to implement Paging and Sorting in ASP.NET Gridview Example?
  • Web Storage (LocalStorage) vs Web SQL vs IndexedDB in HTML5
    Using LocalStorage Objects vs Web SQL Database in HTML5
  • Example of Resume for IT professionals to get their Dream Job
    Example of Resume for IT professionals to get their Dream Job
  • ASP.NET Login form Example validating user from SQL Server Database
    ASP.NET Login form Example validating user from SQL Server Database
  • How to display icon images in Gridview or Datagrid rows?
    How to display various Files icon images in Datagrid rows?
  • ADO.NET Architecture with Examples for beginners
    ADO.NET ExecuteNonQuery, ExecuteReader, ExecuteScalar Examples
  • How to add Search Filter in the Column Header of Gridview?
    How to add Search Filter in the Column Header of Gridview?
  • PHP Treeview Example using data from MySQL Database
    PHP Treeview Example using data from MySQL Database
  • How to Rearrange Rows in a Gridview using Drag and Drop?
    How to Rearrange Rows in a Gridview using Drag and Drop?
Bloggers with AdSense Ads Go for the Top rated CPM Ad Network to Earn better

OUR FACILITIES

  • Signup
  • Login
  • Our Background
  • Policies
  • WordPress.org

CONTACT INFO

  • Reach Us
  • WhatsApp +919096266548

PUBLISH WITH US

Small Business Owners try the power of Article Marketing to grab Leads from Google or Local Search. Organic viewers are the real buyers. Double your Sales with our high DA lifelong SEO backlinks.

WHY ONESTOP?

We are here to develop high Quality Information. As a Multi-niche platform, We worked Several years for Collecting various useful Stories. Dream to establish a Domain where you can get all your day-to-day required information. We Covers Animals to Zoology.
©2014-2026 JHARAPHULA, ALL RIGHTS RESERVED.