码迷,mamicode.com
首页 > Windows程序 > 详细

C# DevExpress GridControl导出表格【转】

时间:2018-06-01 20:04:02      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:oid   ldo   class   csdn   rtt   exce   提示   string   article   

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

C# DevExpress GridControl导出表格【转】

标签:oid   ldo   class   csdn   rtt   exce   提示   string   article   

原文地址:https://www.cnblogs.com/mazhenyu/p/9123187.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!