MVC简单Excel导出

搭个简单三层,如图

技术分享

Model层代码,操作信息日志:

    public partial class SysLog
    {
        //序号ID
        public int LogID { get; set; }
        //登录类型
        public int LogType { get; set; }
        //用户名
        public string UserName { get; set; }
        //操作信息
        public string Operation { get; set; }
        //操作时间
        public string OperaTime { get; set; }
    }

Web层新建控制器DataListController,Index页显示表格信息,样式就直接用了自带的强类型视图吧

技术分享

导出Excel的A标签直接请求DataListController/ExcelExport,控制器内容:

public class DataListController : Controller
    {
        //
        // GET: /DataList/
        SysLogBll sysLogBll = new SysLogBll();
        public ActionResult Index()
        {
            ViewData.Model = sysLogBll.ShowDataList();
            return View();
        }
        public EmptyResult ExcelExport()
        {
            MemoryStream ms = new MemoryStream();
            ms = sysLogBll.ExcelExport();
            Response.Clear();
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("用户日志", System.Text.Encoding.UTF8) + DateTime.Now.ToString() + ".xls");
            Response.BinaryWrite(ms.GetBuffer());
            Response.End();
            return new EmptyResult();
        }
    }

命名为“用户日志”+当前时间,IE会将中文解析为乱码,因此UrlEncode一下。

BLL层引用DAL层方法,ShowDataList用于前台展示,ExcelExport用于表格导出

 public partial class SysLogBll
    {
        SysLogDal sysLogDal = new DAL.SysLogDal();
        public List<SysLog> ShowDataList()
        {
            return sysLogDal.ShowDataList();
        }

        public MemoryStream ExcelExport()
        {
            return sysLogDal.ExcelExport();
        }
    }

DAL层添加ExcelExport方法,返回流

 public partial class SysLogDal
    {
        string sql = "select * from SysLog";
        public List<SysLog> ShowDataList()
        {
            return SqlHelper.ExecuteDataList<SysLog>(sql);
        }
        public MemoryStream ExcelExport()
        {
            MemoryStream ms = new MemoryStream();
            IWorkbook book = new HSSFWorkbook();
            ISheet sheet = book.CreateSheet("系统日志");
            using (SqlDataReader reader = SqlHelper.ExecuteDataReader(sql))
            {
                IRow row = sheet.CreateRow(0);
                ICell cell = row.CreateCell(0);
                cell.SetCellValue("序号");
                cell = row.CreateCell(1);
                cell.SetCellValue("登录类型");
                cell = row.CreateCell(2);
                cell.SetCellValue("用户名");
                cell = row.CreateCell(3);
                cell.SetCellValue("操作信息");
                cell = row.CreateCell(4);
                cell.SetCellValue("操作时间");
                for (int i = 0; reader.Read(); i++)
                {
                    row = sheet.CreateRow(i + 1);
                    for (int j = 0; j < reader.FieldCount; j++)
                    {
                        cell = row.CreateCell(j);
                        cell.SetCellValue(reader[j].ToString());
                    }
                }
            }
            book.Write(ms);
            ms.Flush();
            ms.Position = 0;
            return ms;
        }
    }

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。