1. public class ExportUtility
    2. {
    3. public static void ExportGridToFile(DevExpress.XtraGrid.Views.Base.BaseView gridView, string reportName, string fullFileName = null)
    4. {
    5. string fileName = string.Empty;
    6. if (string.IsNullOrEmpty(fullFileName))
    7. {
    8. System.Windows.Forms.SaveFileDialog dlg = new System.Windows.Forms.SaveFileDialog();
    9. dlg.DefaultExt = ".xls";
    10. dlg.FileName = reportName;
    11. dlg.AddExtension = true;
    12. dlg.Filter = "Excel2000-2003(*.xls)|*.xls|Excel2007以上(*.xlsx)|*.xlsx|PDF文件(*.pdf)|*.pdf|网页文件(*.html)|*.html|RTF文件(*.rtf)|*.rtf|文本文件(*.txt)|*.txt";
    13. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    14. {
    15. fileName = dlg.FileName;
    16. }
    17. else
    18. {
    19. return;
    20. }
    21. }
    22. if (fileName == string.Empty)
    23. {
    24. return;
    25. }
    26. string extFileName = System.IO.Path.GetExtension(fileName).ToUpper();
    27. switch (extFileName)
    28. {
    29. case ".XLSX":
    30. gridView.ExportToXlsx(fileName);
    31. break;
    32. case ".PDF":
    33. gridView.ExportToPdf(fileName);
    34. break;
    35. case ".HTML":
    36. gridView.ExportToHtml(fileName);
    37. break;
    38. case ".RTF":
    39. gridView.ExportToRtf(fileName);
    40. break;
    41. case ".TXT":
    42. gridView.ExportToText(fileName);
    43. break;
    44. default:
    45. gridView.ExportToXls(fileName);
    46. break;
    47. }
    48. if (DevExpress.XtraEditors.XtraMessageBox.Show("保存成功,是否打开文件?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
    49. System.Diagnostics.Process.Start(fileName);//打开指定路径下的文件
    50. }
    51. }