查询从excel表读取数据在c# [英] query for reading data from excel sheet in c#

查看:201
本文介绍了查询从excel表读取数据在c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢 Astander 回复我的查询



我在这里提供更详细的查询。

  string cs =Provider = Microsoft.ACE.OLEDB.12.0; +Data Source =+ @D:\\sample.xls; +Excel 12.0; HDR = YES;; 
OleDbConnection Excelcon = new OleDbConnection(cs);
OleDbDataAdapter ad = new OleDbDataAdapter();
ad.SelectCommand = new OleDbCommand(SELECT * FROM [Sheet1 $],Excelcon);
DataTable dt = new DataTable();
ad.Fill(dt);
return dt;

我在select语句中收到错误:


Microsoft Office Access数据库引擎找不到对象Sheet1 $。确保对象存在,并且您正确拼写其名称和路径名。


希望有人可以帮助我找到一个解决方案。

解决方案

对我有用的是,
当文件创建时,它存储在某个特定的位置。在我的情况下, C:/ Documents



我手动将位置更改为D:
这是我写的


string connStringExcel = @Provider = Microsoft.ACE.OLEDB.12.0; Data Source = em> D:\example.xls ;扩展属性=Excel 12.0; HDR = YES;;`


所以,实际路径应该是


string connStringExcel = @Provider = Microsoft.ACE.OLEDB。 12.0;数据源= C:\A\Documents\example.xls;扩展属性=Excel 12.0; HDR = YES;;`


所以给出正确位置的路径,我的查询被解决了。



希望它帮助某人els e。


Thanks Astander for replying to my query

I am here with more detailed query.

        string cs = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + @"D:\\sample.xls;" + "Excel 12.0;HDR=YES;";
        OleDbConnection Excelcon = new OleDbConnection(cs);
        OleDbDataAdapter ad = new OleDbDataAdapter();
        ad.SelectCommand = new OleDbCommand("SELECT *FROM [Sheet1$]", Excelcon);
        DataTable dt = new DataTable();
        ad.Fill(dt);
        return dt;

I am getting error at the select statement that :

The Microsoft Office Access database engine could not find the object 'Sheet1$'. Make sure the object exists and that you spell its name and the path name correctly.

Hope someone can help me find a solution.

解决方案

What worked for me is, when file was created, it was stored in some specific location. In my case,C:/Documents.

I had manually changed the location to D: this was what I had written

string connStringExcel = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=D:\example.xls;Extended Properties=""Excel 12.0;HDR=YES;""";`

So,the actual path should be

string connStringExcel = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\A\Documents\example.xls;Extended Properties=""Excel 12.0;HDR=YES;""";`

So on giving the path of correct location,my query was solved.

Hope it helps someone else too.

这篇关于查询从excel表读取数据在c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆