Introduction
This article explains how to retrieve data from Excel using ADO.NET then store it into a dataset stored as DataSets data to be shown in a GridView.
I am showing you it step-by-step:
Step 1: Open Visual Studio and create a new ASP.NET project and create a webform.
Step 2: Now on that webform use one FileUploadControl. Using this we will upload an Excel file from our system. Create one button to upload the data to the server, one label to show you what file's data is showing in the grid view, and use one GridView control in which we will show our Excel sheet's data.
So my design aspx page is like; this you can just copy and paste the aspx page given below:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ImportExelDataInGridView.aspx.cs" Inherits="ReadDataFromExcel.ImportExelDataInGridView" %>
-
- <!DOCTYPE html>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
-
- Import Excel File:
- <asp:FileUpload ID="FileUpload1" runat="server" />
- <br />
- <br />
- <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" />
- <br />
- <br />
- <asp:Label ID="Label1" runat="server"></asp:Label>
- <br />
- <asp:GridView ID="gvExcelFile" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
- <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
- <EditRowStyle BackColor="#999999" />
- <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
- <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
- <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
- <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
- <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
- <SortedAscendingCellStyle BackColor="#E9E7E2" />
- <SortedAscendingHeaderStyle BackColor="#506C8C" />
- <SortedDescendingCellStyle BackColor="#FFFDF8" />
- <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
- </asp:GridView>
-
- </div>
- </form>
- </body>
- </html>
Using the code above you will get output of the page such as the following:
Step 3: Now on the code behind of this aspx page, use the following two namespaces:
System.Data;
System.Data.OleDb;
Step 4: For the click event of the upload button you should write the following code:
- protected void btnUpload_Click(object sender, EventArgs e)
- {
-
- string ConStr = "";
-
-
- string ext = Path.GetExtension(FileUpload1.FileName).ToLower();
-
- string path = Server.MapPath("~/MyFolder/"+FileUpload1.FileName);
-
- FileUpload1.SaveAs(path);
- Label1.Text = FileUpload1.FileName + "\'s Data showing into the GridView";
-
- if (ext.Trim() == ".xls")
- {
-
- ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
- }
- else if (ext.Trim() == ".xlsx")
- {
-
- ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
- }
-
- string query = "SELECT * FROM [Sheet1$]";
-
- OleDbConnection conn = new OleDbConnection(ConStr);
-
-
- if (conn.State == ConnectionState.Closed)
- {
- conn.Open();
- }
-
- OleDbCommand cmd = new OleDbCommand(query, conn);
-
- OleDbDataAdapter da = new OleDbDataAdapter(cmd);
- DataSet ds = new DataSet();
-
- da.Fill(ds);
-
- gvExcelFile.DataSource = ds.Tables[0];
-
- gvExcelFile.DataBind();
-
- conn.Close();
- }
Step 5: Now create one Excel file such as the following:
Now save this Excel file with any name like MyNewExcelFile.xlsx.
Step 6: Now run the project.
After choosing the Excel file:
After clicking on the Upload button you will see the following output in the GridView: