2014-03-19 7 views
-1

다음 데이터를 내보내고 Excel로 내보내는 코드를 작성했습니다. 이 코드는 로컬에서 제대로 작동하지만이 코드를 서버 (UAT)에 배포하면 작동하지 않습니다. 서버를 다시 시작하면 잠시 동안 작동하지만 잠시 후 실패합니다.서버에 배포 할 때 코드가 작동하지 않습니다.

코드 : 당신이 알아야 할

public void ExportToExcelFunction(string FlName, DataTable mydt, string DispColName, string BindCols) 
    { 
     Excel.Application xlObj = new Excel.Application(); 
     object oMissing = System.Reflection.Missing.Value; 
     xlObj.Visible = false; 
     //vinod 
     string filepath = Server.MapPath("Export"); 
     string strFlName = filepath + "\\Master.xlsx"; 

     Excel.Workbook xlWB = xlObj.Workbooks.Open(strFlName, 0, true, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, true, 0, true); 
     Excel.Worksheet xlSheet = (Excel.Worksheet)xlWB.ActiveSheet; 

     int cols = mydt.Columns.Count; 
     int rows = mydt.Rows.Count; 

     //Added for export to excel 
     try 
     { 
      //For Column 
      string[] strCols = DispColName.Split(','); 
      for (int i = 1; i <= strCols.Length; i++) 
      { 
       if (strCols[i - 1].Length > 0 && strCols[i - 1] != null) 
        xlSheet.Cells[1, i] = Convert.ToString(strCols[i - 1]); 
      } 

      // for Row 
      string[] strColBind = BindCols.Split(','); 
      for (int r = 0; r < rows; r++) 
      { 
       for (int c = 0; c < strColBind.Length; c++) 
       { 
        xlSheet.Cells[r + 2, c + 1] = mydt.Rows[r][strColBind[c]]; 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
     } 


     String newFlName = "\\" + DateTime.Now.Ticks.ToString() + "_" + FlName + ".xls"; 
     xlWB.SaveAs(filepath + newFlName, Excel.XlFileFormat.xlWorkbookNormal, "", "", false, false, Excel.XlSaveAsAccessMode.xlExclusive, true, false, "", true); 

     xlWB.Close(true, oMissing, oMissing); 
     xlObj.Quit(); 

     System.IO.FileInfo file = new System.IO.FileInfo(@"" + filepath + newFlName + ""); 
     Response.Clear(); 
     Response.ClearHeaders(); 
     Response.ClearContent(); 
     Response.AppendHeader("Content-Disposition", "attachment; filename = " + FlName + ".xls"); 
     Response.AppendHeader("Content-Length", file.Length.ToString()); 
     Response.ContentType = "application/download"; 
     Response.WriteFile(file.FullName); 
     Response.Flush(); 
     Response.Close(); 
     Response.End(); 

    } 
+0

어떤 오류가 발생합니까? – DLeh

+0

서버에 파일 경로에 쓸 수있는 권한이 없을 수 있습니다. – DLeh

+0

서버에 Excel이 있습니까? – PaulG

답변

0

두 가지.

첫 번째 코드는 Excel Interop Services을 사용하는 것 같습니다. 따라서 Excel을 서버에 설치해야합니다.

두 번째로 서버 쪽에서 Excel Interop Services를 사용하는 것이 Microsoft에서 for a number of good reasons을 지원하지 않는다는 것입니다.

대신에 EPPlus 또는 Open Office XML SDK과 같은 대체 라이브러리를 사용하는 것이 좋습니다 (둘 다 서버 측에서 잘 실행 됨).

+0

감사합니다.이 라이브러리를 확인해 보겠습니다. – user3437960

+0

나는 개인적으로 EPPlus를 아주 간단하고 강력하다는 것을 찾아 냈다. NuGet을 통해 익숙하다면 사용할 수 있습니다. – mason

관련 문제