简体   繁体   English

ASp.NET:建议一个最佳选项,以允许用户以Excel格式在客户端下载数据网格中的数据

[英]ASp.NET: suggest a best option to allow the user to download the data in the datagrid in the client side in Excel format

Please suggest the best option to allow the user to download the data in the datagridView from the page in excel format to the client machine. 请提出最佳选择,以允许用户以excel格式将datagridView中的数据从页面下载到客户端计算机。

I have a dataset that is binded to the datagrid view.Even it can be downloading the data in the dataset to an excel in the client side. 我有一个绑定到datagrid视图的数据集,甚至可以将数据集中的数据下载到客户端的Excel中。

PLease suggest whether this it is possible to generate an excel and allow the user to download it in client side ? 请问是否有可能生成一个excel并允许用户在客户端下载它?

I am using asp.Net,C#,SQL server,Excel. 我正在使用asp.Net,C#,SQL Server,Excel。

Please help ! 请帮忙 !

I suggest you output a Comma Separated Values (CSV) file. 我建议您输出逗号分隔值(CSV)文件。 They are relatively easy to code up a solution for, you can write them out directly without having to do interop, and they open directly in Excel. 它们相对容易地为解决方案编写代码,您可以将它们直接写出而无需进行互操作,并且它们可以在Excel中直接打开。

Excel can read HTML tables directly into a spreadsheet. Excel可以将HTML表直接读取到电子表格中。 You just need to send the same data down which is displayed in the web page (minus eg hyperlinks) with a Content-Disposition header set to attachment and a suitable mime-type which causes the client to open Excel. 您只需要向下发送网页中显示的相同数据(减去例如超链接),并将Content-Disposition标头设置为attachment并使用合适的mime类型使客户端打开Excel。 Excel then reads the downloaded HTML into a worksheet. 然后,Excel将下载的HTML读入工作表。

We do that in a few of our applications. 我们在一些应用程序中做到了这一点。 It's really not difficult: We use the same aspx page (we send it a parameter to indicate that we want an excel file generated from the aspx), with a few lines of code that make the aspx be generated as an xls file. 这实际上并不难:我们使用相同的aspx页面(向其发送参数以表明我们希望从aspx生成一个excel文件),并使用几行代码将aspx生成为xls文件。

  1. The first lines of code in the Page_Load method should be: Page_Load方法中的第一行代码应为:

     //open as excell file Response.Buffer = true; Response.ContentType = "application/vnd.ms-excel"; string FileName = this.Page.ToString().Replace("ASP.", "") + ".xls"; // so the file can be saved as excel Response.AppendHeader("content-disposition", "filename=\\"" + FileName + "\\";attachment"); 
  2. It's important that when in this mode, you make sure to disable ViewState. 在此模式下,请务必禁用ViewState,这一点很重要。 Otherwise it could completely confuse Excel and generate errors. 否则可能会完全混淆Excel并产生错误。

That's all we do, and it works wonderfully. 这就是我们要做的,而且效果很好。

I should add that the Microsoft recommendation is to write the GridView into a HtmlTextWriter when exporting it to Excel, in order to make sure that the HTML generated is one that Excel will understand. 我应该补充一点,微软的建议是在将GridView导出到Excel时将其写入HtmlTextWriter,以确保生成的HTML是Excel可以理解的HTML。 It's easy to do (except for an exception that is generated, but is easy to overcome), and is explained nicely in these 2 articles: Gridview Export to Excel , and Extensive Study of GridView Export to Excel 这很容易做到(除了生成的异常,但很容易克服),在以下两篇文章中对此进行了很好的解释: Gridview导出到ExcelGridView导出到Excel的广泛研究

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM