You must Sign In to post a response.
  • Category: ASP.NET

    Export gridview to csv

    I am trying export gridview to csv.I am adding actual record into cell of excel not trying html to excel.I am executing sp,taking record into datatable.Looping through datatable,writing into file using streamwriter.But problem occur when my column has long number,csv shows 890+32 like this.I don't want like this,want actual number like 89012345676898899998776766544333445556677.How to do that?I am not using Gridview.RenderControl(htmltextwrtter).
  • #764480
    Most of the time field gets converted to some other format in CSV, there are couple of ways to resolve the issue.
    1. Open CSV in Text format
    1. Open empty excel file ? select New empty sheet
    2. Go to Data tab ? select from text and select CSV file
    3. Select 'Delimited' radio button and click on Next
    4. Select comma and click on next
    Select Text and select all columns in data preview with the help of SHIFT key and Click on finish
    5. Click on Next screen

    Other way is to write a double quote for number it will resolve your issue
    while writing CSV, write your number in below format, see below
    David,Sooo,="00023423",World

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #764496
    Actually "89012345676898899998776766544333445556677 " is converting into "890+32". While export you can covert that into the string then you export.
    By Nathan
    Direction is important than speed

  • #764521
    You should try cast the number into a String and add ' to excel or you can use given function to Export the GridView to Excel
    private void ExportGridToExcel()  
    {
    Response.Clear();
    Response.Buffer = true;
    Response.ClearContent();
    Response.ClearHeaders();
    Response.Charset = "";
    string FileName ="Vithal"+DateTime.Now+".xls";
    StringWriter strwritter = new StringWriter();
    HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType ="application/vnd.ms-excel";
    Response.AddHeader("Content-Disposition","attachment;filename=" + FileName);
    GridView1.GridLines = GridLines.Both;
    GridView1.HeaderStyle.Font.Bold = true;
    GridView1.RenderControl(htmltextwrtter);
    Response.Write(strwritter.ToString());
    Response.End();

    }

  • #764525
    Hi,

    Refer below link this might be helpful "support.3dcart.com/knowledgebase/article/View/619/7/why-does-my-exported-csv-data-get-converted-to-weird-formats"

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #764536
    hi pinky,
    After export, open your csv.
    Select whole column containing your numbers which it displaying in wrong format.
    Right click on it and select Format Cells.
    Select "Text" as cell format and click on OK.
    Hope it helps.
    Regards,
    Shashikant Gurav
    shashikantgurav22@gmail.com


Sign In to post your comments