使用 EPPlus Excel.文件保存后出错



服务器上有一个Excel文件,里面有一行。我正在尝试下一步:

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)
            {
            }

在此过程中没有错误,但是当我尝试打开文件时,我要求它恢复它。

正确的代码是:

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
            {
            }

最新更新