• 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);
                }
            }
    

    实现效果如下

  • 相关阅读:
    Java 异常基础详解
    Try-with-resources
    Java集合详解
    Java面向对象之多态
    Java面向对象之继承
    Java面向对象之封装
    Java 接口
    Java抽象类
    Java类和对象
    Java 数组结构
  • 原文地址:https://www.cnblogs.com/Chen-Ru/p/14137625.html
Copyright © 2020-2023  润新知