How to use .net to add checkboxes to an Excel spreadsheet?

1.5k views Asked by At

I have carefully researched this and not found the answer, yet.

I received a task that required me to export some tabular data to Excel in .net. I was pleasantly surprised to find the "ClosedXML" library that does most of what I want with relative ease. However, when I sent my work to my client, he said that he would like for each row of data to have a checkbox next to it. The idea is that the user can check and uncheck the boxes, upload the file to a web server, and then have the web server make changes to a database based on which boxes are checked.

Unfortunately, I cannot find any reference to checkboxes in ClosedXML, and I saw at least one comment in a discussion somewhere saying (without any further explanation) that ClosedXML doesn't do checkboxes.

I tried creating an Excel spreadsheet and putting nothing on it except for a single checkbox. I tried browsing the OpenXML object model and found that there seems to be the following reference to the checkbox:

document.WorkbookPart.WorksheetParts.Single().ControlPropertiesParts.Single().FormControlProperties

Yeah, all that to find a single checkbox, much less consider adding one for every row!

If I have code that is writing rows of data in Excel, how can I add checkboxes to the rows? If I have to use a different library, that's okay.

Update: As @Mike suggested, it is easy to set this up as a drop down menu where the user can pick from two different options using data validation. The code looks like this:

cell.SetValue(item.isOn ? "On" : "Off");
cell.DataValidation.List("\"On,Off\"", true);
1

There are 1 answers

1
Mike On BEST ANSWER

I'm not familiar with ClosedXML, but I think I understand your client's intent.

Instead of offering checkboxes, you could create a blank column that the client can type an 'X' in. You could even use data validation to allow the user to pick the 'X' from a drop-down menu.