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
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