简体   繁体   English

C#range.Rows.Count计数不正确-Excel格式?

[英]C# range.Rows.Count doesn't count right - Excel formating?

I'm using C# to get data from Excel. 我正在使用C#从Excel获取数据。

For reading out the Data I use this piece of code: 为了读取数据,我使用以下代码:

for (int rCnt = 2; rCnt <= range.Rows.Count; rCnt++)

The Sheet is 80 rows long, the range.Rows.Count says it is 135 rows long. Sheet的长度为80行,范围为range.Rows.Count表示长度为135行。 I have this problem with 2 Excel Files. 我有2个Excel文件的问题。 The Excel files are generated from an Sharepoint and have filters and some other formating. Excel文件是从Sharepoint生成的,并具有过滤器和某些其他格式。 When I copy the data into an empty Excel file (with Strg + A, not manually selected) it counts the right amount of rows. 当我将数据复制到一个空的Excel文件(使用Strg + A,未手动选择)时,它计算出正确的行数。 With a 3rd Excel file (from an Sharepoint, too) its no problem... 使用第三个Excel文件(也可以从Sharepoint获取),没有问题...

Maybe a solution is to change the excel file first, it is only needed for my programm not for anything else, so that would be ok. 也许一个解决方案是先更改excel文件,这仅对我的程序是必需的,而对于其他任何东西都不需要,所以可以。

Any Ideas? 有任何想法吗?

Edit: 编辑:

I just stopped the code and saw, that after the range of 80, all the entries in the object are "null", so there is no hidden Data or something 我只是停止了代码,发现在80的范围之后,对象中的所有条目均为“ null”,因此没有隐藏的Data或其他内容

Edit2: I deleted all the Data from that Sheet and now it counts 137 rows, so there has to be some formating stuff that is counted... Edit2:我从该工作表中删除了所有数据,现在它统计了137行,因此必须要有一些格式化的东西...

First of all you mention that the excel file has some filtering and formatting. 首先,您提到excel文件具有一些过滤和格式设置。 Could it be that some of the formatting is applied to the first 135 rows in the file and therefore the select returns them all ? 可能是某些格式应用于文件的前135行,因此select返回了全部格式吗? And secondly, what do you use to read the Excel file ? 其次,您用什么来读取Excel文件? Do you use OleDb? 您是否使用OleDb? And are the rest of the returned rows empty ? 其余返回的行是否为空?

If that is the case you can use : SELECT * FROM SHEET WHERE [Column] IS NOT NULL 如果是这种情况,则可以使用:SELECT * FROM SHEET WHERE [Column] IS NOT NULL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM