欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C# Dev控件 GridControl导出Excel

程序员文章站 2022-06-08 17:40:58
...
 //导出错误信息
        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            export(MisTakeInfor);
        }
     public void export(GridControl gridControl1)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.FileName = "";
            saveFileDialog1.Filter = "Excel文件(.xls)|*.xls";
            string strtype = "Excel文件名";
            
            saveFileDialog1.FileName = strtype + DateTime.Now.ToString("yyyyMMddhhmmss");
            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();
                    gridControl1.ExportToXls(fs);//将GridControl的内容导出到指定的文件路径
                    fs.Close();
                    DevExpress.XtraEditors.XtraMessageBox.Show("数据导出成功!", "提示");
                }
                catch (Exception ex)
                {
                    if (ex.Message.Contains("正由另一进程使用"))
                    {
                        DevExpress.XtraEditors.XtraMessageBox.Show("数据导出失败!文件正由另一个程序占用!", "提示");
                    }
                    else
                        DevExpress.XtraEditors.XtraMessageBox.Show("数据导出失败!数据量过大,请分别统计再导出!", "提示");
                }
            }
 
        }

 

相关标签: C#