C# NPOI导出excel

简介:

作为一个刚刚入门的小白,在这里整理一些自己写过的代码,因为时间比较久远大多数的代码没有能找到出处,如果有用过你们代码,请联系我QQ:1206062480;我会表明出处。

NPOI导出excel

public ActionResult ExportExcelRun(DeviceOperationRecordSearch searchParms)
        {
            try
            {
                #region 获取数据
                var db = new Context();

                IQueryable<DeviceOperationRecord> deviceQuery = db.DeviceOperationRecord.Include(p => p.Device);
                if (!searchParms.DeviceNo.IsNullOrEmpty())
                {
                    deviceQuery = deviceQuery.Where(p => p.Device.DeviceNo == searchParms.DeviceNo);
                }
                if (searchParms.StartTime != null)
                {
                    deviceQuery = deviceQuery.Where(p => p.StartTime >= searchParms.StartTime);
                }
                if (searchParms.EndTime != null)
                {
                    deviceQuery = deviceQuery.Where(p => p.EndTime <= searchParms.EndTime);
                }
                deviceQuery = deviceQuery.OrderByDescending(x => x.StartTime);

                var resList = deviceQuery.ToList();

                #endregion

                IWorkbook wb;
                var path = AppDomain.CurrentDomain.BaseDirectory + @"Export\";
                var dif = new DirectoryInfo(path);
                if (!dif.Exists)
                    dif.Create();
                var name = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
                string savePath = path + name;
                string extension = Path.GetExtension(savePath);

                //根据指定的文件格式创建对应的类
                string excelTempPath = AppDomain.CurrentDomain.BaseDirectory + @"File\Excel.xlsx";
                //读取Excel模板
                using (FileStream fs = new FileStream(excelTempPath, FileMode.Open, FileAccess.Read))
                {
                    //根据指定的文件格式创建对应的类
                    if (extension.Equals(".xls"))
                    {
                        wb = new HSSFWorkbook(fs);
                    }
                    else
                    {
                        wb = new XSSFWorkbook(fs);
                    }
                }

                ISheet sheet1 = wb.GetSheet("Run");
                for (int i = 0; i < resList.Count; i++)
                {
                    //for (int j = 1; j < 5; j++)
                    //{
                    //    if (sheet1.GetRow(i + 3) == null || sheet1.GetRow(i + 3).ToString() == string.Empty)
                    //    {
                    //        ICell cell = sheet1.GetRow(i + 3).CreateCell(j, CellType.String);
                    //        cell.SetCellValue("");
                    //    }
                    //}
                    
                    sheet1.GetRow(i + 3).GetCell(1).SetCellValue(resList[i].Device.DeviceNo);
                    sheet1.GetRow(i + 3).GetCell(2).SetCellValue(resList[i].Device.DeviceName);
                    sheet1.GetRow(i + 3).GetCell(3).SetCellValue(Convert.ToDateTime(resList[i].StartTime).ToString("yyyy-MM-dd HH:mm:ss"));
                    sheet1.GetRow(i + 3).GetCell(4).SetCellValue(Convert.ToDateTime(resList[i].EndTime).ToString("yyyy-MM-dd HH:mm:ss"));
                }

                using (FileStream fs = System.IO.File.OpenWrite(savePath))
                {
                    wb.Write(fs);//向打开的这个Excel文件中写入表单并保存。  
                    fs.Close();
                }

                return Json2(new JsonOkay());
            }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值