标签:style blog http color io os ar for 数据
> 代码实现
/// <summary> /// 获取导出表的流. /// </summary> /// <param name="excelDataSource">数据源.</param> /// <param name="sheetName">Excel名.</param> /// <param name="stream">获取的IO流.</param> /// <param name="summaryInfor">页面汇总信息</param> public void GetExportExcelStream(IList<CustomerAuth> excelDataSource, string sheetName, System.IO.Stream stream, Core.Domain.Excel.SummaryInfor summaryInfor = null) { if (stream == null) throw new ArgumentNullException("stream"); using (var xlPackage = new ExcelPackage(stream)) { var worksheet = xlPackage.Workbook.Worksheets.Add(sheetName); // Excel表的列名 var properties = new string[] { "注册号", "手机号", "姓名", "身份证号", "注册来源", "认证时间", "注册时间" }; // 设置第一行的样式 for (int i = 0; i < properties.Length; i++) { worksheet.Cells[1, i + 1].Value = properties[i]; worksheet.Cells[1, i + 1].Style.Fill.PatternType = ExcelFillStyle.Solid; worksheet.Cells[1, i + 1].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(184, 204, 228)); worksheet.Cells[1, i + 1].Style.Font.Bold = true; } int row = 2; foreach (var excelItem in excelDataSource) { int col = 1; // 注册号 worksheet.Cells[row, col].Value = excelItem.RegNumber; col++; // 手机号 worksheet.Cells[row, col].Value = excelItem.Phone; col++; // 姓名 worksheet.Cells[row, col].Value = excelItem.Name; col++; // 身份证号 worksheet.Cells[row, col].Value = excelItem.CardId; col++; // 注册来源 worksheet.Cells[row, col].Value = excelItem.Source; col++; // 认证时间 worksheet.Cells[row, col].Value = excelItem.AuthDate; col++; // 注册时间 worksheet.Cells[row, col].Value = excelItem.RegDate; col++; row++; } worksheet.Cells.AutoFitColumns(0); // 设置为自动宽度 xlPackage.Save(); } }
> 调用实现导出(MVC)
/// <summary> /// 导出实名认证表格. /// </summary> /// <returns></returns> public ActionResult ExportRealNameAuthenticationList(SearchModel model) { // 一些验证实现 byte[] bytes = null; using (var stream = new MemoryStream()) { var exportList = this._tentenActService.ExportCustomerAuthList(model.Source.AsStr(),model.RegNum, model.RegTimeStart, model.RegTimeEnd); this._realNameAuthenticationExcelService.GetExportExcelStream(exportList, "sheet", stream); bytes = stream.ToArray(); } // 必须以xlsx后缀,否则乱码 return File(bytes, "text/xls", "表名.xlsx"); }
标签:style blog http color io os ar for 数据
原文地址:http://www.cnblogs.com/StudyForLC/p/Excel.html