本文介绍了使用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();
这篇关于使用C#在Excel中跳过列总和中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!