C#Excel Interop计数可见行

时间:2017-03-08 13:39:52

标签: c# excel interop

如何获取可见行的总数?

我尝试了以下内容:
1)

setsebool -P httpd_read_user_content 1
执行后

Excel.Range range; range = ws.UsedRange; int rCnt = 0; rCnt = range.Rows.Count;

2)

rCnt = 1
执行后

var countRows = ws.Rows.Count;

countRows = 1048576rCnt都可以返回countRows - >可见的总行数

1 个答案:

答案 0 :(得分:0)

使用VisibleRange

  

返回一个Range对象,该对象表示单元格的范围   在窗口或窗格中可见。如果列或行是部分的   可见,它包含在范围内。

例如,定义了

var xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
    Console.WriteLine("EXCEL could not be started. Check that your office installation and project references are correct.");
    return;
}
xlApp.Visible = true;

var wb = xlApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
var ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[1];

然后你可以做

var test = xlApp.ActiveWindow.VisibleRange.Rows.Count;