.Net Core---- 通过EPPlus批量导出

前台代码:

前台代码是在.net core bootstrap集成框架上的(这是效果浏览地址:http://core.jucheap.com[效果地址来自:http://blog.csdn.net/allenwdj]),首先是添加Nuget包,

 <button id="btnExport" type="button" class="btn btn-info " onclick="exportModel()"><i class="fa fa-paste"></i> 导出Excel</button>
 function exportModel() { //导出
            var delDatas = JucheapGrid.GetDataTableDeleteData();//通过jqgrid获取id值
            if (delDatas.Len > 0 && delDatas.Data.length > 0) {
                if (confirm("确认要导出这" + delDatas.Len + "条数据?")) {
// delDatas.Data 传入的id值 window.location.href = "/***/***?id=" + delDatas.Data; } } else { alert("请选择要导出的数据!"); } }

  下面就是控制器代码:

   #region 导出Excel.xlsx文件
        public IActionResult ExportText()
        {

            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))
            {
               string str = Request.Query["id"];//获取传过来的id
                String[] str1 = str.Split(",");
                long[] ids = new long[str1.Length];
                for (int i = 0; i < str1.Length; i++)
                {
                    ids[i] = long.Parse(str1[i]);//装成long数组
                }
             //根据id在数据库获取集合
                IList<FeedbackDto> collection = _usermanagementservice.GetExportAsync(ids);
                // 添加worksheet
                ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("***列表");
                worksheet.Cells[1, 1].Value = "id";//这的*号表示的是导出后列的名称
                worksheet.Cells[1, 2].Value = "***";
                worksheet.Cells[1, 3].Value = "***";
                worksheet.Cells[1, 4].Value = "*** ";
                worksheet.Cells[1, 5].Value = "***";
                worksheet.Cells[1, 6].Value = "***";
                worksheet.Cells[1, 7].Value = "***";
                worksheet.Cells[1, 8].Value = "***";
                worksheet.Cells[1, 1].Style.Font.Bold = true;
                worksheet.Cells[1, 2].Style.Font.Bold = true;
                worksheet.Cells[1, 3].Style.Font.Bold = true;
                worksheet.Cells[1, 4].Style.Font.Bold = true;
                worksheet.Cells[1, 5].Style.Font.Bold = true;
                worksheet.Cells[1, 6].Style.Font.Bold = true;
                worksheet.Cells[1, 7].Style.Font.Bold = true;
                worksheet.Cells[1, 8].Style.Font.Bold = true;
                for (int i = 0; i < collection.Count; i++)
                {
                    worksheet.Cells[2 + i, 1].Value = collection[i].id;//这的*是字段名
                    worksheet.Cells[2 + i, 2].Value = collection[i].*;
                    worksheet.Cells[2 + i, 3].Value = collection[i].*;
                    worksheet.Cells[2 + i, 4].Value = collection[i].*;
                    worksheet.Cells[2 + i, 5].Value = collection[i].*;
                    worksheet.Cells[2 + i, 6].Value = collection[i].*;
                    worksheet.Cells[2 + i, 7].Value = collection[i].CreateTime.ToString();//日期tostring一下,不然是乱的
                    worksheet.Cells[2 + i, 8].Value = collection[i].UpdateTime.ToString();
                }
                package.Save();

            }
           
            return File(sFileName, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", sFileName);
        }

  菜鸟代码,大神勿喷,

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

weixin_40883328

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

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

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

打赏作者

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

抵扣说明:

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

余额充值