随笔 - 833  文章 - 1  评论 - 106  阅读 - 200万

C# DevExpress GridControl导出表格【转】

DevExpress的GridControl提供方便的数据导出功能,可以方便的导出Exce,PDF,Html页面,world形式,无需写额外的代码,方便、快捷。

/// <summary>
        ///     导出表格
        /// </summary>
        /// <param name="gridControl1"></param>
        public static void DevExpressGridControlExport(GridControl gridControl1)
        {
            using (var saveDialog = new SaveFileDialog())
            {
                saveDialog.Filter = Resources.CommonFunction_DevExpressGridControlExport_Excel_97_2007文件___xls_Excel_2010___xlsx_Excel__2003___xls____xls_Excel__2010____xlsx____xlsx__RichText_File___rtf____rtf__Pdf_File___pdf____pdf__Html_File___html____html;
                if (saveDialog.ShowDialog() == DialogResult.Cancel) return;
                var exportFilePath = saveDialog.FileName;
                var fileExtenstion = new FileInfo(exportFilePath).Extension;
                switch (fileExtenstion)
                {
                    case ".xls":
                        gridControl1.ExportToXls(exportFilePath);
                        break;
                    case ".xlsx":
                        gridControl1.ExportToXlsx(exportFilePath);
                        break;
                    case ".rtf":
                        gridControl1.ExportToRtf(exportFilePath);
                        break;
                    case ".pdf":
                        gridControl1.ExportToPdf(exportFilePath);
                        break;
                    case ".html":
                        gridControl1.ExportToHtml(exportFilePath);
                        break;
                    case ".mht":
                        gridControl1.ExportToMht(exportFilePath);
                        break;
                }

                if (File.Exists(exportFilePath))
                {
                    try
                    {
                        if (DialogResult.Yes == MessageBox.Show(Resources.CommonFunction_DevExpressGridControlExport_, Resources.CommonFunction_DevExpressGridControlExport_提示, MessageBoxButtons.YesNo))
                        {
                            Process.Start(exportFilePath);
                        }
                    }
                    catch
                    {
                        var msg = "The file could not be opened." + Environment.NewLine + Environment.NewLine + "Path: " + exportFilePath;
                        MessageBox.Show(msg, Resources.CommonFunction_DevExpressGridControlExport_Error_, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    var msg = "The file could not be saved." + Environment.NewLine + Environment.NewLine + "Path: " + exportFilePath;
                    MessageBox.Show(msg, Resources.CommonFunction_DevExpressGridControlExport_Error_, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
原文网址: https://blog.csdn.net/Gary_888/article/details/70212796
posted on   3D入魔  阅读(682)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2016-06-01 使用 ODP.NET 访问 Oracle(.net如何访问Oracle)详解【转】
2016-06-01 Web编程前端之7:web.config详解 【转】
2015-06-01 终于会用c#中的delegate(委托)和event(事件)了 [转]
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示