导出.txt / .pdf / .xls

public static string TxtFilter = "文本文件(.txt)|*.txt";
        public static string PDFFilter = "PDF文件(.pdf)|*pdf";
        public static string XLSFilter = "Excel文件(.xls)|*.xls";
        public void ExportGrid(GridControl grid, string ext, string Filter,string title)
        {
            using (SaveFileDialog saveFileDialog1 = new SaveFileDialog())
            {
                saveFileDialog1.FileName = "";
                saveFileDialog1.Filter = Filter;
                saveFileDialog1.FileName = title;
                saveFileDialog1.OverwritePrompt = false; //已存在文件是否覆盖提示
                if (saveFileDialog1.ShowDialog() != DialogResult.OK)
                    return;
                //已存在文件是否覆盖提示
                while (System.IO.File.Exists(saveFileDialog1.FileName) &&
                    DevExpress.XtraEditors.XtraMessageBox.Show("该文件名已存在,是否覆盖?",
                    "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                {
                    if (saveFileDialog1.ShowDialog() != DialogResult.OK)
                        return;
                }
                if (saveFileDialog1.FileName != "")
                {
                    try
                    {
                        System.IO.FileStream fs =
                           (System.IO.FileStream)saveFileDialog1.OpenFile();
                        if (ext == "txt")
                        {
                            grid.ExportToText(fs);
                        }
                        else if (ext == "pdf")
                        {
                            grid.ExportToPdf(fs);
                        }
                        else if (ext == "xls")
                        {
                            XlsExportOptions options = new XlsExportOptions();
                            options.TextExportMode = TextExportMode.Text;
                            grid.ExportToXls(fs, options);
                        }
                        //this.gcISSUESUBs.ExportToText(fs);
                        fs.Close();
                        //DevExpress.XtraEditors.XtraMessageBox.Show("数据导出成功!", "提示");
                    }
                    catch (Exception ex)
                    {
                        if (ex.Message.Contains("正由另一进程使用"))
                        {
                            DevExpress.XtraEditors.XtraMessageBox.Show("数据导出失败!文件正由另一个程序占用!", "提示");
                        }
                        else
                            DevExpress.XtraEditors.XtraMessageBox.Show("数据导出失败!数据量过大,请分别统计再导出!", "提示");
                    }
                }
            }
        }

猜你喜欢

转载自www.cnblogs.com/mapstar/p/10818090.html