DevExpress通用导出Excel,支持多个控件同时导出在同一个Sheet表

21 篇文章 0 订阅
9 篇文章 0 订阅

文章出自深圳-涛神网友提供方案,在此仅做记录,谢谢。

 

 

public static class ViewExport
    { /// <summary>
        /// DevExpress通用导出Excel,支持多个控件同时导出在同一个Sheet表
        /// eg:ExportToXlsx("",gridControl1,gridControl2);
        /// 将gridControl1和gridControl2的数据一同导出到同一张工作表
        /// </summary>
        /// <param name="title">文件名</param>
        /// <param name="panels">控件集</param>
        public static void ExportToExcel ( string title ,params IPrintable [ ] panels )
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog ( );
            saveFileDialog . FileName = title;
            saveFileDialog . Title = "导出Excel";
            saveFileDialog . Filter = "Excel文件(*.xlsx)|*.xlsx|Excel文件(*.xls)|*.xls";
            DialogResult dialogResult = saveFileDialog . ShowDialog ( );
            if ( dialogResult == DialogResult . Cancel )
                return;
            string FileName = saveFileDialog . FileName;
            PrintingSystem ps = new PrintingSystem ( );
            CompositeLink link = new CompositeLink ( ps );
            ps . Links . Add ( link );
            foreach ( IPrintable panel in panels )
            {
                link . Links . Add ( CreatePrintableLink ( panel ) );
            }
            link . Landscape = true;//横向           
            //判断是否有标题,有则设置         
            //link.CreateDocument(); //建立文档
            try
            {
                int count = 1;
                //在重复名称后加(序号)
                while ( File . Exists ( FileName ) )
                {
                    if ( FileName . Contains ( ")." ) )
                    {
                        int start = FileName . LastIndexOf ( "(" );
                        int end = FileName . LastIndexOf ( ")." ) - FileName . LastIndexOf ( "(" ) + 2;
                        FileName = FileName . Replace ( FileName . Substring ( start ,end ) ,string . Format ( "({0})." ,count ) );
                    }
                    else
                    {
                        FileName = FileName . Replace ( "." ,string . Format ( "({0})." ,count ) );
                    }
                    count++;
                }

                if ( FileName . LastIndexOf ( ".xlsx" ) >= FileName . Length - 5 )
                {
                    XlsxExportOptions options = new XlsxExportOptions ( );
                    link . ExportToXlsx ( FileName ,options );
                }
                else
                {
                    XlsExportOptions options = new XlsExportOptions ( );
                    link . ExportToXls ( FileName ,options );
                }
                if ( DevExpress . XtraEditors . XtraMessageBox . Show ( "保存成功,是否打开文件?" ,"提示" ,MessageBoxButtons . YesNo ,MessageBoxIcon . Information ) == DialogResult . Yes )
                    System . Diagnostics . Process . Start ( FileName );//打开指定路径下的文件
            }
            catch ( Exception ex )
            {
                DevExpress . XtraEditors . XtraMessageBox . Show ( ex . Message );
            }
        }
        /// <summary>
        /// 创建打印Componet
        /// </summary>
        /// <param name="printable"></param>
        /// <returns></returns>
        static PrintableComponentLink CreatePrintableLink ( IPrintable printable )
        {
            ChartControl chart = printable as ChartControl;
            if ( chart != null )
                chart . OptionsPrint . SizeMode = DevExpress . XtraCharts . Printing . PrintSizeMode . Stretch;
            PrintableComponentLink printableLink = new PrintableComponentLink ( ) { Component = printable };
            return printableLink;
        }
    }

调用方法:

 

 

ViewExport . ExportToExcel ( "GridControl内容导出" ,gridControl1 );

 

 

 

 

 

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值