简体   繁体   中英

Gridview to Excel

I have a gridview with AutoGenerateDeleteButton

在此输入图像描述

And i export this gridview simple this code..

Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=Avukat.xls");
        Response.Charset = "";

        Response.ContentType = "application/vnd.xls";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        GridView1.RenderControl(htmlWrite);
        Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
        Response.Write(stringWrite.ToString());
        Response.End();

There is no problem with that.

But in the excel there is a delete column :))

在此输入图像描述

How can i delete the delete column in excel?

Your problem is you want 2 x "views" of your grid. With & without the delete column.

One for the browser, and one for Excel.

On your export, drop the delete column. Something something like:

    GridView1.AutoGenerateDeleteButton = false;
    GridView1.DataBind(); 
    GridView1.RenderControl(htmlWrite);
    Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");

您需要在导出之前从GridView删除该列。

...Or, you can Export to a real Excel 2007 .xlsx file the proper way, using the OpenXML libraries.

Using this C# library, you just need to include the CreateExcelFile class in your application, and call one function:

DataSet ds = CreateSampleData();
CreateExcelFile.CreateExcelDocument(ds, "C:\\Sample.xlsx");

Full source code, and a demo application are provided, free of charge.
http://www.mikesknowledgebase.com/pages/CSharp/ExportToExcel.htm

Good luck !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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