locked
Export to csv RRS feed

  • Question

  • User-1971168174 posted

    Hi,

    For Some reasons my export to csv is working but gridview headers are not exported...so i am only getting data and column headings are blank

    My Code:

     protected void btnexport_Click(object sender, EventArgs e)
            {
    
    
                // working
                Response.Clear();
                Response.Buffer = true;
                Response.AddHeader("content-disposition", "attachment;filename=gvtocsv.csv");
                Response.Charset = "";
                Response.ContentType = "application/text";
                StringBuilder sBuilder = new System.Text.StringBuilder();
                for (int index = 0; index < GridView1.Columns.Count; index++)
                {
                    sBuilder.Append(GridView1.Columns[index].HeaderText + ',');
                }
                sBuilder.Append("\r\n");
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    for (int k = 0; k < GridView1.HeaderRow.Cells.Count; k++)
                    {
                        sBuilder.Append(GridView1.Rows[i].Cells[k].Text.Replace(",", "") + ",");
                    }
                    sBuilder.Append("\r\n");
                }
                Response.Output.Write(sBuilder.ToString());
                Response.Flush();
                Response.End();
    
    
    
            }

    Thanks in advance for help!

    Monday, September 14, 2020 9:58 AM

Answers

  • User-939850651 posted

    Hi neerahkumarmodi,

    I created a simple example using your code, and it can get the column header and table data correctly. But I'm not sure about the various property settings in your grid view, so I can't determine your problem.

    If possible, could you post the page code ?

    This is my test example:

    <body>
        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server"
                    CellPadding="6" ForeColor="#333333" GridLines="None">
                    <AlternatingRowStyle BackColor="White" />
                    <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                    <RowStyle BackColor="#E3EAEB" />
                    <Columns>
                        <asp:BoundField DataField="id" HeaderText="id" />
                        <asp:BoundField DataField="Name" HeaderText="Name" />
                        <asp:BoundField DataField="Age" HeaderText="Age" />
                        <asp:BoundField DataField="Gender" HeaderText="Gender" />
                        <asp:BoundField DataField="City" HeaderText="City" />
                    </Columns>
                </asp:GridView>
                <br />
                <asp:Button ID="btnexport" runat="server" Text="Export" OnClick="btnexport_Click" />
            </div>
        </form>
    </body>
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack) {
                    bindGrid(); 
                }
            }
    
            private void bindGrid() {
                DataTable dt = new DataTable();
                string con = ConfigurationManager.ConnectionStrings["conStr"].ToString();
                using (SqlConnection conn = new SqlConnection(con)) {
                    string query = "select * from tbl_default";
                    using (SqlCommand cmd = new SqlCommand(query,conn)) {
                        SqlDataAdapter sda = new SqlDataAdapter(cmd);
                        sda.Fill(dt);
                        GridView1.DataSource = dt;
                        GridView1.DataBind();
                    }
                }
            }
    
            protected void btnexport_Click(object sender, EventArgs e)
            {
                Response.Clear();
                Response.Buffer = true;
                Response.AddHeader("content-disposition", "attachment;filename=gvtocsv.csv");
                Response.Charset = "";
                Response.ContentType = "application/text";
                StringBuilder sBuilder = new StringBuilder();
                for (int index = 0; index < GridView1.Columns.Count; index++)
                {
                    sBuilder.Append(GridView1.Columns[index].HeaderText + ',');
                }
                sBuilder.Append("\r\n");
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    for (int k = 0; k < GridView1.HeaderRow.Cells.Count; k++)
                    {
                        sBuilder.Append(GridView1.Rows[i].Cells[k].Text.Replace(",", "") + ",");
                    }
                    sBuilder.Append("\r\n");
                }
                Response.Output.Write(sBuilder.ToString());
                Response.Flush();
                Response.End();
            }

    Result:

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 15, 2020 2:54 AM