예제 #1
0
        public ActionResult Export(int id)
        {
            var session = (AdminLogin)Session[CommonConstants.USER_SESSION];

            if (session.id_permission == 0)
            {
                return(View("Error"));
            }
            ViewBag.AdminName = session.name;
            var tmp = new ImportExport();
            // Gọi lại hàm để tạo file excel
            var stream = tmp.CreateExcelFile(id);
            // Tạo buffer memory strean để hứng file excel
            var buffer = stream as MemoryStream;

            // Đây là content Type dành cho file excel
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            // trên firefox hay IE thì dòng này sẽ hiện Save As dialog cho người dùng chọn thư mục để lưu
            // File name của Excel này là ExcelDemo
            Response.AddHeader("Content-Disposition", "attachment; filename=DeThi.xlsx");
            // Lưu file excel của chúng ta như 1 mảng byte để trả về response
            Response.BinaryWrite(buffer.ToArray());
            // Send tất cả ouput bytes về phía clients
            Response.Flush();
            Response.End();
            // Redirect về luôn trang index :D
            return(RedirectToAction("Index"));
        }