无法将int类型转换为microsoft.office.interop.excel.range [英] Cannot convert type int to microsoft.office.interop.excel.range
本文介绍了无法将int类型转换为microsoft.office.interop.excel.range的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我试图数数. Excel工作表使用范围内的行数
我的代码如下
Hi All,
I am trying to count the no. of rows in the used range of an excel sheet
My code is as follow
int lastrow1;
string path = textBox1.Text;
string path1 = textBox2.Text;
string Filename = path.Substring(path.LastIndexOf(''\\'') + 1);
string Filename1 = path1.Substring(path.LastIndexOf(''\\'') + 1);
Microsoft.Office.Interop.Excel.Application ExcelObj = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(path, 0, true,6, "", "",false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "|", true, false, 0, true, false, false);
Microsoft.Office.Interop.Excel.Sheets sheets = theWorkbook.Worksheets;
Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1);
lastrow1 = (Microsoft.Office.Interop.Excel.Range)workSheet.UsedRange.Rows.Count;
此时,我得到一个错误.
"Cannot convert type int to microsoft.office.interop.excel.range
"
请帮助
At this point i get an error.
"Cannot convert type int to microsoft.office.interop.excel.range
"
Please help
推荐答案
您将lastRow1声明为int,然后尝试将其显式转换为Excel Range
You declare lastRow1 to be an int, and then you try to explicitly cast it to a Excel Range
lastrow1 = (Microsoft.Office.Interop.Excel.Range)workSheet.UsedRange.Rows.Count;
当然,您可以在这里看到错误.错误告诉您确切的地方是什么.认真地-读取错误.
Surely you can see the error here. The error is telling you exactly what is wrong. Seriously - read the error.
没有必要将其强制转换为Range类型.删除演员表.
可以做到:
There is no need to cast it to type Range. Remove the cast.
This will do :
lastrow1 = workSheet.UsedRange.Rows.Count;
这篇关于无法将int类型转换为microsoft.office.interop.excel.range的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文