本文介绍了通过工作表名称通过C#获取Excel工作表参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前使用下面的C#代码获得的句柄Excel工作表:
I'm currently obtaining a handle to a Excel worksheet by using the below C# code:
* Excel.Worksheet工作表=(Excel.Worksheet) sheets.get_Item(15); //获取工作表SubSignOff号*
有什么办法,我可以通过使用工作表中获得相同名字 - SubSignOff
Is there any way that I can obtain the same by using the worksheet name -- "SubSignOff" ?
非常感谢您的帮助。
- ?Chapax
--Chapax
推荐答案
相反,使用的集合,它更容易访问的收集,这样你可以利用早期绑定。
Instead of the using Excel.Workbook.Sheets collection, it's easier to access the Excel.Workbook.Worksheets collection, that way you can utilize early binding.
在你的情况,它可能看起来像下面这样:
In your case, it could look something like the following:
Excel.Application excelApp = new Excel.Application();
excelApp.Visible = true;
Excel.Workbook workbook = excelApp.Workbooks.Open("C:\MyWorkbook.xls",
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
// The key line:
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets["SubSignOff"];
希望这有助于...
Hope this helps...
这篇关于通过工作表名称通过C#获取Excel工作表参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!