my project mvc 4.now import xls file and show table. this file path save my project but this path is not read and error.how can open this file and save my data base please help me
below in my code
[HttpPost]
public ActionResult Import(HttpPostedFileBase excelfile)
{
if (excelfile == null || excelfile.ContentLength == 0)
{
ViewBag.Error = "Please select a excel file
";
return View("Index");
}
else
{
if (excelfile.FileName.EndsWith("
{
string fileName = Path.GetFileName(excelfile.
string path = Path.Combine(Server.MapPath("~
if (System.IO.File.Exists(path))
System.IO.File.Delete(path);
excelfile.SaveAs(path);
//Read data from excel file
MExcel.Application application = new MExcel.Application();
MExcel.Workbook workbook = application.Workbooks.Open(
MExcel.Worksheet worksheet = workbook.ActiveSheet;
MExcel.Range range = worksheet.UsedRange;
//List
//for (int row = 4; row <= range.Rows.Count; row++ )
//{
// SEBank p = new SEBank();
// p.Invoice = ((Excel.Range)range.Cells[row, 1]).Text;
// p.DateInvoice = ((Excel.Range)range.Cells[row, 2]).Text;
// p.DatePayment = ((Excel.Range)range.Cells[row, 3]).Text;
// p.Sender = ((Excel.Range)range.Cells[row, 4]).Text;
// p.Benificiary = ((Excel.Range)range.Cells[row, 5]).Text;
// p.AccountNumber = ((Excel.Range)range.Cells[row, 6]).Text;
// p.BankBranch = ((Excel.Range)range.Cells[row, 7]).Text;
// p.BankName = ((Excel.Range)range.Cells[row, 8]).Text;
// p.City = ((Excel.Range)range.Cells[row, 9]).Text;
// p.Reference = ((Excel.Range)range.Cells[row, 10]).Text;
// p.Pin = ((Excel.Range)range.Cells[row, 111]).Text;
// p.Cashier = ((Excel.Range)range.Cells[row, 12]).Text;
// p.TotalPayDollar = ((Excel.Range)range.Cells[row, 13]).Text;
// p.TotalPayLocal = ((Excel.Range)range.Cells[row, 14]).Text;
// p.Branch = ((Excel.Range)range.Cells[row, 15]).Text;
// p.SenderPhone = ((Excel.Range)range.Cells[row, 16]).Text;
// p.ReceiverPhone = ((Excel.Range)range.Cells[row, 17]).Text;
// sebank.Add(p);
// item p = new item();
// p.id = ((Excel.Range)range.Cells[row, 1]).Text;
// p.name = ((Excel.Range)range.Cells[row, 2]).Text;
// p.price =decimal.Parse((Excel.Range)
// item.Add(p);
}
//ViewBag.listbankdata = sebank;
return View("Success");
}
//else
//{
// ViewBag.Error = "File type is incorrect
";
// return View("Index");
//}
}
}
Hima BinduPosted Jul 18, 2016, 1:44 AM
1. check if path exists
2. Do you have write permissions on the path this file referring to