<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ReadExcel.aspx.cs"
Inherits="ReadExcel" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
<asp:Button ID="btnRead" runat="server"
Text="Read" OnClick="btnRead_Click" />
</form>
</body>
</html>
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)
{
try
{
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();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch(Exception ex)
{
StackTrace trace = new StackTrace(ex,true);
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(trace.GetFrame(0).GetMethod().Name);
sb.Append("Line: " + trace.GetFrame(0).GetFileLineNumber());
sb.Append("Column: " + trace.GetFrame(0).GetFileColumnNumber());
Response.Write(sb.ToString());
}
}
}
No comments :
Post a Comment