Pass value into ExcelTableCreated sub

3 posts, 1 answers
  1. Brendan
    Brendan avatar
    68 posts
    Member since:
    Dec 2011

    Posted 29 Apr 2013 Link to this post

    I've read through the examples and forums and can't find an answer.  In the code below you can see that a new row is added to the spreadsheet showing the "Exported" time and date.  It is also formatted.  That works fine.

    What I want to do is pass a string into the sub that can be used in the AddCustomExcelRow to describe the contents of the grid that is being exported.  Assume I have a grid showing "Top Investments".  I would like to pass that string into the sub below and use it as a new row.  I would want to pass a descriptive string from any grid I choose.  Any ideas?


    Public Sub exporter_ExcelTableCreated(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.Export.ExcelML.ExcelTableCreatedEventArgs)
     
        Dim subHeader As String = "Exported " & Now().ToString
        Dim style1 As ExcelML.SingleStyleElement = (CType(sender, ExportToExcelML)).AddCustomExcelRow(e.ExcelTableElement, 0, subHeader)
        style.FontStyle.Bold = True
        style.FontStyle.Size = 11
        style1.FontStyle.Color = Color.RoyalBlue
  2. Answer
    Svett
    Admin
    Svett avatar
    728 posts

    Posted 02 May 2013 Link to this post

    Hello Brendan,

    You can achieve that by extending the ExportToExcelML class in the following manner:
    Public Class MyExportToExcelML
        Inherits ExportToExcelML
        Private m_caption As String
     
        Public Sub New(grid As RadGridView)
     
            MyBase.New(grid)
        End Sub
     
        Public Property Caption() As String
            Get
                Return Me.m_caption
            End Get
            Set
                Me.m_caption = value
            End Set
        End Property
    End Class

    Then you can use the class in the following manner:
    Dim impl As New MyExportToExcelML(Me.radGridView1)
    impl.Caption = "Your caption"
    AddHandler impl.ExcelTableCreated, AddressOf impl_ExcelTableCreated
    impl.RunExport("C:\\test.xlx")

    Private Sub impl_ExcelTableCreated(sender As Object, e As Telerik.WinControls.UI.Export.ExcelML.ExcelTableCreatedEventArgs)
        Dim exporter As MyExportToExcelML = TryCast(sender, MyExportToExcelML)
        Dim subHeader As String = exporter.Caption
        Dim style As SingleStyleElement = DirectCast(sender, ExportToExcelML).AddCustomExcelRow(e.ExcelTableElement, 0, subHeader)
        style.FontStyle.Bold = True
        style.FontStyle.Size = 11
        style.FontStyle.Color = Color.RoyalBlue
    End Sub

    I hope that you find this information useful.

    Kind regards,
    Svett
    the Telerik team
    WinForms Q1 2013 boasts PivotGrid, PDF Viewer, Chart enhancements and more. Check out all of the latest highlights.
  3. Brendan
    Brendan avatar
    68 posts
    Member since:
    Dec 2011

    Posted 02 May 2013 Link to this post

    Thanks so much Svett.  That worked perfectly.  In case anyone is interested I found you can also add a border in the ExcelTableCreated event;

    'add header row
    Dim sHeader As String = exporter.Caption
     
    'set header caption
    Dim style1 As ExcelML.SingleStyleElement = DirectCast(sender, ExportToExcelML).AddCustomExcelRow(e.ExcelTableElement, 0, sHeader)
    With style1
        .FontStyle.Bold = True
        .FontStyle.Size = 14
        .FontStyle.Color = Color.Blue
        .InteriorStyle.Color = Color.FromArgb(240, 240, 240)
        .InteriorStyle.Pattern = ExcelML.InteriorPatternType.Solid
    End With
     
    'add subheader row
    Dim subHeader As String = "Exported " & Now().ToString
    Dim style2 As ExcelML.SingleStyleElement = (CType(sender, ExportToExcelML)).AddCustomExcelRow(e.ExcelTableElement, 0, subHeader)
     
    'create border, add to row
    Dim border As ExcelML.BorderStyles = New ExcelML.BorderStyles()
    border.Color = Color.Black
    border.Weight = 1
    border.LineStyle = ExcelML.LineStyle.Continuous
    border.PositionType = ExcelML.PositionType.Bottom
    With style2
        .FontStyle.Bold = True
        .InteriorStyle.Color = Color.FromArgb(240, 240, 240)
        .InteriorStyle.Pattern = ExcelML.InteriorPatternType.Solid
        .Borders.Add(border)
    End With
Back to Top