private void OnPostImport(Importer import) { IFormFile file = import.File; string folderName = "Upload"; string webRootPath = _hostingEnvironment.WebRootPath; string newPath = Path.Combine(webRootPath, folderName); StringBuilder sb = new StringBuilder(); if (!Directory.Exists(newPath)) { Directory.CreateDirectory(newPath); } if (file.Length > 0) { string sFileExtension = Path.GetExtension(file.FileName).ToLower(); ISheet sheet; string fullPath = Path.Combine(newPath, file.FileName); Debug.WriteLine(file.ToString()); Debug.WriteLine(fullPath); CentricsContext context = HttpContext.RequestServices.GetService(typeof(Centrics.Models.CentricsContext)) as CentricsContext; using (var stream = new FileStream(fullPath, FileMode.Create)) { Debug.WriteLine(stream.ToString()); Debug.WriteLine(stream.CanRead); file.CopyTo(stream); stream.Position = 0; if (sFileExtension == ".xls") { HSSFWorkbook hssfwb = new HSSFWorkbook(stream); //This will read the Excel 97-2000 formats sheet = hssfwb.GetSheetAt(0); //get first sheet from workbook } else { XSSFWorkbook hssfwb = new XSSFWorkbook(stream); //This will read 2007 Excel format sheet = hssfwb.GetSheetAt(0); //get first sheet from workbook } IRow headerRow = sheet.GetRow(0); //Get Header Row int cellCount = headerRow.LastCellNum; //sb.Append("<table class='table'><tr>"); // notneeded? for (int j = 0; j < cellCount; j++) { NPOI.SS.UserModel.ICell cell = headerRow.GetCell(j); if (cell == null || string.IsNullOrWhiteSpace(cell.ToString())) { continue; } //sb.Append("<th>" + cell.ToString() + "</th>"); } //sb.Append("</tr>"); //sb.AppendLine("<tr>"); List <ClientAddress> ListCA = context.getAllClientAddress(); for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++) //Read Excel File { IRow row = sheet.GetRow(i); if (row == null) { continue; } if (row.Cells.All(d => d.CellType == CellType.Blank)) { continue; } //if (context.GetClientAddressList(row.GetCell(0).ToString()).ClientCompany != "") continue; bool skip = false; // for ignoring //for (int k = 0; k<ListCA.Count; k++) //{ // if (row.GetCell(0).ToString() == ListCA[i].ClientCompany) // { // skip = true; // } else if (row.GetCell(1).ToString() == ListCA[i].CustomerID) // { // skip = true; // } //} //if (skip) //{ // continue; //} ClientAddress cA = new ClientAddress(); cA.EmailList = new List <string>(); cA.TitleList = new List <string>(); cA.Addresslist = new List <string>(); cA.ContactList = new List <string>(); cA.ContactNoList = new List <string>(); for (int j = row.FirstCellNum; j < cellCount; j++) { if (row.GetCell(j) != null) { //trying to get the value of an empty cell as empty cell is neither 0 nor null nor "". row.CreateCell(200); if (row.GetCell(j).ToString() == row.GetCell(200).ToString()) { //assuming nothing is at 200 if not get fked? continue; } if (j == 0) { cA.ClientCompany = row.GetCell(j).ToString(); } else if (j == 1) { cA.CustomerID = row.GetCell(j).ToString(); } else if (j == 2) { string stringer = ""; stringer = row.GetCell(j).ToString(); cA.Addresslist.Add(stringer); } else if (j == 3) { cA.TitleList.Add(row.GetCell(j).ToString()); } else if (j == 4) { cA.ContactList.Add(row.GetCell(j).ToString()); } else if (j == 5) { cA.ContactNoList.Add(row.GetCell(j).ToString()); } else if (j == 6) { cA.EmailList.Add(row.GetCell(j).ToString()); } else if (j == 7) { cA.TitleList.Add(row.GetCell(j).ToString()); } else if (j == 8) { cA.ContactList.Add(row.GetCell(j).ToString()); } else if (j == 9) { cA.ContactNoList.Add(row.GetCell(j).ToString()); } else if (j == 10) { cA.EmailList.Add(row.GetCell(j).ToString()); } } } //sb.AppendLine("</tr>"); Debug.WriteLine("i am done" + "1"); context.Imported(cA); } //sb.Append("</table>"); } } //return this.Content(sb.ToString()); }