コード例 #1
0
        public ActionResult ExportExcel()
        {
            try
            {
                var                  webRoot = _env.WebRootPath;
                DataTable            dt      = new DataTable();
                QuanLyCongVanContext db      = new QuanLyCongVanContext();
                var                  paht    = System.IO.Path.Combine(webRoot, "Content/TempExcel/NguoiDung.xlsx");
                Workbook             wb      = new Workbook();

                //Cell Cells = char.Parse(new Cell["A3"].ToString());
                Worksheet ws = wb.Worksheets[0];
                dt.Columns.Add("Tên tài khoản");
                dt.Columns.Add("Mật khẩu");
                dt.Columns.Add("kích hoạt");
                dt.Columns.Add("Mã nhân sự");
                List <NguoiDung> listnd = new List <NguoiDung>();
                listnd = db.NguoiDung.ToList();
                string listnds = "";
                if (listnd.Count > 0)
                {
                    listnds = String.Join(",", listnd.Select(x => x.ToString()).ToArray());
                }
                var ob   = new NguoiDungRepository();
                var data = ob.GetAll();
                foreach (NguoiDung item in data)
                {
                    dt.Rows.Add(item.TenTaiKhoan, item.MatKhau, item.KichHoat, item.MaNhanSu);
                }
                string filename   = "DanhSachNguoiDung" + DateTime.Now.Day + "-" + DateTime.Now.Month + "-" + DateTime.Now.Year + ".xlsx";
                string folderName = "/assets/Excel/";

                var file = System.IO.Path.Combine(webRoot + folderName + filename);
                if (!System.IO.Directory.Exists(folderName))
                {
                    System.IO.Directory.CreateDirectory(folderName);
                }
                ws.Cells.ImportDataTable(dt, false, 2, 0);
                wb.Save(file);
                return(Json(folderName + filename));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
コード例 #2
0
        public ActionResult Index()
        {
            var listNguoiDung = nguoidungRepo.GetAll();

            return(View(listNguoiDung));
        }