.NET Core export Excel data

.NetCore+EPPlus export Excel report

Look at the renderings first
Insert picture description here
Insert picture description here

1. Install EPPlus
epplus2. Package help

Show some below 内联代码片.

public class ExcelHelper
    {
    
    
        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <param name="dt">数据源</param>
        /// <param name="sWebRootFolder">webRoot文件夹</param>
        /// <param name="sFileName">文件名</param>
        /// <param name="sColumnName">自定义列名(不传默认dt列名)</param>
        /// <returns></returns>
public static byte[] ExportExcel(DataTable dt, string sWebRootFolder, string sFileName, string[] sColumnName, ref string msg)
        {
    
    
            try
            {
    
    
                if (dt == null || dt.Rows.Count == 0)
                {
    
    
                    msg = "没有符合条件的数据!";
                  //  return false;
                }
                 //转utf-8
          UTF8Encoding utf8 = new UTF8Encoding();
          byte[] buffer = utf8.GetBytes(sFileName);
          sFileName = utf8.GetString(buffer);
 	//判断文件夹
          sWebRootFolder = Path.Combine(sWebRootFolder, "ExprotExcel");
                if (!Directory.Exists(sWebRootFolder))
                    Directory.CreateDirectory(sWebRootFolder);
      //删除大于7天的文件
        string[] files = Directory.GetFiles(sWebRootFolder, "*.xlsx", SearchOption.AllDirectories);
        foreach (string item in files)
         {
    
    
         
                    FileInfo f = new FileInfo(item);
                    DateTime now = DateTime.Now;
                    TimeSpan t = now - f.CreationTime;
                    int day = t.Days;
                    if (day > 7)
                    {
    
    
                        File.Delete(item);
                    }
                }
        //判断同名文件
                FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
                if (file.Exists)
                {
    
    
                    //判断同名文件创建时间
                    file.Delete();
                    file = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
                }
         using (ExcelPackage package = new ExcelPackage(file))
                {
    
    
                    //添加worksheet
                    ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(sFileName.Split('.')[0]);
                    //添加表头
                    int column = 1;
               if (sColumnName.Count() == dt.Columns.Count)
                    {
    
    
                        foreach (string cn in sColumnName)
                        {
    
    
                            worksheet.Cells[1, column].Value = cn.Trim();
                            worksheet.Cells[1, column].Style.Font.Bold = true;//字体为粗体
                            worksheet.Cells[1, column].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;//水平居中
                            worksheet.Cells[1, column].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;//设置样式类型
                            worksheet.Cells[1, column].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.FromArgb(159, 197, 232));//设置单元格背景色
                            column++;
                        }
                    }
               else
                    {
    
    
                        foreach (DataColumn dc in dt.Columns)
                        {
    
    
                            worksheet.Cells[1, column].Value = dc.ColumnName;
                            worksheet.Cells[1, column].Style.Font.Bold = true;//字体为粗体
                            worksheet.Cells[1, column].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;//水平居中
                            worksheet.Cells[1, column].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;//设置样式类型
                            worksheet.Cells[1, column].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.FromArgb(159, 197, 232));//设置单元格背景色
                            column++;
                        }
                    }
            //添加数据
           int row = 2;
           foreach (DataRow dr in dt.Rows)
             {
    
    
                  int col = 1;
                  foreach (DataColumn dc in dt.Columns)
                     {
    
    
                       worksheet.Cells[row, col].Value = dr[col - 1].ToString();
                            col++;
                        }
                        row++;
                    }
                //自动列宽
                    worksheet.Cells.AutoFitColumns();       
               //保存
                 package.Save();
                 return package.GetAsByteArray() ;
                 }
            }
 catch (Exception ex)
            {
    
    
                msg = "生成Excel失败:" + ex.Message;
                return null;
            }
	}
}

Three, controller call

Register the helper class in the controller
Some of the following are shown 内联代码片.

 //注册 导出帮助类
        private IHostingEnvironment _hostingEnvironment;
        public PurchaseController(IHostingEnvironment hostingEnvironment)
        {
    
            
            _hostingEnvironment = hostingEnvironment;
        }

Show some below 内联代码片.

 [HttpGet]
        //导出合同
        public ActionResult DaoContractList()
        {
    
    
        //计算总条数
            var list = _PMContext.Query<Contract>().ToList();
            //转为json格式
            var json = JsonConvert.SerializeObject(list);
            //转为dataTable
            DataTable dt = new DataTable();
            dt = JsonConvert.DeserializeObject<DataTable>(json);
             var msg = string.Empty;//返回消息
             string[] sColumnName = {
    
     "合同编号", "合同名称", "合同类型", "供应商", "采购物品", "需求数量", "我方签约人", "合同金额", "开始时间", "结束时间","合同状态" };
            
            //调用帮助类里的导出方法
            byte[] by = ExcelHelper.ExportExcel(dt, "", "导出信息", sColumnName, ref msg);
            return File(by, "application/ms-excel", "test.xlsx");
        }

Just call location.href=" xxxxxx" directly

Guess you like

Origin blog.csdn.net/d1332508051/article/details/107612340