NOPI Excel data into the database

         /// <summary>

        /// excel file and upload the data file into a database

        /// </summary>

        /// <param name="file"></param>

        /// <returns></returns>

        [HttpPost]

        public JsonResult UploadFile(HttpPostedFileBase file)

        {

            var fileName = file.FileName;

 

            fileName = fileName.Replace(" ", "_").Replace("\\", "_").Replace("/", "_");

            fileName = DateTime.Now.Ticks.ToString() + "_" + fileName;

 

            var defaultPath = AppSettings["UploadFiles"];

            if (String.IsNullOrWhiteSpace(defaultPath))

                defaultPath = @"D:\RegTech\UploadFiles";

 

            var excelUploadPath = Path.Combine(defaultPath, "UserCustomBlackList");

            if (!Directory.Exists(excelUploadPath))

                Directory.CreateDirectory(excelUploadPath);

            // Save the upload files to the server

            var saveFilePath = Path.Combine(excelUploadPath, fileName);

            file.SaveAs(saveFilePath);

 

            List<UserCustomBlackList> excelResult = ReadExcelByCustomBlack(saveFilePath);

}

 

/// <summary>

        /// upload of Excel data into the database

        /// </summary>

        /// <param name="fileName"></param>

        /// <param name="type"></param>

        /// <returns></returns>

        protected List<UserCustomBlackList> ReadExcelByCustomBlack(String fileName)

        {                                  

            List<UserCustomBlackList> rtn = new List<UserCustomBlackList>();

            FileInfo existingFile = new FileInfo(fileName);          

            try

            {

                using (FileStream fs = System.IO.File.OpenRead(fileName))

                {

                    // Create Excel WorkBook based on the file

                    IWorkbook wk = WorkbookFactory.Create(fs);

                    string extension = fileName.Substring(fileName.LastIndexOf(".")).ToString().ToLower();

                    // get the first page Sheet

                    ISheet sheet = wk.GetSheetAt(0);

                    int rowIndex = 1;

                    for (int i = 1; i <= sheet.LastRowNum; i++)

                    {

                        UserCustomBlackList data = new UserCustomBlackList();

                        IRow row = sheet.GetRow(i);

                        if (row != null)

                        {

                            data.DisplayIndex = rowIndex++;

                            data.BlackContent = row.GetCell(0) == null ? String.Empty : row.GetCell(0).ToString();

 

                        }

                        if (!string.IsNullOrWhiteSpace(data.BlackContent))

                            rtn.Add(data);

                    }

                    return rtn;

                }

            }

            catch (Exception ex)

            {

                LogUtility.Exception (ex, source: "Client -ReadExcelByCustomBlack");            

                return null;

            }

      }

Guess you like

Origin www.cnblogs.com/liyanbofly/p/10984208.html