使用C#在Excel中跳过列总和中的行

问题描述:

我试图通过使用C#中的excel interop跳过特定行来计算列总和.我无法弄清楚该怎么做,请帮忙.我想对突出显示的行进行汇总,并跳过所附图像中的其他行.

I am trying to calculate the column sum by skipping specific rows with excel interop in C#.i am not able to figure out how to do that please help. I want to sum highlighted rows and skip the others in the attached image.

Excel数组索引通常基于1,而不是基于0(即,最低元素为数字1).

Excel array indices are generally 1-based, not 0-based (i.e. lowest element is number 1).

var xl = new Microsoft.Office.Interop.Excel.Application();
var b = xl.Workbooks.Open("C:\\Junk\\Junk.xlsx");
Microsoft.Office.Interop.Excel.Worksheet s = b.Worksheets[1]; //1-based
var r = s.Range["A1", "E4"];
int sum = 0;
for (var row = 1; row <= 4; row += 2)
{
    sum += r.Value2[row, 1]; //also 1-based
}            
xl.Quit();