Click here to Skip to main content
14,825,162 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

I want to export the gridview data into .csv(comma separated values) file.
I have the code for exporting data into excel file as below:
protected void lnkExport_Click(object sender, EventArgs e)
    {
        string fileName = "attachment;filename=OverallReport_" + DateTime.Now.ToString("dd-MM-yyyy-hh-mm-ss") + ".xls";
        Response.Clear();
        Response.AddHeader("content-disposition", fileName);
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/excel";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        gvOverallReport.RenderControl(htmlWrite);
        string s_styleInfo = @"<style> td { mso-number-format:""" + "\\@" + @"""; }</style>";
        string s_excel = stringWrite.ToString();
        Response.Write(s_styleInfo + s_excel);
        Response.End();
}


html code for gridview(gvOverallReport):

XML
<asp:GridView ID="gvOverallReport" runat="server" AutoGenerateColumns="true">
                                           <PagerStyle CssClass="gridPagerStyle" HorizontalAlign="Center" Width="100%" />
                        <SelectedRowStyle CssClass="gridSelectedRowStyle" />
                        <HeaderStyle CssClass="gridHeader" BackColor="#AFB8BF" HorizontalAlign="Left" VerticalAlign="Middle"
                        BorderColor="#bbbbbb" BorderWidth="1px" Font-Bold="True" ForeColor="White" Font-Size="10pt" />
                        <AlternatingRowStyle BackColor="WhiteSmoke" />
                        <PagerSettings Position="TopAndBottom" />
                        <RowStyle CssClass="grdRow" />
                                           </asp:GridView>

--------

Its working perfectly for exporting into .xls file.. But I want a little modification in the same code for exporting into .csv file..
If I change the extension as .csv instead of .xls, its displaying only the html tags in the downloaded file..

Can anyone please suggest me..

Thanks & Regards,
Posted
Updated 18-Feb-11 19:15pm
v5

1 solution

Hi,

For comma separator csv file you have to write your csv file. find code on net.
   
Comments
Raj.rcr 18-Feb-11 23:31pm
   
I tried yesterday... But I didnt get.. still I am trying for the same.. Can u plz gimme the idea of writing the csv file?
Raj.rcr 18-Feb-11 23:52pm
   
can anyone please give me an idea for this...?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900