Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding header to excel export vb.net

Status
Not open for further replies.

lisat76

Programmer
Sep 25, 2007
89
US
I have a event that exports my gridviews to excel. it works just fine however i was trying to find a way that i could add a header to the excel spreadsheet that the data goes to something like excel.header text =mytext......
I would also would like to add text to a particular cell in excel like cellM1.text =my text
I am using vb.net
Here is my current export code again this works just fine
Code:
    Sub doExcel(ByVal Source As Object, ByVal E As EventArgs)
        DisableControls(gviewReport)
        DisableControls(gviewproblems)
        If gviewReport.Rows.Count.ToString + gviewproblems.Rows.Count.ToString + 1 < 65536 Then
            gviewReport.AllowPaging = "False"
            gviewReport.AllowSorting = "False"
            gviewproblems.AllowPaging = "False"
            gviewproblems.AllowSorting = "False"
            gviewReport.DataBind()
            gviewproblems.DataBind()
            Dim tw As New StringWriter()
            Dim hw As New System.Web.UI.HtmlTextWriter(tw)
            Dim frm As HtmlForm = New HtmlForm()
            Response.ContentType = "application/vnd.ms-excel"
            Response.AddHeader("content-disposition", "attachment;filename=" & TxtFile.Text & ".xls")
            Response.Charset = ""
            EnableViewState = False
            Controls.Add(frm)
            frm.Controls.Add(gviewReport)
            frm.Controls.Add(gviewproblems)
            frm.RenderControl(hw)
            Response.Write(tw.ToString())
            Response.End()
            gviewReport.AllowPaging = "True"
            gviewproblems.AllowSorting = "True"
            gviewReport.DataBind()
            gviewproblems.DataBind()
        Else
            lblError.Text = "Too many rows - Export to Excel not possible"
        End If
    End Sub

    Private Sub DisableControls(ByVal gv As Control)
        Dim lb As LinkButton = New LinkButton
        Dim l As Literal = New Literal
        Dim name As String = String.Empty
        Dim i As Integer = 0
        Do While (i < gv.Controls.Count)
            If (gv.Controls(i).GetType Is GetType(LinkButton)) Then
                l.Text = CType(gv.Controls(i), LinkButton).Text
                gv.Controls.Remove(gv.Controls(i))
                gv.Controls.AddAt(i, l)
            ElseIf (gv.Controls(i).GetType Is GetType(DropDownList)) Then
                l.Text = CType(gv.Controls(i), DropDownList).SelectedItem.Text
                gv.Controls.Remove(gv.Controls(i))
                gv.Controls.AddAt(i, l)
            End If
            If gv.Controls(i).HasControls Then
                DisableControls(gv.Controls(i))
            End If
            i = (i + 1)
        Loop

        'Dim test As eWorld.UI.CalendarPopup
        'test.t()
    End Sub
 
Rather than trying to add data to the excel spreadsheet, add it to the dataset that is bound to the gridview. It will then be exported out to excel with the rest of your information.

[!]
-------------------------------------------------------

Mark,

Join me at the new IT Community of the 21st Century: [!][URL unfurl="true"]http://lessthandot.com[/!][/url]

[/!]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top