2014-01-29 30 views
0

您好我正在使用json調用一個調用另一個函數,最終調用ExpotToExcel函數的函數。的功能是作爲以下導出到使用Json調用的函數不是以excel格式導出數據

$.ajax({ 
       type: "post", 
       contentType: "application/json;charset=utf-8", 
       // dataType: "json", 
       url: "Model/Services/CurrentHistoryService.asmx/TestingExport", 
       data: JSON.stringify(empno), 
       success: function (resp) { 
        if (!(resp == null)) { 
         $('#ExportEmpInfo').empty(); 
         $('#ExportEmpInfo').append('<p><b>' + 'Data Has been Exported to Excel successfully ' + '</b></p>'); 
        } 
        else { 

         $('#ExportEmpInfo').empty(); 
         $('#ExportEmpInfo').append('<p><b>' + 'Exporting to Excel Failed' + '</b></p>'); 

        } 


       }, 
       error: function (jqXHR, textStatus, errorThrown) { 
        alert('TextStatus:' + textStatus + ' errorThrown: ' + errorThrown); 
       } 
      }) 

的WebMethod:

[WebMethod] 
     [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
     public void TestingExport(string empNo) 
     { 
      RefEmployeeRepository emply = new RefEmployeeRepository(); 
      List<RefEmployee> empcurlist = emply.GetEmployeeCurrentByEMPNO(empNo); 

      DataTable dt = new DataTable(); 
      dt = ToDataTable(empcurlist); 
      string title = "Customer Current Information"; 
      ReferenceDataLogic.GenerateExcelReport(title, dt); 

     } 

GenerateExcelReport功能:

public static void GenerateExcelReport(string value, DataTable localTable) 
     { 
      DataSet ds = new DataSet();  
      DataTable dtHeader = new DataTable(); 
      dtHeader.Columns.Add(new DataColumn(value)); 
      DataTable dtBlankRow = new DataTable(); 
      dtBlankRow.Columns.Add(new DataColumn(" ")); 
      ds.Tables.Add(dtHeader); 
      ds.Tables.Add(dtBlankRow); 
      ds.Tables.Add(localTable); 

      HelperFunctions.ExportToExcel(HttpContext.Current.Response, ds); 
     } 

ExportToExcel功能:

public static void ExportToExcel(HttpResponse response, DataSet ds) 
     { 
      try 
      { 
       response.Clear(); 
       response.ClearContent(); 
       response.Charset = ""; 
       response.ContentType = "application/vnd.ms-excel"; 

       DataGrid dg = new DataGrid(); 

       for (int i = 0; i < ds.Tables.Count; i++) 
       { 

        StringWriter stringWriter = new StringWriter(); 

        HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(stringWriter); 

        stringWriter = new System.IO.StringWriter(); 
        htmlWriter = new System.Web.UI.HtmlTextWriter(stringWriter); 
        dg.DataSource = ds.Tables[i]; 
        dg.DataBind(); 
        dg.RenderControl(htmlWriter); 
        response.Write(stringWriter.ToString()); 
       } 
       response.Flush(); 
       response.Close(); 
       response.Write(""); 

      } 
      catch (Exception ex) 
      { 
       System.Diagnostics.Debug.WriteLine(ex.Message); 
      } 
     } 

控制正在經歷所有這些功能但經過上次導出到Excel函數後不生成Excel文件。但是如果我直接使用另一個代碼背後的代碼來調用這個函數,而不是使用json來調用間接調用這個方法的web方法,那麼我會得到Excel工作表。請幫我解決這個問題,爲什麼在函數執行完成後出錯並沒有生成Excel表單。

回答

0

經過大量的研究,我發現我們不能使用Json格式調用ExportToExcel功能,因爲返回的響應與json預期的響應之間會有衝突。