Friday, July 8, 2011

Excel Streaming Code

Here is my Level 5 Code on Excel Streaming.   As you can see it works to send a string to excel through the browser.   The user is then prompted to open or save the file.   You can use HTML to create excel output with formatting such as column colors, and fonts.   Try it out and let me know if it works for you.   It might do the trick to help you if you're interested in how to do this sort of thing.

------------------------------------------------------------------------------------------------------------------

Dim strPrintOut As String = BuildOutputString(StoredProcedureName, aryParameters())
                   
Call PrintToExcel(strPrintOut, "MyFirstExcelStreamingDoc", Me.Page)


        Public Sub PrintToExcel(ByVal strPrintOut As String, ByVal FileName As String, ByVal pg As Page)
            Try

                Dim worksheetTitle = ""
                Dim strHeader As String = ""

                worksheetTitle = "WS_" & FileName
                FileName &= FileName & ".xls"

                strHeader &= ""
                strHeader &= ""
                strHeader &= " "
                strHeader &= " "

                pg.Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", FileName))
                pg.Response.Charset = ""
                pg.Response.Cache.SetCacheability(HttpCacheability.NoCache)

                Dim stringWrite As System.IO.StringWriter = New System.IO.StringWriter
                Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)

                htmlWrite.Write(strPrintOut)

                pg.Response.Write(stringWrite.ToString)
                pg.Response.Write(" ")
                pg.Response.End()

            Catch ex As System.Threading.ThreadAbortException

            Catch ex As Exception
                MySession.BC.HandleMessage(ex, pg)
            End Try

        End Sub