This article will show you to excel file upload or import
and display in gridview using c# in asp.net. This article will show you how to read
excel file in asp.net, show the import and display the excel data in gridview
using c#.
Some of my previous articles are as follows: Read
List Of File From Server and Display In GridView Using C# In Asp.Net, How
to Change DateTime Format Using C# In Asp.Net, Facebook
like Button Integration In Asp.net Using C#.Net, Hide
Or Show Gridview Row By Column Name at RunTime In Asp.Net Using C#.Net, Display
Grand Total In Gridview Footer On RowDataBound In Asp.Net Using C#.Net, Detect
Header Control and Assign Value of Gridview RowDataBound In Asp.Net Using
C#.Net, Merge
Footer or Apply ColumnSpan to Footer of GridView In Asp.Net Using C#.Net.
First we will create a new excel sheet with some data.
So for this article first I will create a new asp.net
application and add the below code in your 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 have first read the file and saved in a
folder and the I read the excel sheet and stored in dataset and bind it to the
gridview control.
Now we will create the folder.
In this folder we will upload the file as shown in code.
Now we will create the folder.
In this folder we will upload the file as shown in code.
Now we have done run the application and check the output.
0 comments:
Please let me know your view