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

    Export to excel in vb.net 2003

    I need code for export to excel from grid in vb.net 2003
  • #648480
    Please check the following code:


    Public Overloads Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
    ' Verifies that the control is rendered
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Response.Clear()
    Response.Buffer = True
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls")
    Response.Charset = ""
    Response.ContentType = "application/vnd.ms-excel"
    Dim sw As New StringWriter()
    Dim hw As New HtmlTextWriter(sw)
    GridView1.AllowPaging = False
    GridView1.DataBind()
    GridView1.RenderControl(hw)
    Response.Output.Write(sw.ToString())
    Response.Flush()
    Response.End()
    End Sub


    Thanks & Regards
    Paritosh Mohapatra
    Microsoft MVP (ASP.Net/IIS)
    DotNetSpider MVM

  • #648628
    Hello,

    check below code:

    var dataTable = (DataTable)dataGrid.DataSource;

    var ef = new ExcelFile();
    var ws = ef.Worksheets.Add(dataTable.TableName);
    ws.InsertDataTable(dataTable, "A1", true);
    Response.Clear();
    Response.ContentType = "application/vnd.ms-excel";
    Response.AddHeader("Content-Disposition", "attachment; filename=test.xls");
    ef.SaveXls(Response.OutputStream);
    Response.End();

    Regards
    Mr.Rajesh
    Software Engg.

  • #648651
    Hi lakshmi,

    Refer the below vb.net code


    Protected Sub btnExport_Click(sender As Object, e As EventArgs)

    DataTable SampleData=(DataTable)YourGridview.Datasource
    Dim GridView1 As New GridView()
    GridView1.AllowPaging = False
    GridView1.DataSource = DirectCast(SampleData, DataTable)
    GridView1.DataBind()
    Response.Clear()
    Response.Buffer = True
    Response.AddHeader("content-disposition", "attachment;filename=EnterYourFileName.xls")
    Response.Charset = ""
    Response.ContentType = "application/vnd.ms-excel"
    Dim sw As New StringWriter()
    Dim hw As New HtmlTextWriter(sw)
    For Each row As GridViewRow In GridView1.Rows
    For Each tc As TableCell In row.Cells
    tc.Attributes.Add("class", "textmode")
    Next
    Next
    'for formating your excel sheet back color and header row back color
    GridView1.HeaderRow.BackColor = System.Drawing.Color.Aqua
    GridView1.BackColor = System.Drawing.Color.Beige
    '
    GridView1.RenderControl(hw)
    Dim style As String = "<style> .textmode { mso-number-format:\@; } </style>"
    Response.Write(style)
    Response.Output.Write(sw.ToString())
    Response.Flush()
    Response.Close()
    Response.[End]()

    End Sub



    Hope this will help you

    Regards,
    SonyShiva
    Never lose hope..You never know what tomorrow will bring


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.