0

How to export Gridview data to csv with gridview styles.Below code will do for Excel but i want to download CSV.

    Response.Clear()
    Response.Buffer = True
    Response.AddHeader("content-disposition", "attachment;filename= ABCReport.xls")
    Response.Charset = ""
    Response.ContentType = "application/vnd.ms-excel"
    Using sw As New StringWriter()
        Dim hw As New HtmlTextWriter(sw)
        DGReport.RenderControl(hw)
        'style to format numbers to string
        Dim style As String = "<style> .textmode { } </style>"
        Response.Write(style)
        Response.Output.Write(sw.ToString())
        Response.Flush()
        Response.[End]()
    End Using
aadi
  • 59
  • 5
  • 2
    Start using a specialized library for creating Excel files, like [EPPlus](https://github.com/JanKallman/EPPlus). [Example here](https://www.vanderwaal.eu/mini-projecten/epplus-export-list-t-to-excel), [here](https://stackoverflow.com/a/47293207/5836671) and [here](https://stackoverflow.com/questions/52002573/send-excel-email-attachment-c-sharp). All you are doing now is creating a HTML page with an .xls extension. – VDWWD Dec 26 '21 at 12:40
  • 2
    CSV files don't have formatting and styles. A CSV file is a plain Jane text file of comma delimited values that Excel or even notepad can open. – Albert D. Kallal Dec 26 '21 at 17:46

0 Answers0