大佬教程收集整理的这篇文章主要介绍了如何通过c#从asp.net中的excel文件加载gridview?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_764_5@microsoft jet数据库引擎’Sheet1 $’找不到该对象.确保对象存在且其名称拼写正确且路径正确.
并且此行中存在错误:excelDataAdapter.Fill(dt);
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.GetOleDb@R_674_10906@maTable(OleDb@R_674_10906@maGuid.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?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。