/// <summary>
        /// Exporta la tabla seleccionada a un formato selccionado.
        /// </summary>
        /// <param name="exportTo"></param>
        /// <param name="view"></param>
        /// <param name="name"></param>
        public void exportTo(string exportTo, DevExpress.Xpf.Grid.TableView view, string name)
        {
            System.Windows.Forms.FolderBrowserDialog carpeta = new System.Windows.Forms.FolderBrowserDialog();
            carpeta.Description = "Seleccione la carpeta destino";
            carpeta.ShowDialog();
            DateTime thisDay = DateTime.Today;
            string   fecha   = thisDay.ToString("D");
            string   rout    = carpeta.SelectedPath;
            string   nombre  = name;

            if (exportTo == ".xsls")
            {
                if (!String.IsNullOrEmpty(rout))
                {
                    view.ExportToXlsx(rout + @"\Shark_" + nombre + "_" + fecha + ".xlsx");
                    System.Windows.MessageBoxResult dialogResult = System.Windows.MessageBox.Show("El Reporte se creó satisfactoriamente en la ubicación especificada, ¿Desea Abrir el Archivo? '", "Creación De Reporte", System.Windows.MessageBoxButton.YesNo);
                    if (dialogResult == System.Windows.MessageBoxResult.Yes)
                    {
                        System.Diagnostics.Process.Start(rout + @"\Shark_" + nombre + "_" + fecha + ".xlsx");
                    }
                }
            }
            else if (exportTo == ".png")
            {
                if (!String.IsNullOrEmpty(rout))
                {
                    view.ExportToImage(rout + @"\Shark_" + nombre + "_" + fecha + ".png");
                    System.Windows.MessageBoxResult dialogResult = System.Windows.MessageBox.Show("El Reporte se creó satisfactoriamente en la ubicación especificada, ¿Desea Abrir el Archivo? '", "Creación De Reporte", System.Windows.MessageBoxButton.YesNo);
                    if (dialogResult == System.Windows.MessageBoxResult.Yes)
                    {
                        System.Diagnostics.Process.Start(rout + @"\Shark_" + nombre + "_" + fecha + ".png");
                    }
                }
            }
            else if (exportTo == ".pdf")
            {
                if (!String.IsNullOrEmpty(rout))
                {
                    view.ExportToPdf(rout + @"\Shark_" + nombre + "_" + fecha + ".pdf");
                    System.Windows.MessageBoxResult dialogResult = System.Windows.MessageBox.Show("El Reporte se creó satisfactoriamente en la ubicación especificada, ¿Desea Abrir el Archivo? '", "Creación De Reporte", System.Windows.MessageBoxButton.YesNo);
                    if (dialogResult == System.Windows.MessageBoxResult.Yes)
                    {
                        System.Diagnostics.Process.Start(rout + @"\Shark_" + nombre + "_" + fecha + ".pdf");
                    }
                }
            }
        }
Example #2
0
 /// <summary>
 /// table生成excel xlsx格式
 /// </summary>
 /// <param name="TableView"></param>
 /// <param name="filOutputType">生成的文件名</param>
 /// <param name="FilePath">路径</param>
 /// <param name="IsStart">是否打开文件</param>
 /// <returns></returns>
 public static bool CreateTableViewToXlsx(DevExpress.Xpf.Grid.TableView TableView, FileOutputType filOutputType, string FilePath = null, bool IsStart = true)
 {
     try
     {
         string FileName = GetFileName(filOutputType);
         if (FilePath == null)
         {
             FilePath = System.Environment.CurrentDirectory + "\\输出文件夹";
         }
         if (!Directory.Exists(FilePath))
         {
             Directory.CreateDirectory(FilePath);
         }
         SaveFileDialog dialog = new SaveFileDialog();
         dialog.Filter           = "Excel文件 (*.xlsx)|*.xlsx";
         dialog.InitialDirectory = FilePath;
         dialog.FileName         = FileName;
         dialog.FilterIndex      = 1;
         string saveFileName;
         if (dialog.ShowDialog() == DialogResult.OK)
         {
             saveFileName = dialog.FileName;
             FileName     = Path.GetFileNameWithoutExtension(saveFileName);
         }
         else
         {
             return(false);
         }
         if (File.Exists(saveFileName))
         {
             FileStream fs    = null;
             bool       inUse = true;
             try
             {
                 fs    = new FileStream(saveFileName, FileMode.Open, FileAccess.Read, FileShare.None);
                 inUse = false;
             }
             catch { }
             finally
             {
                 if (fs != null)
                 {
                     fs.Close();
                 }
             }
             if (inUse)
             {
                 DevExpress.Xpf.Core.DXMessageBox.Show(filOutputType + "正在被使用,请关闭后重新操作。");
                 return(false);
             }
         }
         DevExpress.XtraPrinting.XlsxExportOptionsEx option = new DevExpress.XtraPrinting.XlsxExportOptionsEx()
         {
             ExportType     = DevExpress.Export.ExportType.WYSIWYG,
             TextExportMode = DevExpress.XtraPrinting.TextExportMode.Text,
             ShowGridLines  = true,
         };
         TableView.ExportToXlsx(saveFileName, option);
         LOGGER.Info($"生成文件{filOutputType}成功");
         if (IsStart)
         {
             //保存多个会打开多个文件夹,应让用户自主选择保存位置
             //System.Diagnostics.Process.Start("Explorer.exe", filePath);
             System.Diagnostics.Process.Start(saveFileName);
         }
         return(true);
     }
     catch (Exception)
     {
         LOGGER.Error($"生成{filOutputType}文件失败");
         //throw new Exception($"生成{FileName}文件失败");
         return(false);
     }
 }