Excelファイルからグリッドビューを読み込もうとしましたが、このエラーが発生しました
Microsoft Jet データベース エンジン 'Sheet1 $' はオブジェクトを見つけることができませんでした。オブジェクトが存在し、その名前とパスのスペルが正しいことを確認してください。
そして、この行にエラーがあります: excelDataAdapter.Fill(dt);
App_Data フォルダーに .xls ファイルがあります。Wh
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;
}
}
}