导出到使用Json调用的函数不会以excel格式导出数据

时间:2014-01-29 20:10:35

标签: jquery asp.net json c#-4.0 export-to-excel

您好我正在使用json调用一个调用另一个函数的函数,该函数最终调用ExpotToExcel函数。功能如下

$.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表而没有错误。

1 个答案:

答案 0 :(得分:0)

经过大量研究后,我发现我们无法使用Json格式调用ExportToExcel功能,因为返回的响应与json期望的响应之间会发生冲突。