在不循环遍历每个单元格的情况下,以C#读取Excel文件 [英] Read Excel file in C# without looping through each Cell

查看:288
本文介绍了在不循环遍历每个单元格的情况下,以C#读取Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


谁能告诉我如何在不循环浏览excel文件的每个单元格的情况下读取excel文件的内容.
最主要的是,我应该避免循环遍历每个单元格以读取excel文件的内容.

谢谢&问候,
Ramana

Hi,
Can anyone tell me how to read the content of an excel file without looping through each cell of the excel file.
The main thing is that I should avoid looping through each cell for reading the content of the excel file.

Thanks & Regards,
Ramana

推荐答案

您可以使用OleDb连接到工作簿并将数据读取到DataSet

http://forums.asp.net/t/1255191.aspx/1?从+ Excel + to + a + datatable + C + 中读取+数据+ [ ^ ]
You could use OleDb to connect to the workbook and read the data into a DataSet

http://forums.asp.net/t/1255191.aspx/1?Read+data+from+Excel+to+a+datatable+C+[^]


将调查 ADO.Net [
Will looking into the ADO.Net[^] angle help?


据我所知,没有办法在没有单元循环的情况下读取它.考虑一下,如果您不阅读这些单元格,那么您将如何获得其价值.

但我可以建议您一种减少循环的方法.
您可以使用工作表的UsedRange属性,该属性将返回您的单元格具有值的范围,因此您不必阅读整个工作表就可以找到值.

As far as I know, there is no way to read it "without" looping through the cells. Think about it, if you don''t read the cells, then how are you going to get its value.

But I can suggest you a way to reduce the looping.
You can use the UsedRange property of the Sheet which will return you the range where your cells have values, so that you don''t have to read the whole sheet searching for values.

Using xl = Microsoft.Office.Interop.Excel;
private xl.Application xlApp;

xlApp = Globals.ThisAddin.Application;
xl.WorkBook workbook = xlApp.ActiveWorkbook;  //Get the active workbook object
xl.WorkSheet worksheet = workbook.ActiveSheet;  //Get the active worksheet object
xl.Range usedRange = worksheet.UsedRange;  //This will return the whole range where you data is used in it
String value;
// Loop through the range to get data of each cell
foreach(xl.Range rng in usedRange.Cells)
{
 value = rng.Value;
}


这篇关于在不循环遍历每个单元格的情况下,以C#读取Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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