Ejemplo n.º 1
0
        public ActionResult ExcelUpload(HttpPostedFileBase file)
        {
            // Tạo danh sách từ mới.
            List <WordsModel> listWord = new List <WordsModel>();
            WordsModel        word     = new WordsModel();

            // Biến lưu kết quả trả về
            int result = 0;

            if (file != null && file.ContentLength > 0)
            {
                Excel.Application app = new Excel.Application();
                Excel.Workbook    workBook;
                Excel.Worksheet   workSheet;
                // Bước 1: Lưu file vào server.
                string fileLocation = Server.MapPath("~/Content/Uploads/" + file.FileName);
                if (!System.IO.File.Exists(fileLocation))
                {
                    file.SaveAs(fileLocation);
                }

                // Bước 2: Đọc file.
                workBook  = app.Workbooks.Open(fileLocation);
                workSheet = workBook.ActiveSheet;
                Excel.Range range = workSheet.UsedRange;

                // Duyệt toàn bộ dòng từ dòng 2.
                int chkRow = 2;
                try
                {
                    for (int row = 2; row <= range.Rows.Count; row++)
                    {
                        word           = new WordsModel();
                        word.Dict_Type = (int)range.Cells[row, 1].Value; // Kiểu từ điển
                        word.Key       = range.Cells[row, 2].Value;      // Từ vựng
                        word.Value     = range.Cells[row, 3].Value;      // Nghĩa của từ
                        word.Update_By = int.Parse(CookieHelper.Get(Configs.COOKIES_ACCOUNT_ID).ToString());
                        listWord.Add(word);
                        chkRow++;
                    }
                }
                catch
                {
                    word.Message = string.Format(Configs.ERROR_FORMAT_FILE, chkRow);
                    word.Code    = (int)EnumError.UPDATE_ERROR;
                    word.Result  = false;
                    SetTempData(word.Message);
                    return(View());
                }

                // Nếu có yêu cầu xóa toàn bộ dữ liệu cũ thì xóa.
                var deleteOld = Request.Form.Get("deleteOld");
                if (deleteOld != null && deleteOld == "on")
                {
                    result = _service.DeleteAll();
                }

                // Cập nhật từ vựng vào CSDL.
                result = _service.InsertFromExcel(listWord);

                workBook.Close(false, null, null);
                app.Quit();

                releaseObject(workSheet);
                releaseObject(workBook);
                releaseObject(app);

                if (result > 0)
                {
                    word.Message  = Configs.SUCCESS_UPDATE;
                    word.Code     = result;
                    word.Result   = true;
                    word.Redirect = Url.Action("List", "Words");
                    SetTempData(word.Message, word.Redirect);
                }
                else
                {
                    word.Message = Configs.ERROR_PROCESS;
                    word.Code    = (int)EnumError.UPDATE_ERROR;
                    word.Result  = false;
                    SetTempData(word.Message);
                }

                // Sau khi đọc file xong thì xóa file đã upload đi.
                if (System.IO.File.Exists(fileLocation))
                {
                    System.IO.File.Delete(fileLocation);
                }
            }
            else
            {
                word.Message = Configs.ERROR_NOT_CHOICE_FILE;
                word.Code    = (int)EnumError.UPDATE_ERROR;
                word.Result  = false;
                SetTempData(word.Message);
            }

            return(View());
        }