Click here to Skip to main content
15,896,063 members
Articles / Web Development / ASP.NET

Excel Export Component Using XSL

Rate me:
Please Sign up or sign in to vote.
4.55/5 (34 votes)
22 Oct 2006CPOL3 min read 376.3K   12.2K   165  
This is an article on an Excel export component (using XSL and XML) in .NET.
Imports System.xml

Partial Class Example2
    Inherits System.Web.UI.Page

    Dim objExport As ExportToExcel.ExcelExport

    Protected Sub cmdExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdExport.Click
        Dim strSql As String
        Dim strCon As String
        Dim strExcelFile As String
        Dim dsOrders As DataSet
        Dim XMLDoc As XmlDataDocument
        Dim b As XmlNode
        Dim a As XmlNode

        Try

            strSql = "select ord.orderid,ord.EmployeeID, ordDet.ProductID, ordDet.UnitPrice, ordDet.Quantity, "
            strSql = strSql & " ordDet.Discount from orders ord inner join ""order details"" ordDet "
            strSql = strSql & "on ord.orderid = ordDet.orderid where customerID = 'ALFKI' order by ord.orderid"

            strCon = System.Configuration.ConfigurationManager.AppSettings("ConStr")

            dsOrders = SqlHelper.ExecuteDataset(strCon, CommandType.Text, strSql)

            XMLDoc = New XmlDataDocument(dsOrders)
            a = XMLDoc.CreateElement("CustomerDetails")
            a.InnerXml = "<CustomerId>ALFKI</CustomerId><CustomerNm>Alfreds Futterkiste</CustomerNm><ContactNm>Maria Anders</ContactNm><City>Berlin</City>"

            XMLDoc.DataSet.EnforceConstraints = False

            b = XMLDoc.DocumentElement.FirstChild

            XMLDoc.DocumentElement.InsertBefore(a, b)

            strExcelFile = objExport.TransformXMLDocumentToExcel(XMLDoc, "Example2.xsl")

            objExport.SendExcelToClient(strExcelFile)

        Catch ex As Threading.ThreadAbortException
            'Do nothing
        Catch ex As Exception
            Response.Write(ex.ToString)
        End Try
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        objExport = New ExportToExcel.ExcelExport

        objExport.TempFolder = "\Excel\Temp\"
        objExport.TemplateFolder = "\Excel\Template\"
        objExport.XSLStyleSheetFolder = "\Excel\XSLStyleSheet\"

        objExport.CleanUpTemporaryFiles()
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions