C# - 如何迭代Excel._Worksheet中的所有行? [英] C# - How do I iterate all the rows in Excel._Worksheet?

查看:788
本文介绍了C# - 如何迭代Excel._Worksheet中的所有行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以编程方式从Excel工作表中提取数据并将其插入到数据库表中。



如何确定工作表中的列和行的数量或以其他方式迭代行?



我有

  Excel._Worksheet工作表=(Excel._Worksheet )workbook.ActiveSheet;我试过 worksheet.Range.Rows.Count  



折腾


索引属性Microsoft.Office.Interop。 Excel._Worksheet.Range'有
必须提供的非可选参数


需要做什么? p>

解决方案

我假定你实际上正在寻找最后一行。在这种情况下,您需要这样写:

  Range UsedRange = worksheet.UsedRange; 
int lastUsedRow = UsedRange.Row + UsedRange.Rows.Count - 1;


I am looking to programmatically pull data from an Excel worksheet and insert it into a database table.

How do I determine the number of columns and rows in a worksheet or otherwise iterate the rows?

I have

Excel._Worksheet worksheet = (Excel._Worksheet)workbook.ActiveSheet;

I tried worksheet.Range.Rows.Count

which tosses up

Indexed property 'Microsoft.Office.Interop.Excel._Worksheet.Range' has non-optional arguments which must be provided

What needs to be done?

解决方案

I presume you are actually looking for the last used row. In that case you need to write it like this:

Range UsedRange = worksheet.UsedRange;
int lastUsedRow = UsedRange.Row + UsedRange.Rows.Count - 1;

这篇关于C# - 如何迭代Excel._Worksheet中的所有行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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