예제 #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/NhanSu.xlsx");
                Workbook             wb      = new Workbook();

                //Cell Cells = char.Parse(new Cell["A3"].ToString());
                Worksheet ws = wb.Worksheets[0];
                dt.Columns.Add("Mã nhân sự");
                dt.Columns.Add("Tên");
                dt.Columns.Add("Giới tính");
                dt.Columns.Add("ngày sinh");
                dt.Columns.Add("Địa chỉ nhà");
                dt.Columns.Add("Địa chỉ cơ quan");
                dt.Columns.Add("Điện  thoại");
                dt.Columns.Add("Mã phòng");
                List <NhanSu> listns = new List <NhanSu>();
                listns = db.NhanSu.ToList();
                string listnss = "";
                if (listns.Count > 0)
                {
                    listnss = String.Join(",", listns.Select(x => x.ToString()).ToArray());
                }
                var ob   = new NhanSuRepository();
                var data = ob.GetAll();
                foreach (NhanSu item in data)
                {
                    dt.Rows.Add(item.MaNhanSu, item.Ten, item.GioiTinh, Convert.ToDateTime(item.NgaySinh), item.DiaChiNR, item.DiaChiCQ, item.DienThoai, item.MaPhong);
                }
                string filename   = "DanhSachNhanSu" + 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 listNhanSu = nhansuRepo.GetAll();

            return(View(listNhanSu));
        }