本文介绍了Excel worksheetfunction.subtotal方法在C#中引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 //Referencing Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.WorksheetFunction xlWsf; //declaring a range xlRangeDB = sheet.get_Range("A1", "A1000"); //declaring a double variable double numVal = 0; //using subtoal function but throws error!!! numVal = xlWsf.Subtotal(3,xlRangeDB); 我尝试了什么: 搜索google,codeproject网站。What I have tried:Searched google, codeproject website.推荐答案 尝试下面的代码来计算总和 xlRangeDB = sheet.get_Range(A1,A1000); double numVal = 0 ; int rCnt = 0; int cCnt = 0; for(rCnt = 1; rCnt< = xlRangeDB.Rows.Count ; rCnt ++) { for(cCnt = 1; cCnt< = xlRangeDB.Columns.Count; cCnt ++) { numVal = numVal +(double)(xlRangeDB.Cells [rCnt,cCnt] as Excel.Range).Value2; } } Try below code to calculate the sum xlRangeDB = sheet.get_Range("A1", "A1000"); double numVal = 0; int rCnt = 0; int cCnt = 0;for (rCnt = 1; rCnt <= xlRangeDB.Rows.Count; rCnt++) { for (cCnt = 1; cCnt <= xlRangeDB.Columns.Count; cCnt++) { numVal = numVal + (double)(xlRangeDB.Cells[rCnt, cCnt] as Excel.Range).Value2; } } Microsoft.Office.Interop.Excel.WorksheetFunction xlWsf = xlApp.WorksheetFunction; 唯一的问题是没有启动变量。 我非常感谢你们。 感谢天才Ryan :-) 感谢Madhav :-)你的替代解决方案也在工作。The only problem was not initiating variable.I am really grateful to you guys.Thanks genius Ryan :-)Thanks Madhav :-) your alternate solution also working. 这篇关于Excel worksheetfunction.subtotal方法在C#中引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-27 04:19