Create a dropdownlist of checkboxes using EPPlus

泪湿孤枕 提交于 2019-12-11 08:56:43

问题


I am using EPPlus (http://epplus.codeplex.com/) to create Excel files in my ASP.NET application. I was wondering if anyone knows if it's possible to create a dropdownlist with checkboxes in one cell. I've checked the documentation, but found nothing so far. I've also tried Googling it but haven't found anything in the right direction yet.

This question on their forum is actually a pretty good demonstration of what I'm looking for, but it hasn't received any answers: http://epplus.codeplex.com/discussions/585879

Does anyone happen to have any ideas and can point me in the right direction?


回答1:


You can use:

var validationCell = sheet.DataValidations.AddListValidation("A1");
validationCell.Formula.Values.Add("a");
validationCell.Formula.Values.Add("b");
validationCell.Formula.Values.Add("c");
...

but you can choose only one single value. I think that multiple values are not supported by excel.




回答2:


For others reference, here is how I could enable multiple values select:

By inserting VBA Code:

see how to do it in here , using EPPlus you can insert VBA code using something like:

package.Workbook.CreateVBAProject();
worksheet.CodeModule.Code = code;

This following code did the trick:

using (var package = new ExcelPackage(new System.IO.FileInfo("D:\\b.xlsm")))
        {
            ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("11");
            var validationCell = worksheet.DataValidations.AddListValidation("A1");
            validationCell.Formula.Values.Add("a");
            validationCell.Formula.Values.Add("b");
            validationCell.Formula.Values.Add("c");

            string code = "Private Sub Worksheet_Change(ByVal Target As Range)\n" +
"Dim Oldvalue As String\n" +
"Dim Newvalue As String\n" +
"Application.EnableEvents = True\n" +
"On Error GoTo Exitsub\n" +
"If Target.Address = \"$A$1\" Then\n" +
 " If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then\n" +
  "  GoTo Exitsub\n" +
  "Else: If Target.Value = \"\" Then GoTo Exitsub Else\n" +
   " Application.EnableEvents = False\n" +
    "Newvalue = Target.Value\n" +
    "Application.Undo\n" +
    "Oldvalue = Target.Value\n" +
    "  If Oldvalue = \"\" Then\n" +
     "   Target.Value = Newvalue\n" +
      "Else\n" +
       " If InStr(1, Oldvalue, Newvalue) = 0 Then\n" +
        "    Target.Value = Oldvalue & \", \" & Newvalue\n" +
      "Else: \n" +
       "         Target.Value = Oldvalue\n" +
      "End If\n" +
    "End If\n" +
  "End If\n" +
 "End If\n" +
"Application.EnableEvents = True\n" +
"Exitsub: \n" +
 "  Application.EnableEvents = True\n" +
"End Sub";

            package.Workbook.CreateVBAProject();
            worksheet.CodeModule.Code = code;
            package.Save();
        }


来源:https://stackoverflow.com/questions/31050195/create-a-dropdownlist-of-checkboxes-using-epplus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!