.Net Core 导出Excel

1、使用NuGet 安装 EPPlus.Core,

 2、代码如下

using OfficeOpenXml;
using OfficeOpenXml.Style;

public IActionResult DownloadTest(int id)
        { 
            string sWebRootFolder = _hostingEnvironment.WebRootPath;
            string sFileName = $"{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.xlsx";
            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("确认单");

                worksheet.Cells[1, 1].Value = "确认单";

                worksheet.Cells[2, 1].Value = "老用户";
                worksheet.Cells[2, 3].Value = "新用户";

                worksheet.Cells[3, 1].Value = "姓名";
                worksheet.Cells[3, 2].Value = "张三";
                worksheet.Cells[3, 3].Value = "姓名";
                worksheet.Cells[3, 4].Value = "李四";

                worksheet.Cells[4, 1].Value = "电话";
                worksheet.Cells[4, 2].Value = "123456789";
                worksheet.Cells[4, 3].Value = "电话";
                worksheet.Cells[4, 4].Value = "987654321";
                  
                worksheet.Cells[5, 1].Value = "备注:";

                worksheet.Cells[9, 1].Value = "老用户签字:";
                worksheet.Cells[9, 2].Value = "";
                worksheet.Cells[9, 3].Value = "新用户签字:";
                worksheet.Cells[9, 4].Value = "";

                //表头合并
                worksheet.Cells[1, 1, 1, 4].Merge = true;
                worksheet.Cells[2, 1, 2, 2].Merge = true;
                worksheet.Cells[2, 3, 2, 4].Merge = true;
                //备注合并
                worksheet.Cells[5, 1, 8, 4].Merge = true;

                //设置垂直居中,水平居中
                worksheet.Cells[1, 1, 2, 4].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                worksheet.Cells[1, 1, 9, 4].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                worksheet.Cells[5, 1, 8, 4].Style.VerticalAlignment = ExcelVerticalAlignment.Top;

                worksheet.Cells[1, 1].Style.Font.Bold = true;//设置字体加粗
                worksheet.Cells[1, 1].Style.Font.Size = 18;  //设置字体大小
                 
                worksheet.Cells[2, 1, 9, 4].Style.Font.Size = 12;//根据区域设置字体大小

                //worksheet.Row(1).Height = 50;     //单独设置一行行高(注意这里设置了行高,下面默认行高会失效)
                worksheet.DefaultRowHeight = 35;    //默认行高
                worksheet.DefaultColWidth = 30;     //默认列宽

                package.Save();
            }
            return File(sFileName, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "确认单.xlsx");
        }

3、_hostingEnvironment   用来获取当前目录的,使用需要引用  Microsoft.AspNetCore.Hosting   ,代码如下

using Microsoft.AspNetCore.Hosting;
 
namespace Test
{
    public class TAAController
    {
        private readonly IHostingEnvironment _hostingEnvironment;
        public TAAController( IHostingEnvironment hostingEnvironment)
        { 
            _hostingEnvironment = hostingEnvironment;
        }
    }
}

4、导出截图

 

原文地址:https://www.cnblogs.com/Zing/p/10557691.html