2017-10-26 9 views
0

한 줄로 된 서버에 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 
      { 
      }