读取Excel文件

编程入门 行业动态 更新时间:2024-10-25 06:21:40
读取Excel文件 - 没有给出一个或多个必需参数的值(Reading Excel file - No value given for one or more required parameters)

更新:

更新正确的路径后,我收到的新错误显示"External table is not in the expected format."

我不确定这段代码有什么问题,我正在尝试阅读excel表,下面是我的代码,并收到以下错误。

OleDbConnection oledbConn = new OleDbConnection(); string path = Path.GetFullPath(Server.MapPath("~/InformationNew.xlsx")); oledbConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';"); oledbConn.Open(); OleDbCommand cmd = new OleDbCommand(); OleDbDataAdapter oleda = new OleDbDataAdapter(); DataSet ds = new DataSet(); cmd.Connection = oledbConn; cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT [epic],[desc] FROM [Sheet1$]"; oleda = new OleDbDataAdapter(cmd); oleda.Fill(ds); //<<<ERROR

System.Data.dll中出现“System.Data.OleDb.OleDbException”类型的异常,但未在用户代码中处理

Additional information: No value given for one or more required parameters.

UPDATE:

after updating the correct path, I'm getting the new error which says "External table is not in the expected format."

I'm not sure what is wrong with this code and I'm trying to read the excel sheet and below is my code and getting the following error.

OleDbConnection oledbConn = new OleDbConnection(); string path = Path.GetFullPath(Server.MapPath("~/InformationNew.xlsx")); oledbConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';"); oledbConn.Open(); OleDbCommand cmd = new OleDbCommand(); OleDbDataAdapter oleda = new OleDbDataAdapter(); DataSet ds = new DataSet(); cmd.Connection = oledbConn; cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT [epic],[desc] FROM [Sheet1$]"; oleda = new OleDbDataAdapter(cmd); oleda.Fill(ds); //<<<ERROR

An exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll but was not handled in user code

Additional information: No value given for one or more required parameters.

最满意答案

我以前见过这个。 确保您的电子表格名称正确无误(如果您关闭了一个字符,它将无效),您的列名称是正确的,并且您的路径有效。

在Try ... catch ... finally块中包装您的数据访问,您可能会获得更多的错误数据。

I've seen this before. Make sure your spreadsheet name is correct (if you're off by one character it won't work), your column names are correct, and your path is valid.

Wrap your data access in a Try...catch...finally block and you can probably get more error data.

更多推荐

本文发布于:2023-07-25 10:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1260005.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   Excel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!