public ActionResult ImportExcel(MProjectImportExcelStub model) { if (ModelState.IsValid) { XSSFSheet sheet; int colCount; int rowCount; int startRow = 3; int colNum; int id; List <string> messageList = new List <string>(); string currentFilePathInServer = Server.MapPath(model.ExcelFilePath); XSSFWorkbook book; try { using (FileStream file = new FileStream(currentFilePathInServer, FileMode.Open, FileAccess.Read)) { book = new XSSFWorkbook(file); } sheet = (XSSFSheet)book.GetSheet("Data"); rowCount = sheet.LastRowNum; colNum = 0; id = 0; bool allowSave = true; for (int row = startRow; row <= rowCount; row++) { colNum = 0; var dt = (XSSFCell)sheet.GetRow(row).GetCell(colNum); if (dt != null) { allowSave = true; sheet.GetRow(row).GetCell(colNum).SetCellType(CellType.String); if (sheet.GetRow(row).GetCell(colNum).StringCellValue != "") { m_project insertData = new m_project(); m_project filledData = this.FillDataFromExcel(insertData, sheet, row, colNum, ref messageList, ref allowSave); if (allowSave == true) { RepoMProject.Save(filledData); } } } } } catch (Exception e) { messageList.Add("An error has occurred. Please contact administrator."); } ViewBag.MessageList = messageList; return(View("ImportExcelForm", model)); //return ret; } else { ViewBag.MessageList = null; return(View("ImportExcelForm", model)); } }
public ActionResult GetExcelTemplate() { List <Business.Entities.company> listCompany = RepoCompany.FindAll(); List <Business.Entities.contractor> listContractor = RepoContractor.FindAll(); List <Business.Entities.project> listProject = RepoProject.FindAll(); string filename = "Template M Project.xlsx"; byte[] excel = MProjectImportExcelStub.GenerateTemplate(listCompany, listContractor, listProject); //"~/App_Data/template/licensor.xls" return(File(excel, "application/x-msexcel", filename)); }
public ViewResult ImportExcel() { MProjectImportExcelStub model = new MProjectImportExcelStub(); return(View("ImportExcelForm", model)); }