2

Searched and found the following solution Here Users have given up vote to it but it throwing an exception for me.

MultiFileExportToExcel Unable to cast object of type 'DevExpress.XtraGrid.Views.Grid.GridView' to type 'DevExpress.XtraPrinting.IPrintable'. Unable to cast object of type 'DevExpress.XtraGrid.Views.Grid.GridView' to type 'DevExpress.XtraPrinting.IPrintable'.

Full code is here:

 Public Sub MultiFileExportToExcel()
    Using saveDialog = New SaveFileDialog()
        saveDialog.Filter = "Excel (.xlsx)|*.xlsx"
        If saveDialog.ShowDialog() = DialogResult.OK Then
            Dim printingSystem = New PrintingSystemBase()
            Dim compositeLink = New CompositeLinkBase()
            compositeLink.PrintingSystemBase = printingSystem

            Dim link1 = New PrintableComponentLinkBase()
            link1.Component = CType(GridView1, IPrintable)
            Dim link2 = New PrintableComponentLinkBase()
            link2.Component = CType(GridView2, IPrintable)

            compositeLink.Links.Add(link1)
            compositeLink.Links.Add(link2)
            Dim options = New XlsxExportOptions()

            options.ExportMode = XlsxExportMode.SingleFilePageByPage
            compositeLink.CreatePageForEachLink()
            compositeLink.ExportToXlsx(saveDialog.FileName, options)
        End If
    End Using
End Sub

Thank in advance..

Community
  • 1
  • 1
newcommer
  • 29
  • 8
  • `CType(GridView1, IPrintable)` is the error im guessing. Looking at the solution I think you should just use `GridView1`. This is the solution code; `link1.Component = grid1;` as you can see there is no casting. – Bugs Feb 12 '17 at 22:06
  • Got same error. – newcommer Feb 12 '17 at 22:20
  • Just to be sure, you changed the code for both `link1.Component = GridView1` _and_ `link2.Component = GridView2`? – Bugs Feb 12 '17 at 22:21
  • Yes, I have changed it. – newcommer Feb 12 '17 at 22:26
  • https://www.dropbox.com/s/acp3ce8rzk2w42t/Capture.PNG?dl=0 – newcommer Feb 12 '17 at 22:27
  • I can't click on that link. I'm seeing slight differences in the solution code to yours. For example are you definitely using a `GridControl` rather than a `GridView`? Maybe that's causing an issue? – Bugs Feb 12 '17 at 22:29
  • 1
    that's it. Thank you it worked. I wasted my 2 to 3 hours to find out a solution for it. that shows, I am new to coding :) – newcommer Feb 12 '17 at 22:39
  • Is there any way to give name to each worksheet that exported using above code? Like I exported excel file (named myReport.xlsx) that have 10 sheets called sheet1, sheet2 sheet3 and so on. – newcommer Feb 13 '17 at 20:06
  • I would look at creating a new question @JagjeetSingh. It's the way things work here at SO. – Bugs Feb 13 '17 at 22:17

0 Answers0