Reputation: 81
there is an Excel file on the server with one line in it. I'm trying to do next:
try
{
using (FileStream fs = new FileStream(System.Web.HttpContext.Current.Server.MapPath("/DiscountLog" + ".xlsx"), FileMode.OpenOrCreate))
{
using (ExcelPackage pp = new ExcelPackage(fs))
{
ExcelWorksheet wss = pp.Workbook.Worksheets[1];
int CurrentRow = wss.Dimension.Rows + 1;
wss.Cells[CurrentRow, 1].Value = model.FirstName;
wss.Cells[CurrentRow, 2].Value = model.LastName;
wss.Cells[CurrentRow, 3].Value = model.DiscountCard;
wss.Cells[CurrentRow, 4].Value = model.PhoneNumber;
wss.Cells[CurrentRow, 5].Value = model.Email;
wss.Cells[CurrentRow, 6].Value = DateTime.Now.ToString();
pp.SaveAs(fs);
}
}
}
catch (Exception)
{
}
There is no error in the process, however when I try to open the file, I ask it to restore it.
Upvotes: 1
Views: 3189
Reputation: 81
The correct code is:
var fileinfo = new FileInfo(System.Web.HttpContext.Current.Server.MapPath("/DiscountLog" + ".xlsx"));
if (fileinfo.Exists)
{
using (ExcelPackage pp = new ExcelPackage(fileinfo))
{
ExcelWorksheet wss = pp.Workbook.Worksheets[1];
int CurrentRow = wss.Dimension.Rows + 1;
wss.Cells[CurrentRow, 1].Value = model.FirstName;
wss.Cells[CurrentRow, 2].Value = model.LastName;
wss.Cells[CurrentRow, 3].Value = model.DiscountCard;
wss.Cells[CurrentRow, 4].Value = model.PhoneNumber;
wss.Cells[CurrentRow, 5].Value = model.Email;
wss.Cells[CurrentRow, 6].Value = DateTime.Now.ToString();
pp.Save();
}
}
else
{
}
Upvotes: 1