2017-02-16 47 views
0

我正在使用外部DLL來創建Excel文件。它完全在網頁中工作,但是當我通過Web服務使用它時,它不會給出任何錯誤,但不會創建任何文件。這裏是web服務和excel導出函數的代碼。使用asp.net web服務創建excel文件

[WebMethod, System.Web.Script.Services.ScriptMethod] 
    public static string DownloadProfile(string profileids) 
    { 
     Result pro = new Result(); 
     if (profileids == "") 
      pro = new Result(0, false, "No Profile to download", ""); 
     else 
     { 
      candidate_search cs = new candidate_search(); 
      string SqlProfile = "query"; 

      try 
      { 
       cs.ExportExcel(SqlProfile); 
       pro = new Result(0, true, "CV Downloaded Successfully !", ""); 
      } 
      catch { 
       //pro = new Result(0, false, "No Profile to download", ""); 
      } 
     } 

     string json = JsonConvert.SerializeObject(pro, Newtonsoft.Json.Formatting.Indented); 
     return json; 
    } 

    public void ExportExcel(string SQLqueryProfile) 
    { 
     string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; 
     SqlConnection ConProfile = new SqlConnection(constr); 
     SqlCommand CmdProfile = new SqlCommand(SQLqueryProfile); 
     SqlDataAdapter SdaProfile = new SqlDataAdapter(); 
     CmdProfile.Connection = ConProfile; 
     SdaProfile.SelectCommand = CmdProfile; 
     DataTable DtProfile = new DataTable(); 
     SdaProfile.Fill(DtProfile); 

     using (XLWorkbook wb = new XLWorkbook()) 
     { 
      wb.Worksheets.Add(DtProfile, "Profile"); 
      Response.Clear(); 
      Response.Buffer = true; 
      Response.Charset = ""; 
      Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
      Response.AddHeader("content-disposition", "attachment;filename=dataExport.xlsx"); 
      using (MemoryStream MyMemoryStream = new MemoryStream()) 
      { 
       wb.SaveAs(MyMemoryStream); 
       MyMemoryStream.WriteTo(Response.OutputStream); 
       Response.Flush(); 
       Response.End(); 
      } 
     } 
    } 
+1

錯誤??????????? –

+0

沒有錯誤。但不創建任何Excel文件。 –

+0

使用調試器。 – nvoigt

回答

0

既然你沒有錯誤,你可以做兩件事情:

  1. 調試。也許有一個被拋出和吞噬的錯誤?
  2. 我看到你正在將文件寫入響應。寫入磁盤,而不是看看是否有效。