public async Task<ActionResult> ExportDataToExcel() { //查询出的数据 var list = new List<TestDataViewModel>(); list.Add(new TestDataViewModel { PlanSubject = "test1", MobileNo = "13577778888", TelcoOperatorText = "移动" }); list.Add(new TestDataViewModel { PlanSubject = "test2", MobileNo = "13577779999", TelcoOperatorText = "移动" }); list.Add(new TestDataViewModel { PlanSubject = "test3", MobileNo = "8077778888", TelcoOperatorText = "电信" }); //var model = await DataTrafficGiftRecordQueryEntry.SelectAsync(); //已经领取了流量 //var hasGetList = model.Where(m => !string.IsNullOrWhiteSpace(m.MobileNo) && m.MobileNo.Length == 11); //var result = hasGetList.MapList<DataTrafficGiftRecordViewModel>().ToList(); var setting = new ExcelExportOption<TestDataViewModel> { FileName = "客户领取流量记录", DefaultRowHeight = 270 }; if (list.Count() >= 65535) setting.Description = "结果数据总数:" + list.Count() + "但Excel最多支持65535条数据, 请分开条件导出"; setting.Source = list.Take(65534); var colums = new List<ExcelColum<TestDataViewModel>> { new ExcelColum<TestDataViewModel> { Name = "赠送计划标题", Width = 7000, ColumType = ExcelColumType.String, ResultFunc = x => x.PlanSubject }, new ExcelColum<TestDataViewModel> { Name = "手机号码", Width = 7000, ColumType = ExcelColumType.String, ResultFunc = x => x.MobileNo }, new ExcelColum<TestDataViewModel> { Name = "手机运营商", Width = 7000, ColumType = ExcelColumType.String, ResultFunc = x => x.TelcoOperatorText }, }; setting.Colums = colums; //发送文件路径到客户端 var steam = ExcelUtility.ExportToStream(setting); var filename = HttpUtility.UrlEncode(setting.FileName + DateTime.Now.ToString("_yyyyMMdd-HHmmss") + ".xls", System.Text.Encoding.UTF8); return File(steam, "application/vnd.ms-excel", filename); //Response.AddHeader("Content-Disposition", $"attachment;filename={filename}"); //Response.Clear(); //Response.BinaryWrite(steam.GetBuffer()); //Response.End(); }