C#:MS Excel中checkbox的状态

我试图通过C#获取XLS文档中存在的checkbox的状态。 让我回到这里。 这是我的:

  • MS Office 2007 +开发工具和VC#2010 Express
  • 引用的MS Excel 12.0对象库
  • 一个XLS文件

我成功地检索Excel.Shape对象。 但是,当我试图确定它是否被选中时,我被卡住了。 到目前为止,我已经获得了它的AutoShapeType,它说msoShapeMixed。

有人能指点我正确的方向吗? 谢谢!

class Program { static void Main(string[] args) { Application excel = new Application(); Workbook wb = excel.Workbooks.Open( "document.xls", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value ); Worksheet ws = wb.Worksheets[3]; Microsoft.Office.Interop.Excel.Shape sh = ws.Shapes.Item("checkbox1"); Console.WriteLine("[" + (sh.AutoShapeType.ToString()) + "]"); // msoShapeMixed Console.ReadLine(); } } 

我已经解决了这个问题的帮助下VB和生成类库。 这个库在C#中使用。

VB:

 Option Strict Off Imports Excel = Microsoft.Office.Interop.Excel Public Class CheckboxReader Dim xlApp As Excel.Application = Nothing Dim xlWorkBooks As Excel.Workbooks = Nothing Dim xlWorkBook As Excel.Workbook = Nothing Dim xlWorkSheet As Excel.Worksheet = Nothing Public Sub New(ByVal excelFilename As String, ByVal worksheetName As String) xlApp = New Excel.Application xlApp.DisplayAlerts = False xlWorkBooks = xlApp.Workbooks xlWorkBook = xlWorkBooks.Open(excelFilename) For Each worksheet As Excel.Worksheet In xlWorkBook.Worksheets If worksheet.Name = worksheetName Then xlWorkSheet = worksheet Exit For End If Next End Sub Public Function GetCheckBoxValue(ByVal Name As String) As Boolean Dim found As Boolean = False Dim result As Boolean = False If Not found Then result = xlWorkSheet.OLEObjects(Name).Object.Value() found = True End If Return result End Function End Class 

C#:

 CheckboxReader chr = new CheckboxReader(excelFilename, worksheetName); bool typeFabInstall = chr.GetCheckBoxValue("checkboxName"); 

很好用。 祝你好运!