DEV导出多个控件到excel

DEV导出多个控件到excel

代码如下

//title文件名,isPageForEachLink是分成多个工作薄,sheetName工作薄名,cll容器
ExportToExcel("维修率统计图", true, "",groupBox1);

public void ExportToExcel(string title, bool isPageForEachLink, string sheetName, Control cll)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog()
            {
                FileName = title,
                Title = "导出Excel",
                Filter = "Excel文件(*.xlsx)|*.xlsx|Excel文件(*.xls)|*.xls"
            };
            DialogResult dialogResult = saveFileDialog.ShowDialog();
            if (dialogResult == DialogResult.Cancel)
                return;
            string FileName = saveFileDialog.FileName;
            DevExpress.XtraPrintingLinks.CompositeLink link = new DevExpress.XtraPrintingLinks.CompositeLink(new DevExpress.XtraPrinting.PrintingSystem());

            foreach (ChartControl item in cll.Controls)
            {
                var plink = new DevExpress.XtraPrinting.PrintableComponentLink() { Component = item };
                link.Links.Add(plink);
            }

            if (isPageForEachLink)//15.1 的Xls不支持这个功能,15.2未知
                link.CreatePageForEachLink();
            if (string.IsNullOrEmpty(sheetName)) sheetName = "Sheet";//默认工作薄名称
            try
            {
                int count = 1;
                //在重复名称后加(序号)
                while (System.IO.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)
                {
                    DevExpress.XtraPrinting.XlsxExportOptions options = new DevExpress.XtraPrinting.XlsxExportOptions() { SheetName = sheetName };
                    if (isPageForEachLink)
                        options.ExportMode = DevExpress.XtraPrinting.XlsxExportMode.SingleFilePageByPage;
                    link.ExportToXlsx(FileName, options);
                }
                else
                {
                    DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions() { SheetName = sheetName };
                    if (isPageForEachLink) //15.Xls没有这个属性,15.2未知
                        options.ExportMode = DevExpress.XtraPrinting.XlsExportMode.SingleFile;
                    link.ExportToXls(FileName, options);
                }
                //if (DevExpress.XtraEditors.XtraMessageBox.Show("保存成功,是否打开文件?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                //    System.Diagnostics.Process.Start(FileName);//打开指定路径下的文件
                DevExpress.XtraEditors.XtraMessageBox.Show("导出成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message);
            }
        }

实现效果如下

posted @ 2020-12-15 11:39  代号-9527  阅读(311)  评论(0)    收藏  举报