Exporting Several XtraGrid Controls to a Single Excel File

前端 未结 3 1026
隐瞒了意图╮
隐瞒了意图╮ 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: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.

提交回复
热议问题