【发布时间】:2019-02-05 03:12:57
【问题描述】:
我正在使用以下代码将 Excel 文件数据导入网格视图。但我收到一个错误。
protected void uploadLinkButton_Click(object sender, EventArgs e)
{
string name = "Items";
string path = Server.MapPath(StyleOperationsFileUpload.FileName);
string Constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=2;'";
OleDbConnection OleCon = new OleDbConnection(Constr);
OleDbCommand OleCom = new OleDbCommand("Select * From [" + name + "$]", OleCon);
OleCon.Open();
OleDbDataAdapter OleAdapObj = new OleDbDataAdapter(OleCom);
DataTable DatTabObj = new DataTable();
OleAdap.Fill(DatTab);
UploadGridView.DataSource = DatTab;
UploadGridView.DataBind();
}
错误如下
Microsoft Access 数据库引擎找不到对象 '项目$'。确保对象存在并且您拼写了它的名称和 路径名正确。如果 'Items$' 不是本地对象,请检查您的 网络连接或联系服务器管理员。
完整的堆栈跟踪和错误如下
System.Data.OleDb.OleDbException was unhandled by user code
ErrorCode=-2147217865
HResult=-2147217865
Message=The Microsoft Access database engine could not find the object 'Items$'. Make sure the object exists and that you spell its name and the path name correctly. If 'Items$' is not a local object, check your network connection or contact the server administrator.
Source=Microsoft Access Database Engine
StackTrace:
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.OleDb.OleDbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
at StyleOperations.Operations.uploadLinkButton_Click(Object sender, EventArgs e) in D:\Developments\On Going Developments\StyleOperations\StyleOperations\StyleOperations\Operations.aspx.cs:line 145
at System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e)
at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
请帮我解决这个问题。先感谢您。
以下是excel文件的格式。 excel文件的名字是Sheet1
| 1 |DESCRIPTION| SMV |SEQ NO
| 2 | Des1 | 1.2 | 1
| 3 | Des2 | 2.5 | 2
| 4 | Des3 | 5.8 | 3
| 5 | Des4 | 4.2 | 4
【问题讨论】:
-
缺少什么? excel长什么样子?
-
@JanAndersen 我已经编辑了问题并添加了 Excel 文件的格式。请看一下。
-
您是否将默认
sheet1重命名为Items或为Item创建了新工作表? -
^ 因为,否则您需要将分配给 name 的值更改为: string name = "Sheet1";
-
不,我正在将代码中的名称 Sheet1 修改为 Items。即使我没有修改名称,我也遇到了同样的错误。只是名称 Item 已错误地替换为 Sheet1 :D