VB.Net Generate Excel Problem

kpao

Active member
Joined
Apr 3, 2006
Messages
29
Programming Experience
Beginner
I've already try generate excel by using C#, It works exactly.
VB.NET:
private void Button1_Click(object sender, System.EventArgs e)
  {
   Response.Clear();
   Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
   Response.Charset = "";
   Response.Cache.SetCacheability(HttpCacheability.NoCache);
   Response.ContentType = "application/vnd.xls";
   System.IO.StringWriter stringWrite = new System.IO.StringWriter();
   System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
   myDataGrid.RenderControl(htmlWrite);
   Response.Write(stringWrite.ToString());
   Response.End();
  }

However, I convert this code into VB.Net. It doesn't work. The following is my VB Code:
VB.NET:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Response.Clear()
        Page.EnableViewState = False
        Response.ContentType = "application/vnd.ms-excel"
        Response.AddHeader("Content-Disposition", "attachmentfilename=""YourReportName.xls""")
        Response.Charset = ""
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        Response.ContentType = "application/vnd.xls"
        Dim stringWrite = New System.IO.StringWriter
        Dim htmlWrite = New HtmlTextWriter(stringWrite)
        myDataGrid.RenderControl(htmlWrite)
        Response.Write(stringWrite.ToString())
        Response.End()
    End Sub

What's wrong with my VB Code? :confused:
 
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls")
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = "application/vnd.xls"
Dim stringWrite As System.IO.StringWriter = New System.IO.StringWriter()
Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)
myDataGrid.RenderControl(htmlWrite)
Response.Write(stringWrite.ToString())
Response.End()
End Sub
 
Back
Top