如何在Sencha / Ext JS下载Excel文件?

时间:2017-01-18 07:13:30

标签: javascript c# extjs

在浏览器端的Exjs中保存Excel文件时遇到问题。

下面是我的C#代码,用于将extJS App作为Excel文件提供响应。

我使用C#代码转换我的Excel数据表e,exportToExcel()函数返回Excel文件作为对ext JS应用程序请求的响应。

 public void exportToExcel(string userAutoId)
    {
        DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter("getContacts", con);
        da.SelectCommand.CommandType = CommandType.StoredProcedure;
        da.SelectCommand.Parameters.AddWithValue("@userAutoId", userAutoId);
        //cmd.Parameters.Add("@retValue", System.Data.SqlDbType.VarChar).Direction = System.Data.ParameterDirection.ReturnValue; 
        da.Fill(dt);

        //Create a dummy GridView
        GridView GridView1 = new GridView();
        GridView1.AllowPaging = false;
        GridView1.DataSource = dt;
        GridView1.DataBind();

        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition",
         "attachment;filename=DataTable.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            //Apply text style to each Row
            GridView1.Rows[i].Attributes.Add("class", "textmode");
        }
        GridView1.RenderControl(hw);

        //style to format numbers to string
        string style = @"<style> .textmode { mso-number-format:\@; } </style>";
        Response.Write(style);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
    }

这是我的Ext JS代码

                handler: function () {
                    //me = this;
                    var hfUserAutoId = me.InfoPanel.getForm().findField('hfUserAutoId').getValue();

                    var form = me.InfoPanel.getForm();
                    if (form.isValid()) {
                        form.submit({
                            url: 'getCSV.aspx',
                            method: 'POST',
                            params: { "userAutoId": hfUserAutoId },
                            waitMsg: 'Downloading your File...',
                            success: function (fp, o) {

                                                // Heare I want to do code for download respoce file

                                me.fireEvent('CloseThisEx');

                            }
                        });
                    }
                }

            }

1 个答案:

答案 0 :(得分:0)

您应该使用window.open使用浏览器下载功能。 C#route必须有GET方法,因此userAutoId和form值应该成为查询参数,然后ExtJS代码将是:

            handler: function () {
                var hfUserAutoId = me.InfoPanel.getForm().findField('hfUserAutoId').getValue(),
                    form = me.InfoPanel.getForm();

                if (form.isValid()) {
                    var params = Ext.merge(form.getValues(), {userAutoId: hfUserAutoId});
                    window.open('getCSV.aspx?' + Ext.Object.toQueryString(params), '_blank')
                }
            }