.NET Core 通過EPPlus批量導出
阿新 • • 發佈:2018-07-05
lec har async func 通過 data quest location UNC
前臺代碼:
前臺代碼是在.net core bootstrap集成框架上的(這是效果瀏覽地址:http://core.jucheap.com),首先是添加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(); 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 = "***";//這的*號表示的是導出後列的名稱 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].*;//這的*是字段名 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); }
新手代碼,大神勿噴,
.NET Core 通過EPPlus批量導出