Exporting Several XtraGrid Controls to a Single Excel File

前端 未结 3 1016
隐瞒了意图╮
隐瞒了意图╮ 2020-12-20 06:02

I\'ve got several XtraGrid Controls each one containing different information, I get some information about the way in which you can export a XtraGrid to an Excel file in th

相关标签:
3条回答
  • 2020-12-20 06:27

    To do that you will want to add a printableComponentLink to each gridControl, and then Create a compositeLink that you can add each of the printableComponent links to.

    This link may prove DevExpress KB Article may prove useful as it has an example of that.

    Then you will use the compositeLink.ExportToXlsx method. If you create XlsxExportOptions with the XlsxExportOptions.ExportMode property equal to SingleFilePageByPage and pass it to the CompositeLink.ExportToXlsx method, every page will be exported to a separate sheet.

    0 讨论(0)
  • 2020-12-20 06:32

    In above code, compositeLink.ExportToXlsx failed for me--no such method. Of course I am using V10.2.5, which is old. I suggest this link from the DEVXPRESS site that uses the ShowPreviewDialog method which allows exporting in a number of different formats. The link also shows how to do some customization of the output. https://documentation.devexpress.com/#WindowsForms/clsDevExpressXtraPrintingLinksCompositeLinktopic

    0 讨论(0)
  • 2020-12-20 06:41

    I just wanted to provide a more complete answer, since it took a while for me to get a solution together using D..'s answer.

    And yes - it looks like I'm trying to print something, but I'm just exporting to Excel, I promise.

    using DevExpress.XtraPrinting;
    using DevExpress.XtraPrintingLinks;
    using DevExpress.XtraGrid;
    
    class whatever
    {
        GridControl grid1;
        GridControl grid2;
        //.....
    
        public void exportToExcel()
        {
            using (var saveDialog = new SaveFileDialog())
            {
                saveDialog.Filter = "Excel (.xlsx)|*.xlsx";
                if (saveDialog.ShowDialog() == DialogResult.OK)
                {
                    var printingSystem = new PrintingSystemBase();
                    var compositeLink = new CompositeLinkBase();
                    compositeLink.PrintingSystemBase = printingSystem;
    
                    var link1 = new PrintableComponentLinkBase();
                    link1.Component = grid1;
                    var link2 = new PrintableComponentLinkBase();
                    link2.Component = grid2;
    
                    compositeLink.Links.Add(link1);
                    compositeLink.Links.Add(link2);
    
                    var options = new XlsxExportOptions();
                    options.ExportMode = XlsxExportMode.SingleFilePageByPage;
    
                    compositeLink.CreatePageForEachLink();
                    compositeLink.ExportToXlsx(saveDialog.FileName, options);
                }
            }
        }
    }    
    

    Hope it saves somebody a little time.

    0 讨论(0)
提交回复
热议问题