DevExpress gridview excel

 // 导出的文件名
            string strFileName = "构库记录";
            string strSavePath = "";
            SaveFileDialog dlgSave = new SaveFileDialog();
            dlgSave.Title = "导出到Microsoft Excel Document";// 对话框标题
            dlgSave.FileName = strFileName;
            dlgSave.Filter = "Microsoft Excel|*.xls";// 过滤格式
            if (dlgSave.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                strSavePath = dlgSave.FileName;
            }
            if (strSavePath != "")
            {
                // 保存
                Cursor currentCursor = Cursor.Current;
                Cursor.Current = Cursors.WaitCursor;

                DevExpress.XtraGrid.Export.BaseExportLink link = this.gridView1.CreateExportLink(new DevExpress.XtraExport.ExportXlsProvider(strSavePath));
                (link as DevExpress.XtraGrid.Export.GridViewExportLink).ExpandAll = false;
                link.ExportCellsAsDisplayText = true;
                link.ExportTo(true);

                Cursor.Current = currentCursor;
                //打开
                if (MessageBox.Show("你想打开这个文件吗?""导出到...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    try
                    {
                        System.Diagnostics.Process process = new System.Diagnostics.Process();
                        process.StartInfo.FileName = strSavePath;
                        process.StartInfo.Verb = "Open";
                        process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
                        process.Start();
                    }
                    catch
                    {
                        MessageBox.Show(this"你的计算机中未安装Excel,不能打开该文档!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }

posted on 2012-11-20 18:17  Arvin Lai  阅读(498)  评论(0编辑  收藏  举报