mahmudur rifat

mahmudur rifat

  • NA
  • 33
  • 13.3k

import data to xls file and show data grid & store data mvc4

Jul 15 2016 3:48 PM
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 <br/>";
return View("Index");
}
else
{
if (excelfile.FileName.EndsWith("
xls") || excelfile.FileName.EndsWith("xlsx"))
{
string fileName = Path.GetFileName(excelfile.FileName);
string path = Path.Combine(Server.MapPath("~/Content"), fileName);


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(path);
MExcel.Worksheet worksheet = workbook.ActiveSheet;
MExcel.Range range = worksheet.UsedRange;



//List<SEBank> sebank = new List<SEBank>();
//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)range.Cells[row, 3]).Text;
// item.Add(p);
}
//ViewBag.listbankdata = sebank;

return View("Success");
}
//else
//{
// ViewBag.Error = "File type is incorrect <br/>";
// return View("Index");
//}

}
}
 

Answers (1)