Exemplo n.º 1
0
        /// <summary>
        /// 把XtraGrid中的数据倒出到Excel 中 并打开Excel 进行显示
        /// </summary>
        /// <param name="pDg"></param>
        public void ExportToExcelAndShow(DevExpress.XtraPivotGrid.PivotGridControl xtraGrid)
        {
            //string temPath = MB.Util.General.GeApplicationDirectory() + @"Temp\";
            //bool b = System.IO.Directory.Exists(temPath);
            //if (!b) {
            //    System.IO.Directory.CreateDirectory(temPath);
            //}
            //string fullPath = temPath + System.Guid.NewGuid().ToString() + ".xls";
            //b = System.IO.File.Exists(fullPath);
            var fileDialog = new System.Windows.Forms.SaveFileDialog();

            fileDialog.Filter = "Excel 文件|.xls";
            fileDialog.ShowDialog();
            var fileFullName = fileDialog.FileName;

            if (string.IsNullOrEmpty(fileFullName))
            {
                return;
            }
            try {
                xtraGrid.ExportToXls(fileFullName);
                var b = System.IO.File.Exists(fileFullName);
                //判断该文件是否导出成功,如果是那么直接打开该文件
                if (b)
                {
                    //System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();
                    //Info.FileName = fullPath;
                    ////声明一个程序类
                    //System.Diagnostics.Process Proc;
                    ////启动外部程序
                    //Proc = System.Diagnostics.Process.Start(Info);
                    var dre = MB.WinBase.MessageBoxEx.Question("文件导出成功,是否需要打开文件所在的目录");
                    if (dre == DialogResult.Yes)
                    {
                        System.Diagnostics.ProcessStartInfo explore = new System.Diagnostics.ProcessStartInfo();
                        explore.FileName  = "explorer.exe";
                        explore.Arguments = System.IO.Path.GetDirectoryName(fileFullName);
                        System.Diagnostics.Process.Start(explore);
                    }
                }
            }
            catch (Exception e) {
                MB.Util.TraceEx.Write("导出Excel 文件出错!" + e.Message, MB.Util.APPMessageType.SysErrInfo);
                MB.WinBase.MessageBoxEx.Show("导出Excel 文件出错!");
            }
        }
        public static void Export2Xls(DevExpress.XtraPivotGrid.PivotGridControl grd_ctl)
        {
            SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog();

            saveFileDialog.Filter = "Excel (*.xls)|*.xls";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                if (!saveFileDialog.FileName.Equals(String.Empty))
                {
                    try
                    {
                        string file_name = saveFileDialog.FileName;
                        grd_ctl.ExportToXls(file_name);
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                }
            }
        }