By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,677 Members | 1,150 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,677 IT Pros & Developers. It's quick & easy.

Exporting in Excel using Response.ContentType = "application/ms-excel"

P: n/a
I am creating a TAB delimited file with .XLS extention using the Response.ContentType = "application/ms-excel" method. Normally I would place this on it's own form and delete all the HTML in the ASPX page - otherwise the aspx code gets appended to the end of the file out put to Excel. How do I produce the same output from a page where I cannot simply delete all the HTML code (must leave buttons and other objects intact)? In other words how can I keep the aspx code from being appended to the file that is sent to the client?

Here is the pertinent code (adapted from article by Steve C. Orr):

If custConnection.State = ConnectionState.Closed Then
custConnection.ConnectionString = "data source=OURSERVERINFOHERE;initial catalog=DB_NAME;integrated security=SSPI"
custConnection.Open()
End If
custAdapterCommand = New SqlClient.SqlDataAdapter("SELECT * FROM tMAIN WHERE DivisionDeptID = " & TextBox1.Text.ToString, custConnection)
custAdapterCommand.Fill(custDataSet, "tMain")
Response.ContentType = "application/ms-excel"
Response.AddHeader("Content-Disposition", "inline;filename=test.xls")
Response.Write(ConvertDtToTDF(custDataSet))

Private Function ConvertDtToTDF(ByVal dt As DataSet) As String
Dim dr As DataRow, ary() As Object, i As Integer
Dim iCol As Integer

'Output Column Headers
For iCol = 0 To dt.Tables(0).Columns.Count - 1
Response.Write(dt.Tables(0).Columns(iCol).ColumnNa me.ToString & vbTab)
Next
Response.Write(vbCrLf)

'Output Data
Dim aText As String = "" ' TESTING
For Each dr In dt.Tables(0).Rows
ary = dr.ItemArray
For i = 0 To UBound(ary)
'Response.Write(ary(i).ToString & vbTab)
aText += ary(i).ToString & vbTab
Next
'Response.Write(vbCrLf)
aText += vbCrLf
Next
Return aText
End Function
Nov 20 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.