devexpress 多控件导出到excel

  /// <summary>
        /// 导出多个控件数据到excel中
        /// </summary>
        /// <param name="FileName">文件名</param>
        /// <param name="tables">控件列表</param>
        /// <returns></returns>
        public static string ControlExportToXlsxNoOpen(string FileName, params IPrintable[] tables)
        {
            string path = "";
            try
            {

                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "Excel文件|*.xlsx;";
                sfd.FileName = FileName;
                sfd.Title = "导出" + FileName;
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    DevExpress.XtraPrinting.XlsxExportOptions options = new DevExpress.XtraPrinting.XlsxExportOptions();
                    options.SheetName = FileName;
                    options.ExportMode = XlsxExportMode.SingleFilePageByPage;
                    path = sfd.FileName;

                    var defautps = new DevExpress.XtraPrinting.PrintingSystem();
                    PrintableComponentLink pclink = null;
                    CompositeLink compLink = new CompositeLink(defautps);
                    foreach (var tbl in tables)
                    {
                        pclink = new PrintableComponentLink();
                        pclink.Component = tbl;
                        compLink.Links.Add(pclink);
                    }

                    #region 分Sheet
                    // 用于每个Link生成一个Sheet,不使用此方法,则合并在一个Sheet内
                    compLink.CreatePageForEachLink();
                    compLink.ExportToXlsx(path, options);
                    #endregion 分Sheet
                }
            }
            catch (Exception ex)
            {
                LogUtils.Error(ex.Message);
                Error(ex.Message);
            }
            return path;
        }
原文地址:https://www.cnblogs.com/coder-soldier/p/11987730.html