dev gridcontrol导出几种格式

1、初始化导出格式下来框,目前提供的有html,pdf,excel txt 等各种方式。

初始化函数如下:

 void InitExportData() {
            for(int i = 0; i < exportData.GetLength(0); i++)  
                cbExport.Properties.Items.Add(exportData.GetValue(i, 0));
            cbExport.SelectedIndex = 0;
        }

cbexport 为对应combox下拉框控件。

加黑的exportData为几种格式。

2.初始化下拉框内容:

     string[,] exportData = new string[,] {{"HTML Document", "HTML Documents|*.html", "htm"}, 
            {"Microsoft Excel 2007 Document", "Microsoft Excel|*.xlsx", "xlsx"}, 
            {"Microsoft Excel Document", "Microsoft Excel|*.xls", "xls"}, 
            {"RTF Document", "RTF Files|*.rtf", "rtf"},
            {"PDF Document", "PDF Files|*.pdf", "pdf"},
            {"MHT Document", "MHT Files|*.mht", "mht"},
            {"Text Document", "Text Files|*.txt", "txt"}};

3、导出按钮如下:

int index = cbExport.SelectedIndex;
            if(index < 0) return;
            string fileName = ShowSaveFileDialog(exportData.GetValue(index, 0).ToString(), exportData.GetValue(index, 1).ToString());
            if(fileName == string.Empty) return;
            ExportToEx(fileName, exportData.GetValue(index, 2).ToString(), gridView1);
            OpenFile(fileName);

4、其中调用的函数如下:

      private string ShowSaveFileDialog(string title, string filter) {
            SaveFileDialog dlg = new SaveFileDialog();
            string name = Application.ProductName;
            int n = name.LastIndexOf(".") + 1;
            if(n > 0) name = name.Substring(n, name.Length - n);
            dlg.Title = "Export To " + title;
            dlg.FileName = name;
            dlg.Filter = filter;
            if(dlg.ShowDialog() == DialogResult.OK) return dlg.FileName;
            return "";
        }

       private void ExportToEx(String filename, string ext, BaseView exportView) {
            Cursor currentCursor = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;
            if(ext == "rtf") exportView.ExportToRtf(filename);
            if(ext == "pdf") exportView.ExportToPdf(filename);
            if(ext == "mht") exportView.ExportToMht(filename);
            if(ext == "htm") exportView.ExportToHtml(filename);
            if(ext == "txt") exportView.ExportToText(filename);
            if(ext == "xls") exportView.ExportToXls(filename);
            if(ext == "xlsx") exportView.ExportToXlsx(filename);
            Cursor.Current = currentCursor;
        }

5、导出文件后,提示是否打开当前导出的文件函数

        private void OpenFile(string fileName) {
            if(XtraMessageBox.Show("是否打开?", "信息提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) {
                try {
                    System.Diagnostics.Process process = new System.Diagnostics.Process();
                    process.StartInfo.FileName = fileName;
                    process.StartInfo.Verb = "Open";
                    process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
                    process.Start();
                }
                catch {
                   
                }
            }
            
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值