如何使用OpenXML SDK访问Excel工作表中的FormControl复选框 [英] How to access a FormControl checkbox in an Excel sheet using OpenXML SDK

查看:95
本文介绍了如何使用OpenXML SDK访问Excel工作表中的FormControl复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个电子表格,该电子表格在工作表的各个单元格中都有许多复选框,我需要从c#程序中获取(选中/未选中)的值.

I have a spreadsheet that has a number of check boxes in various cells on the sheet that I need to get the value of (checked/unchecked) from within a c# program.

我正在使用OpenXML SDK v2.5和关联的工具箱.

I'm using the OpenXML SDK v2.5 and the associated toolbox.

使用工具箱,我可以看到复选框控件是AlternateControlParts集合的一部分.这些不是ActiveX复选框,而是通过Excel中的开发人员"选项卡添加的表单控件.

Using the toolbox I can see the check box controls as part of the AlternateControlParts collection. These are not ActiveX checkboxes but are form controls added via the developer tab in Excel.

当我使用SDK时,我还可以看到WorkSheetPart,它上面有一个ControlPropertiesParts集合,其中列出了所有复选框.

When I use the SDK I can also see the WorkSheetPart which has a ControlPropertiesParts collection on it which lists all the checkboxes.

我的问题是,如何找到哪个复选框位于哪个单元格中,或者至少与哪个单元格相关?

My problem is, how do I find which checkbox is in which cell or at least related to which cell?

我也找到了收藏wsPart.ControlPropertiesParts.First().DrawingsPart.WorkSheetDrawing.DrawingsPart.WorkSheetDrawing

I have also found the collection wsPart.ControlPropertiesParts.First().DrawingsPart .WorkSheetDrawing.DrawingsPart.WorkSheetDrawing

此集合似乎具有每个复选框的替代内容,如果我进一步进行深入研究,我可以找到锚点,这些锚点似乎提供了复选框相对于工作表中单元格的位置.但是,列ID和行ID似乎不完全匹配,我怀疑偏移"值也可能与此有关.

This collection appears to have the alternate content of each of the checkboxes and if I drill down further I can find the anchor points which appear to give the location of the checkboxes relative to the cells on the sheet. However, the col and row Id’s don’t appear to exactly match up and I suspect that the Offset values may also have something to do with it.

如果有人可以向我指出如何将复选框映射到正确的行/单元格,我将不胜感激.

If someone can point me in the right direction on how to map the checkboxes to the correct row/cells I would be very grateful.

谢谢您的帮助.

问候保罗

推荐答案

我有一个解决方案,它只包含逻辑(属性 FormControlProperties 自Office 2010起可用:

I have a solution, it contains only the logic (The property FormControlProperties is available since Office 2010:

        SpreadsheetDocument document;
        string sheetName = "sheetName";
        string controlName = "Option Button 5";
        ...
        var wbPart = document.WorkbookPart;
        var theSheet = wbPart.Workbook.Descendants<Sheet>().FirstOrDefault(s => s.Name == sheetName);
        var wsPart = (WorksheetPart)wbPart.GetPartById(theSheet.Id);
        var control = wsPart.Worksheet.Descendants<DocumentFormat.OpenXml.Spreadsheet.Control>().FirstOrDefault(c => c.Name == controlName);
        var controlProperies = (ControlPropertiesPart)wsPart.GetPartById(control.Id);
        bool isChecked = controlProperies.FormControlProperties.Checked == "Checked";

但是,如果可以编辑excel文件,则将FormControl值映射到单元格并读取该单元格值会更简单.

But it is simplier to map the FormControl value to a cell and read the cell value if the you can edit the excel file.

这篇关于如何使用OpenXML SDK访问Excel工作表中的FormControl复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆