c# npoi创建数据有效性约束制定列和单元格分别设置

  public ActionResult Export()
        {

            var codes 
            string templateFileName = Path.Combine(Server.MapPath("/ExcelTemplate"), "Auto.xlsx");
           

            var fileName = @"Auto.xlsx";

            ExportExcelXlsxHelper ex = new ExportExcelXlsxHelper();
            ex.InitEmptyExcel(); // 初始化后, 可以在外部引用excelWorkbook, currentSheet
            var excelWorkbook = ex.excelWorkbook;
            using (FileStream file = new FileStream(templateFileName, FileMode.Open, FileAccess.Read))
            {
                excelWorkbook = new XSSFWorkbook(file);
              
                file.Close();
            }
            var currentSheet = (XSSFSheet)excelWorkbook.GetSheet("Sheet1");
            ISheet sheet2 = (XSSFSheet)excelWorkbook.GetSheet("options");
            // 创建数据有效性约束并应用于单元格范围
            XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(currentSheet);
            var logisticCompany = codes.Where(o => o.TypeId == 77).Select(o => o.TextValue).ToArray();
            if (logisticCompany != null && logisticCompany.Length != 0)
            {

                CellRangeAddressList rangeLogisticCompany = new CellRangeAddressList(1, 100, 1, 1); // 列1,行1到100
                XSSFDataValidationConstraint constraintLogisticCompany = (XSSFDataValidationConstraint)
                    dvHelper.CreateExplicitListConstraint(logisticCompany);

                XSSFDataValidation dataValidationLogisticCompany = (XSSFDataValidation)
                    dvHelper.CreateValidation(constraintLogisticCompany, rangeLogisticCompany);

                dataValidationLogisticCompany.ShowErrorBox = true; // 如果需要显示错误提示框

                currentSheet.AddValidationData(dataValidationLogisticCompany);

            }
            var from = codes.ToArray();
            var fromNo =ToArray();
            if (from != null && from.Length != 0)
            {
                for (int rowIndex = 0; rowIndex < from.Length; rowIndex++)
                {
                    IRow row = sheet2.GetRow(rowIndex);
                    if (row == null)
                    {
                        row = sheet2.CreateRow(rowIndex);
                    }
                    ICell cell = row.GetCell(4); // 指定列索引
                    if (cell == null)
                    {
                        cell = row.CreateCell(4);
                    }
                    // 设置单元格的值
                    cell.SetCellValue(from[rowIndex]);


                }
            }
            if (fromNo!= null && fromNo.Length != 0)
            {
                for (int rowIndex = 0; rowIndex < fromNo.Length; rowIndex++)
                {
                    IRow row = sheet2.GetRow(rowIndex);
                    if (row == null)
                    {
                        row = sheet2.CreateRow(rowIndex);
                    }
                    ICell cell = row.GetCell(5); // 指定列索引
                    if (cell == null)
                    {
                        cell = row.CreateCell(5);
                    }
                    // 设置单元格的值
                    cell.SetCellValue(fromNo[rowIndex]);
                }
            }

            for (int i = 1; i <= 100; i++)
            {
                var j = i+ 1;
                string formula = "IF($A$"+ j + " = \"OverSea\", 'options'!$E$1:$E$" + from.Length+ ",'options'!$F$1:$F$" + fromNo.Length+" )";

                CellRangeAddressList rangeTo = new CellRangeAddressList(i, i, 4, 4); // 列1,行1到100
                XSSFDataValidationConstraint constraintTo = (XSSFDataValidationConstraint)
                    dvHelper.CreateFormulaListConstraint(formula); 

                XSSFDataValidation dataValidationTo = (XSSFDataValidation)
                    dvHelper.CreateValidation(constraintTo, rangeTo);

                dataValidationTo.ShowErrorBox = true; // 如果需要显示错误提示框
                currentSheet.AddValidationData(dataValidationTo);
            }
            var toOversea = codes.Where(o => o.TypeId == 78).Select(o =>o.TextValue).ToArray();
            var toNoOversea=Business.WareHouse.LoadWareHouse().GroupBy(o => o.City).Select(group => group.First().City).Distinct().Where(o=>!string.IsNullOrEmpty(o)).ToArray();
            if (toOversea != null && toOversea.Length != 0)
            {
                for (int rowIndex = 0; rowIndex < toOversea.Length; rowIndex++)
                {
                    IRow row = sheet2.GetRow(rowIndex);
                    if (row == null)
                    {
                        row = sheet2.CreateRow(rowIndex);
                    }
                    ICell cell = row.GetCell(6); // 指定列索引
                    if (cell == null)
                    {
                        cell = row.CreateCell(6);
                    }
                    // 设置单元格的值
                    cell.SetCellValue(toOversea[rowIndex]);


                }
            }
            if (toNoOversea != null && toNoOversea.Length != 0)
            {
                for (int rowIndex = 0; rowIndex < toNoOversea.Length; rowIndex++)
                {
                    IRow row = sheet2.GetRow(rowIndex);
                    if (row == null)
                    {
                        row = sheet2.CreateRow(rowIndex);
                    }
                    ICell cell = row.GetCell(7); // 指定列索引
                    if (cell == null)
                    {
                        cell = row.CreateCell(7);
                    }
                    // 设置单元格的值
                    cell.SetCellValue(toNoOversea[rowIndex]);
                }
            }

            for (int i = 1; i <= 100; i++)
            {
                var j = i + 1;
                string formula = "IF($A$" + j + " = \"OverSea\", 'options'!$G$1:$G$" + toOversea.Length + ",'options'!$H$1:$H$" + toNoOversea.Length + " )";

                CellRangeAddressList rangeTo = new CellRangeAddressList(i, i, 5, 5); // 列1,行1到100
                XSSFDataValidationConstraint constraintTo = (XSSFDataValidationConstraint)
                    dvHelper.CreateFormulaListConstraint(formula);

                XSSFDataValidation dataValidationTo = (XSSFDataValidation)
                    dvHelper.CreateValidation(constraintTo, rangeTo);

                dataValidationTo.ShowErrorBox = true; // 如果需要显示错误提示框
                currentSheet.AddValidationData(dataValidationTo);
            }

            ExportExcelXlsxHelper.NPOIMemoryStream ms = new ExportExcelXlsxHelper.NPOIMemoryStream();
            excelWorkbook.Write(ms);
            ex.DownLoad(fileName, ms);
            ms = null;
            excelWorkbook.Clear();
            excelWorkbook.Close();

            GC.Collect();

            return new EmptyResult();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

葩熊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值