简体   繁体   中英

Display columns and rows after exporting data from gridview to excel

Hi all i am having a task to export gridview data to excel which i have done using the forums and articles available.

But i would like to display complete excel columns after the data was imported to excel, means expect the place occupied by the grid content i would like to display the remaining columns of the excel as it is.

Sample the normal way of exporting the we do in common is as follows

在此处输入图片说明

My requirement is to show as follows

在此处输入图片说明

I followed as per here

http://www.aspsnippets.com/Articles/Export-GridView-To-Word-Excel-PDF-CSV-Formats-in-ASP.Net.aspx

You can use the following code to do it

protected void btnExportCSV_Click(object sender, EventArgs e)

{

    Response.Clear();

    Response.Buffer = true;

    Response.AddHeader("content-disposition",

     "attachment;filename=GridViewExport.csv");

    Response.Charset = "";

    Response.ContentType = "application/text";



    GridView1.AllowPaging = false;

    GridView1.DataBind();



    StringBuilder sb = new StringBuilder();

    for (int k = 0; k < GridView1.Columns.Count; k++)

    {

        //add separator

        sb.Append(GridView1.Columns[k].HeaderText + ',');

    }

    //append new line

    sb.Append("\r\n");

    for (int i = 0; i < GridView1.Rows.Count; i++)

    {

        for (int k = 0; k < GridView1.Columns.Count; k++)

        {

            //add separator

            sb.Append(GridView1.Rows[i].Cells[k].Text + ',');

        }

        //append new line

        sb.Append("\r\n");

    }

    Response.Output.Write(sb.ToString());

    Response.Flush();

    Response.End();

}
-->Return only hearder,not view data.Help Me !
Thank you !
Email:tsmcstcd@gmail.com

查看要导出的代码,您已经删除了代码中生成的Excel边框。

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