My previous article discussed about How Displaying Validation Errors with ASP.NET MVC. Now in this article, I’ve covered a brief introduction about How to Import or Upload and Display Excel File in GridView Using C# in ASP.NET. There are lots of ways for Importing data from Excel to ASP.NET using C#, and here I’m going to introduce one simple common method to import data.
First we will create a new excel sheet with some data.
So for this article 1st I'll create a new asp.net application and add the beneath code inside your web page.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Excel File Upload Or Import and Display In GridView Using C# In Asp.Net</title>
</head>
<body>
<form id="form1" runat="server">
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload & Display" OnClick="btnUpload_Click" />
<br />
<br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>
<br />
</form>
</body>
</html>
Now please check the code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.OleDb;
using System.Data;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string fileExtention = System.IO.Path.GetExtension(FileUpload1.FileName);
if (fileExtention == ".xls" || fileExtention == ".xlsx")
{
string fileName = System.IO.Path.GetFileName(FileUpload1.FileName);
FileUpload1.SaveAs(Server.MapPath("~/ExcelSheet/" + fileName));
/*Read excel sheet*/
string excelSheetFilename = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("~/ExcelSheet/" + fileName) + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
OleDbConnection objcon = new OleDbConnection(excelSheetFilename);
string queryForExcel = "Select * from [UserDetail$];";
OleDbDataAdapter objda = new OleDbDataAdapter(queryForExcel, objcon);
DataSet objds = new DataSet();
objda.Fill(objds);
if (objds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = objds.Tables[0];
GridView1.DataBind();
}
}
else
{
lblMessage.Text = "Please upload excel sheet.";
}
}
}
}
}
Now in above code I've very first read the file and saved inside a folder and also the I study the excel sheet and stored in dataset and bind it towards the gridview manage.
Now we will develop the folder.
Within this folder we'll upload the file as shown in code.
Now we've accomplished run the application and check the output.
Best ASP.NET Hosting Recommendation
ASPHostPortal.com provides its customers with Plesk Panel, one of the most popular and stable control panels for Windows hosting, as free. You could also see the latest .NET framework, a crazy amount of functionality as well as Large disk space, bandwidth, MSSQL databases and more. All those give people the convenience to build up a powerful site in Windows server. ASPHostPortal.com offers ASP.NET hosting starts from $1/month only. They also guarantees 30 days money back and guarantee 99.9% uptime. If you need a reliable affordable ASP.NET Hosting, ASPHostPortal.com should be your best choice.