如何通过c#从asp.net中的excel文件加载gridview?

如何通过c#从asp.net中的excel文件加载gridview?,第1张

概述我尝试从excel文件加载gridview.但是我得到了这个错误 Microsoft Jet数据库引擎’Sheet1 $’找不到该对象.确保对象存在且其名称拼写正确且路径正确. 并且此行中存在错误:excelDataAdapter.Fill(dt); 我在App_Data文件夹中有一个.xls文件 using System;using System.Collections.Generic;us 我尝试从excel文件加载grIDvIEw.但是我得到了这个错误

Microsoft Jet数据库引擎’Sheet1 $’找不到该对象.确保对象存在且其名称拼写正确且路径正确.

并且此行中存在错误:excelDataAdapter.Fill(dt);

我在App_Data文件夹中有一个.xls文件

using System;using System.Collections.Generic;using System.linq;using System.Web;using System.Web.UI;using System.IO;using System.Data;using System.Data.oleDb;namespace Excell{public partial class LoadExcelToGrID: System.Web.UI.Page{    protected voID Page_Load(object sender,EventArgs e)    {       gv.DataSource = exceldata(Server.MapPath("~/data.xls"));       gv.DataBind();    }    public static DataSet exceldata(string filelocation)    {        DataSet ds = new DataSet();        oleDbCommand excelCommand = new oleDbCommand(); oleDbDataAdapter excelDataAdapter = new  oleDbDataAdapter();        string excelConnStr = "ProvIDer=Microsoft.Jet.olEDB.4.0; Data Source=" + filelocation + "; Extended PropertIEs=Excel 8.0;";        oleDbConnection excelConn = new oleDbConnection(excelConnStr);        excelConn.open();        Datatable dt = new Datatable();        excelCommand = new oleDbCommand("Select * from [Sheet1$]",excelConn);        excelDataAdapter.SelectCommand = excelCommand;        excelDataAdapter.Fill(dt);        ds.tables.Add(dt);        return ds;    }}}
解决方法 本规范适用于我.

protected voID btnUpload_Click(object sender,EventArgs e){String strConnection = "ConnectionString";string connectionString ="";if (fileUpload1.Hasfile){    string filename = Path.Getfilename(fileUpload1.Postedfile.filename);    string fileExtension = Path.GetExtension(fileUpload1.Postedfile.filename);    string fileLocation = Server.MapPath("~/App_Data/" + filename);    fileUpload1.SaveAs(fileLocation);     if (fileExtension == ".xls")    {        connectionString = "ProvIDer=Microsoft.Jet.olEDB.4.0;Data Source=" +           fileLocation + ";Extended PropertIEs=\"Excel 8.0;HDR=Yes;IMEX=2\"";     }    else if (fileExtension == ".xlsx")    {        connectionString = "ProvIDer=Microsoft.ACE.olEDB.12.0;Data Source=" +           fileLocation + ";Extended PropertIEs=\"Excel 12.0;HDR=Yes;IMEX=2\"";    }     oleDbConnection con = new oleDbConnection(connectionString);    oleDbCommand cmd = new oleDbCommand();    cmd.CommandType = System.Data.CommandType.Text;    cmd.Connection = con;    oleDbDataAdapter dAdapter = new oleDbDataAdapter(cmd);    Datatable dtExcelRecords = new Datatable();    con.open();    Datatable dtExcelSheetname = con.GetoleDbSchematable(oleDbSchemaGuID.tables,null);    string getExcelSheetname = dtExcelSheetname.Rows[0]["table_name"].ToString();    cmd.CommandText = "SELECT * FROM [" + getExcelSheetname +"]";    dAdapter.SelectCommand = cmd;    dAdapter.Fill(dtExcelRecords);     GrIDVIEw1.DataSource = dtExcelRecords;    GrIDVIEw1.DataBind(); }
总结

以上是内存溢出为你收集整理的如何通过c#从asp.net中的excel文件加载gridview?全部内容,希望文章能够帮你解决如何通过c#从asp.net中的excel文件加载gridview?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1218332.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存