Wednesday, 22 February 2012

How To Read Excel File and Display Data In GridView

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ReadExcel.aspx.cs"
 Inherits="ReadExcel" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<html xmlns="" >
<head runat="server">
  <title>Untitled Page</title>
  <form id="form1" runat="server">
      <asp:GridView ID="GridView1" runat="server">

      <asp:Button ID="btnRead" runat="server" 
Text="Read" OnClick="btnRead_Click" />
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Diagnostics;

public partial class ReadExcel : System.Web.UI.Page
  protected void Page_Load(object sender, EventArgs e)

  protected void btnRead_Click(object sender, EventArgs e)
          OleDbConnection con = 
new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\Test.xls;
Extended Properties=Excel 8.0");
          OleDbDataAdapter da =
new OleDbDataAdapter("select *from  [Sheet1$]", con);
          DataSet ds = new DataSet();
          GridView1.DataSource = ds;
      catch(Exception ex)

          StackTrace trace = new StackTrace(ex,true);
          System.Text.StringBuilder sb = new System.Text.StringBuilder();
          sb.Append("Line: " + trace.GetFrame(0).GetFileLineNumber());
          sb.Append("Column: " + trace.GetFrame(0).GetFileColumnNumber());

Post a Comment