Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 27th, 2007, 10:38 PM
Authorized User
Join Date: Aug 2007
Location: , , Singapore.
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to yukijocelyn
Default Exporting gridview into excel


I have another problem here. I need to export the details in my gridview into an excel file. However, I would like this to be done in the background. I have searched and tried alot of codes online, however, almost all of them requires the user to confirm whether they want to save or open. I need it to save in the background without them even knowing that the excel is save there. The codes that I have now are as below:

Protected Sub lblFind_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim selectedDate As DateTime
        Dim selectedCompany As String

        selectedDate = ddlDateCreated.SelectedValue
        selectedCompany = ddlCompany.SelectedValue.ToString

        Dim dbconn As OleDbConnection
        Dim sql As String
        Dim dbcomm As OleDbCommand
        Dim dataAdap As OleDbDataAdapter
        Dim ds As DataSet

        dbconn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & Server.MapPath("App_Data/DB.mdb"))
        sql = "SELECT number, dNo, FullName, acct, passwd, cost FROM table WHERE o LIKE '" + selectedCompany + "%'" + " and DateCreated LIKE '" + selectedDate + "%'"

        dbcomm = New OleDbCommand(sql, dbconn)
        dataAdap = New OleDbDataAdapter(dbcomm)
        ds = New DataSet

            Dim totnumrecs As Integer = dataAdap.Fill(ds)

            If totnumrecs > 0 Then
                lblEmailTo.Visible = True
                ddlEmailTo.Visible = True
                cmdSend.Visible = True
                lblRemarks.Visible = True
                txtRemarks.Visible = True
                GridView1.DataSource = ds
                lblEmailTo.Visible = False
                ddlEmailTo.Visible = False
                cmdSend.Visible = False
                lblRemarks.Visible = False
                txtRemarks.Visible = False
            End If

        Catch ex As Exception
            lblEmailTo.Visible = False
            ddlEmailTo.Visible = False
            cmdSend.Visible = False
            MsgBox("No records found for " + selectedCompany + " and of date " + selectedDate, MsgBoxStyle.OkOnly, "No Records Found")
        End Try

    End Sub

    Protected Sub cmdSend_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        GridView1.AllowPaging = "False"
        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=tryingexcel.xls")
        Response.Charset = ""
        EnableViewState = True
        GridView1.AllowPaging = "True"

    End Sub

the problem here is that what my excel have saved is not the details of the data, instead it saved:

<form name="ctl01" method="post" action="try.aspx" id="ctl01">
<input type="hidden" name="__VIEWSTATE" id="
__VIEWSTATE" value="" />

I do not understand where's my error. I tried understanding the codes, however, I must admit that some of the codes in cmdSend, I don't really understand. Please help.
Thank you!
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in exporting data in a gridview to MSExcel Rashmi_Dubey ASP.NET 2.0 Basics 12 July 15th, 2009 04:57 PM
Exporting GridView to MS word sureshbabu C# 2005 0 August 29th, 2008 05:39 AM
Exporting Gridview to CSV kadjw ASP.NET 2.0 Basics 2 May 17th, 2008 06:30 AM
Exporting gridview to pdf format priyan.viji C# 2005 0 December 12th, 2007 06:30 AM

All times are GMT -4. The time now is 08:25 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.