2017-10-26 16 views
0

サーバーに1行のExcelファイルがあります。私は次をやろうとしている:EPPlus 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) 
      { 

      } 

過程に誤りがない、私は、ファイルを開こうとするが、私はそれを復元するためにそれを頼みます。

+0

これはあなたに明白かもしれませんが、どのようにあなたはすべての例外を嚥下、およびエラー情報を何もしていないエラーがないことを確認していますか? –

+0

try catchブロックを削除しました。 主な理由は、このパッケージが既存のファイルでストリームで動作しないことです。答えは以下の通りです。 – gtktuf

答えて

0

正しいコードは次のとおりです。

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 
      { 
      } 
関連する問題