I am try attach Excel file in MVC 4. After attach excel file read all values from xl and added to List. I can upload excel file and get values in local machine. When I add this code to server I can't able to upload Excel file. I go below error.
An error occurred while processing your request.
Here I is my code;
public ActionResult AttachedXL(LoginModel log)
{
try
{
var Xllist = (List<List<string>>)Session["fulldata"];
string FilePath=string.Empty;
if (log.AttachXLMailIds != null)
{
Array.ForEach(Directory.GetFiles(Server.MapPath(@"~/XLFile/")), System.IO.File.Delete);
string FileName = Path.GetFileName(log.AttachXLMailIds.FileName);
string Extension = Path.GetExtension(log.AttachXLMailIds.FileName);
FilePath = Server.MapPath(@"~/XLFile/" + FileName);
log.AttachXLMailIds.SaveAs(FilePath);
var s = DisplayXLColumnName(FilePath);
Session["fulldata"] = s.data;
TempData["temphead"] = s.XLColumnHeading;
return RedirectToAction("SendMail", "Mail");
}
else
{
return View();
}
}
catch (ThreadAbortException)
{
throw;
}
}
Function code:
public LoginModel DisplayXLColumnName(string FilePath)
{
int val = 0;
LoginModel datamodel;
var fieldcolumn = new List<List<string>>();
var columnheading = new List<string>();
try
{
using (FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (SpreadsheetDocument doc = SpreadsheetDocument.Open(fs, false))
{
WorkbookPart workbookPart = doc.WorkbookPart;
SharedStringTablePart sstpart = workbookPart.GetPartsOfType<SharedStringTablePart>().First();
SharedStringTable sst = sstpart.SharedStringTable;
WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
Worksheet sheet = worksheetPart.Worksheet;
var cells = sheet.Descendants<Cell>();
var rows = sheet.Descendants<Row>();
foreach (Cell cell in cells)
{
if ((cell.DataType != null) && (cell.DataType == CellValues.SharedString))
{
int ssid = int.Parse(cell.CellValue.Text);
string str = sst.ChildElements[ssid].InnerText;
}
}
foreach (Row row in rows)
{
var fieldrow = new List<string>();
foreach (Cell c in row.Elements<Cell>())
{
int columnId = int.Parse(c.CellValue.Text);
string columnValue = sst.ChildElements[columnId].InnerText;
fieldrow.Add(columnValue);
}
fieldcolumn.Add(fieldrow);
}
}
}
if (val == 0)
{
foreach (var item in fieldcolumn.First())
{
columnheading.Add(item);
}
val++;
}
datamodel = new LoginModel();
{
datamodel.data = fieldcolumn;
datamodel.XLColumnHeading = columnheading;
}
}
catch (Exception ex)
{
return null;
}
return datamodel;
}
Thank You
Parameswaran R
8973215178