0

I am working on reading excel sheet using C#. Here, I get stuck when reading check box control from excel. We need to read whether a checkbox is checked or unchecked.

We are using office open xml to read the excel.

OfficeOpenXml.ExcelWorksheet worksheet=Package.workbook.worksheets["abc"];

var  draw=worksheet.Drawings.SingleOrDefault(a=>a.name ==  "Check Box 45");

using draw.text I am getting checkbox text but I'm not able to read whether it is checked or unchecked.

Thanks,

Thanks, Ashish

3
  • Try casting to ExcelControlCheckBox (or use dynamic). Then read the Checked property. Commented Aug 17, 2021 at 18:14
  • Hi @BenVoigt can you please share how to cast the checkbox? Commented Aug 18, 2021 at 18:23
  • "cast" is a C# feature, like ((ExcelControlCheckBox)draw) Commented Aug 18, 2021 at 19:07

2 Answers 2

0

Link checkbox with some other cell. and store value true/false in that cell based on checkbox check/uncheck.

Here I have linked checkbox with cell[z:1]. when user check i will add value in cell[z:1] as true. if user uncheck, I will add false. after that i will read value from cell[z:1] as like below.

ExcelWorksheet loExcelSheet = excel.Workbook.Worksheets.SingleOrDefault(a => a.Name == "SheetName");
                    ExcelDrawing cbox1 = loExcelSheet.Drawings.SingleOrDefault(a => a.Name == "CBox1");
                   var lsRes = loExcelSheet.Cells["Z1"].Value.ToString();
Sign up to request clarification or add additional context in comments.

4 Comments

Link checkbox with some other cell. -- This solution will not work in my case as end user is going to upload excel file with check box checked or unchecked.
Link checkbox with some other cell - This solution is not suitable for my project. As these excels are generated from other application/other team, so we do not have control over excel. User uploads these files in our application, where these file needs to auto read and insert values in database. We need to read checkbox value without changing the template.
Hi @sujit-patel How it can be cast using open xml or any other solution for it?
@AshishShinde I need to check for the same.
0

You can read the value using OpenXml's control properties.

var control = wsPart.Worksheet.Descendants<DocumentFormat.OpenXml.Spreadsheet.Control>().FirstOrDefault();
        var controlProperies = (ControlPropertiesPart)wsPart.GetPartById(control.Id);
        isChecked = controlProperies.FormControlProperties.Checked == "Checked";

original post reading excel file -> getting checkbox value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.