简体   繁体   English

如何将WebDataGridview的数据导出到现有Excel工作表?

[英]How to export data of WebDataGridview to an existing Excel sheet?

我有一个Webdatagridview,我想将它的数据(不包括标题,因为我的excel文件已经有标题)导出到我现有的Excel文件中。

If you are using the Infragistics WebDataGrid, you can use the WebExcelExporter and use an overload for the Export method that takes and instance of a workbook or worksheet. 如果使用的是Infragistics WebDataGrid,则可以使用WebExcelExporter并为采用工作簿或工作表实例的Export方法使用重载。

For preventing the headers set the ShowHeader property of the WebDataGrid that you are exporting to false before calling Export. 为了防止标题,在调用Export之前,将要导出的WebDataGrid的ShowHeader属性设置为false。 Note that this will not need to be set back to true since the response is being changed to send the excel file to the client so there is no state information for the grid sent to the client for this request. 请注意,由于将响应更改为将excel文件发送到客户端,因此不需要将其重新设置为true,因此对于此请求发送到客户端的网格没有状态信息。

try: 尝试:

 protected void ExportToExcel()
 {
        Response.Clear();
        Response.Buffer = true;
        Response.ContentType = "application/vnd.ms-excel";
        Response.AddHeader("content-disposition", "attachment;filename=YourFiles.xls");
        Response.Charset = "";
        this.EnableViewState = false;

        System.IO.StringWriter sr = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htw= new System.Web.UI.HtmlTextWriter(sr);

        gvFiles.RenderControl(htw);

        Response.Write(sw.ToString());
        Response.End();
    }

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

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