c#: MS Excel中复选框的状态
本文关键字:复选框 状态 Excel MS | 更新日期: 2023-09-27 18:12:43
我试图通过c#获取XLS文档中存在的复选框的状态。让我回到这里。这是我的文件:
- MS Office 2007 + Dev Tools and vc# 2010 Express
- 参考MS Excel 12.0对象库
- XLS文件
我成功检索Excel。形状的物体。然而,当我试图确定它是否被检查时,我被卡住了。到目前为止,我已经获得了它的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");
伟大的工作。好运!