将数据集导出到Excel并从asp.net Web方法引发文件下载对话框

时间:2021-11-20 01:58:14

I am using the following code to export a data set to an Excel sheet.

我使用以下代码将数据集导出到Excel工作表。

[WebMethod]
    public static void ExporttoExcel()
    {
        DataSet ds;
       productfactory pf=new productfactory();
        ds = pf.getproducts();
        HttpResponse response = HttpContext.Current.Response;

        // first let's clean up the response.object
        response.Clear();
        response.Charset = "";
        response.ContentEncoding = System.Text.Encoding.Default;

        // set the response mime type for excel
        response.ContentType = "application/vnd.ms-excel";
        response.AddHeader("Content-Disposition", "attachment;filename=\"products.xls\"");

        // create a string writer
        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                // instantiate a datagrid
                DataGrid dg = new DataGrid();                    
                dg.DataSource = ds.Tables[0];
                dg.DataBind();
                dg.RenderControl(htw);
                string filepath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\products.xls";                    
                response.Write(sw.ToString());
               // response.End();

            }
        }       
    }

The problem is that it's not raising file download and hence no export is taking place. The same code works fine in a normal method. But with the web method it's not working.

问题是它没有提高文件下载,因此没有出口。相同的代码在普通方法中工作正常。但是使用网络方法它不起作用。

3 个解决方案

#1


2  

I suggest to make an HttpHandler ending in ashx, and place inside him your code that create the excel file.

我建议让一个HttpHandler以ashx结尾,并在其中放置你创建excel文件的代码。

then call it from your javascript code like that.

然后从你的javascript代码中调用它。

document.location.href = "ExporttoExcel.ashx";

#2


1  

The problem is that WebMethods are not designed to allow you to interact with the Response object (evident in that it wasn't available and you had to use HttpContext.Current.Response to get to it). WebMethods are designed to be blackbox to the user. They will perform and action and/or return a value.

问题是WebMethods的设计并不允许您与Response对象进行交互(很明显,它不可用,您必须使用HttpContext.Current.Response来实现它)。 WebMethods被设计为用户的黑盒子。他们将执行和行动和/或返回一个值。

Perhaps you can give us a better idea of what you are trying to accomplish and we can suggest an alternate solution.

也许你可以让我们更好地了解你想要实现的目标,我们可以建议一个替代解决方案。

#3


-1  

u can use to create a dynamic iframe with URL set to the Web Handler to generate the Excel this will raise the file download with out posting the current page.

您可以使用创建动态iframe,并将URL设置为Web处理程序以生成Excel,这将提高文件下载而不发布当前页面。

#1


2  

I suggest to make an HttpHandler ending in ashx, and place inside him your code that create the excel file.

我建议让一个HttpHandler以ashx结尾,并在其中放置你创建excel文件的代码。

then call it from your javascript code like that.

然后从你的javascript代码中调用它。

document.location.href = "ExporttoExcel.ashx";

#2


1  

The problem is that WebMethods are not designed to allow you to interact with the Response object (evident in that it wasn't available and you had to use HttpContext.Current.Response to get to it). WebMethods are designed to be blackbox to the user. They will perform and action and/or return a value.

问题是WebMethods的设计并不允许您与Response对象进行交互(很明显,它不可用,您必须使用HttpContext.Current.Response来实现它)。 WebMethods被设计为用户的黑盒子。他们将执行和行动和/或返回一个值。

Perhaps you can give us a better idea of what you are trying to accomplish and we can suggest an alternate solution.

也许你可以让我们更好地了解你想要实现的目标,我们可以建议一个替代解决方案。

#3


-1  

u can use to create a dynamic iframe with URL set to the Web Handler to generate the Excel this will raise the file download with out posting the current page.

您可以使用创建动态iframe,并将URL设置为Web处理程序以生成Excel,这将提高文件下载而不发布当前页面。