c# MVC NPOI导出Excel

这里是根据已经存在的模板往模板里面写数据。然后导出。

示例

     public FileResult GetGenerateExport(string Barcode, Guid? CustomerId, string BranchCode, string Status)
    {
        try
        {
            //模板
            string configurl = System.Configuration.ConfigurationSettings.AppSettings["TempletsPath"].ToString();
            string FileFullPath = Server.MapPath(configurl) + "BranchKey.xls";
            if (!System.IO.File.Exists(FileFullPath))
            {
                return null;
            }

            HSSFWorkbook workbook = new HSSFWorkbook();
            using (FileStream file = new FileStream(FileFullPath, FileMode.Open, FileAccess.Read))
            {
                workbook = (HSSFWorkbook)WorkbookFactory.Create(file);
            }

			 //需要查询的数据 Str
            var model = _IBranchKeyService.GetAllEnt(false).Where(a => a.AtmCustomer != null && a.AtmCustomer.Deleted == false);//客户不能为空,或删除的。

            if (!string.IsNullOrEmpty(Barcode))
            {
                model = model.Where(a => a.Barcode.Contains(Barcode));
            }

            if (CustomerId.HasValue)
            {
                model = model.Where(a => a.CustomerId == CustomerId.Value);
            }

            if (!string.IsNullOrEmpty(BranchCode))
            {
                model = model.Where(a => a.BranchCode.Contains(BranchCode));
            }

            if (!string.IsNullOrEmpty(Status))
            {
                var Enumstatus = (BranchKeyStatus)int.Parse(Status);
                model = model.Where(a => a.Status == Enumstatus);
            }
			
			
            var dataList = model.OrderBy(a => a.CustomerName).ThenBy(a => a.BranchCode).ToList();
            //需要查询的数据 End

            HSSFSheet datasheet = (HSSFSheet)workbook.GetSheetAt(0);
            HSSFRow row = null;
            var indexRow = 4;
            if (dataList.Count > 0)
            {
                int number = 0;
                int SequenceSum = 0;
                string SequenceRouteName = "";
                string SequenceCustomerName = "";

                ICellStyle s = workbook.CreateCellStyle();
                s.FillForegroundColor = HSSFColor.RED.index;
                s.VerticalAlignment = VerticalAlignment.CENTER;
                s.FillPattern = FillPatternType.SOLID_FOREGROUND;
                s.Alignment = HorizontalAlignment.CENTER;
                s.VerticalAlignment = VerticalAlignment.CENTER;

                foreach (var item in dataList)
                {
                    number++;
                    //创建行
                    HSSFRow IndexRow = (HSSFRow)datasheet.GetRow(indexRow);
                    if ((indexRow + 1) < (4 + dataList.Count))
                    {
                        row = (HSSFRow)datasheet.CopyRow(indexRow, indexRow + 1);
                        row.Height = IndexRow.Height;
                    }

                    row = (HSSFRow)datasheet.GetRow(indexRow);
                    row.GetCell(0).SetCellValue(number);
                    row.GetCell(1).SetCellValue(item.Barcode);
                    row.GetCell(2).SetCellValue(item.CustomerName);
                    row.GetCell(3).SetCellValue(item.BranchCode);
                    row.GetCell(4).SetCellValue(item.BranchName);
                    int Countofkey = GetCountOfKey(ref SequenceSum, ref SequenceRouteName, item.BranchCode, ref SequenceCustomerName, item.CustomerName);
                    row.GetCell(5).SetCellValue(Countofkey.ToStr());
                    row.GetCell(6).SetCellValue(item.ServiceType == CustomerTypes.CustomerTypes_Other ? "" : item.ServiceType.GetString().ToStr());

                    row.GetCell(7).SetCellValue(item.Status == BranchKeyStatus.Node ? "" : item.Status.GetString().ToStr());
                    if (item.Status == BranchKeyStatus.Damaged)
                    {
                        ICell mcell = row.GetCell(7);
                        row.GetCell(7).CellStyle = s;
                    }

                    row.GetCell(8).SetCellValue(item.CreatedDate);
                    indexRow++;
                }
            }

            workbook.ForceFormulaRecalculation = true;
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            workbook.Write(ms);
            ms.Seek(0, SeekOrigin.Begin);
            DateTime dt = DateTime.Now;
            string dateTime = dt.ToString("yyMMdd");//yyMMddHHmmssfff
            string fileName = "BranchKey" + dateTime + ".xls";
            return File(ms, "application/vnd.ms-excel", fileName);
        }
        catch (Exception ex)
        {
            throw;
        }
    }

需要下载的DLL
https://download.csdn.net/download/xiongliuyun/20432621

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值