コード例 #1
0
        static void AddToValidations(ICollection <DataValidation> validations, XSSFSheet sheet, string[] names)
        {
            CT_DataValidations dataValidations = ((CT_Worksheet)sheet.GetType()
                                                  .GetField("worksheet", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)
                                                  .GetValue(sheet)).dataValidations;

            if (dataValidations == null)
            {
                return;
            }

            foreach (CT_DataValidation dataValidation in dataValidations.dataValidation)
            {
                if (dataValidation.formula1 == null)
                {
                    continue;
                }

                var range = ExcelHelper.ParseRange(dataValidation.sqref, ExcelVersion.Xlsx);
                if (range == null)
                {
                    continue;
                }

                var validation = new DataValidation {
                    Range = range
                };

                if (names.Contains(dataValidation.formula1))
                {
                    validation.Type = DataValidationType.Formula;
                    validation.Name = dataValidation.formula1;
                }
                else
                {
                    validation.Type = DataValidationType.List;
                    validation.List = dataValidation.formula1.Trim('\"').Split(',');
                }

                validations.Add(validation);
            }
        }