首页 > 其他 > 详细

导出Excel表

时间:2014-10-09 13:30:23      阅读:130      评论:0      收藏:0      [点我收藏+]

代码实现    

> 代码实现  

bubuko.com,布布扣
        /// <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();
            }
        }    
View Code

> 调用实现导出(MVC)

bubuko.com,布布扣
        /// <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");
        }    
View Code

 

导出Excel表

原文:http://www.cnblogs.com/StudyForLC/p/Excel.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!