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 @   代号-9527  阅读(293)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示