zoukankan      html  css  js  c++  java
  • 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);
                }
            }
    

    实现效果如下

  • 相关阅读:
    Git官方推荐用书
    二叉树-补习
    POJ 2251 Dungeon Master(三维BFS)
    Codeforces 675C Money Transfers (思维题)
    HDU 1195 Open the Lock(BFS)
    HDU 1010 Tempter of the Bone(DFS+剪枝)
    POJ 1426 Find The Multiple(DFS,BFS)
    POJ 3216 Prime Path (BFS)
    POJ 3278 Catch that cow(BFS)
    UVa 572 Oil Deposits(简单DFS)
  • 原文地址:https://www.cnblogs.com/Chen-Ru/p/14137625.html
Copyright © 2011-2022 走看看