ASP.net连接Excel的代码

人生坎坎坷坷,跌跌撞撞那是在所难免。但是,不论跌了多少次,你都要坚强地再次站起来。再试一次,成功一定属于你!
首先添加命名空间
 
using System.Data.OleDb;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
strfile = Request.QueryString["filename"];//从其他页面传过来的文件路径
Excel(strfile);
}
}
private void Excel(string filepath)
{
try
{
Dataset ds = new DataSet();
string ConnStr = "Provider=Microsoft.Jet.OleDb.4.0;data source=" + filepath +
";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";//连接Excel的字符串
string query = "SELECT * FROM [student$]";//Excel中的表名称
OleDbCommand oleCommand = new OleDbCommand(query, new OleDbConnection(ConnStr));
OleDbDataAdapter oleAdapter = new OleDbDataAdapter(oleCommand);
oleAdapter.Fill(ds, "[student$]");
rowcount = ds.Tables[0].Rows.Count;
gridview1.DataSource = ds;
gridview1.DataBind();
lblmes.Text = "上传成功,数据如下所示,请确认:";//lblmes为label,显示提示信息
}
catch (OleDbException)
{
string filename = filepath.Substring(filepath.LastIndexOf('/') + 1);
lblmes.Text = "错误!请确认上传文件是否正确!当前上传的文件为:" + filename;
lbtnSure.Visible = false;
}
catch(Exception ee)
{
lblmes.Text = ee.Message;
}
}

asp.net 读取并显示excel数据的实现代码在Asp.net用C#建立动态Excel

asp.net 操作excel的实现代码

以上就是ASP.net连接Excel的代码。一个人,只要知道付出爱与关心,她内心自然会被爱与关心充满。更多关于ASP.net连接Excel的代码请关注haodaima.com其它相关文章!

您可能有感兴趣的文章
ASP.NET轻量级MVC框架Nancy的基本用法

使用grpcui测试ASP.NET core的gRPC服务

ASP.NET Core中的对象池介绍

asp.net中MVC的处理流程详解

ASP.NET Core的日志系统介绍