ExportHelper.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using Microsoft.Reporting.WinForms;
  2. using System;
  3. using System.IO;
  4. using System.Linq;
  5. namespace Muchinfo.PC.Common.Helpers
  6. {
  7. public class ExportHelper
  8. {
  9. /// <summary>
  10. /// 导出
  11. /// </summary>
  12. /// <param name="reportViewer">The report viewer.</param>
  13. /// <param name="fileName">Name of the file.</param>
  14. public static void ExportReport(ReportViewer reportViewer, string fileName)
  15. {
  16. if (reportViewer.LocalReport.DataSources.Count != 0)
  17. {
  18. System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  19. {
  20. var file = new System.Windows.Forms.SaveFileDialog();
  21. file.FileName = fileName;
  22. file.Filter = "PDF|*.pdf|Word|*.doc|Excel|*.xls"; //保存类型
  23. if (file.ShowDialog().Equals(System.Windows.Forms.DialogResult.OK))
  24. {
  25. if (string.IsNullOrWhiteSpace(file.FileName)) return;
  26. string ext = Path.GetExtension(file.FileName).Replace(".", ""); //扩展名
  27. //页面信息
  28. string devInfo = @"<DeviceInfo>
  29. <OutputFormat>" + ext + @"</OutputFormat>
  30. <PageWidth>21cm</PageWidth>
  31. <PageHeight>29.7cm</PageHeight>
  32. <MarginTop>0in</MarginTop>
  33. <MarginLeft>0in</MarginLeft>
  34. <MarginRight>0in</MarginRight>
  35. <MarginBottom>0in</MarginBottom>
  36. </DeviceInfo>";
  37. devInfo = "";
  38. RenderingExtension render = null;
  39. if (ext.Equals("pdf"))
  40. render = reportViewer.LocalReport.ListRenderingExtensions().FirstOrDefault((r) => r.Name.Equals("PDF"));
  41. else if (ext.Equals("xls"))
  42. render = reportViewer.LocalReport.ListRenderingExtensions().FirstOrDefault((r) => r.Name.Equals("Excel"));
  43. //else if (ext.Equals("xlsx"))
  44. // render = this._reportViewer.LocalReport.ListRenderingExtensions().FirstOrDefault((r) => r.Name.Equals("EXCELOPENXML"));
  45. else if (ext.Equals("doc"))
  46. render = reportViewer.LocalReport.ListRenderingExtensions().FirstOrDefault((r) => r.Name.Equals("WORD"));
  47. //else if (ext.Equals("docx"))
  48. // render = this._reportViewer.LocalReport.ListRenderingExtensions().FirstOrDefault((r) => r.Name.Equals("WORDOPENXML"));
  49. if (render != null)
  50. reportViewer.ExportDialog(render, devInfo, file.FileName);
  51. else
  52. System.Windows.MessageBox.Show("导出失败!");
  53. }
  54. }));
  55. }
  56. }
  57. /// <summary>
  58. /// 打印
  59. /// </summary>
  60. /// <param name="reportViewer"></param>
  61. public static void Printeport(ReportViewer reportViewer)
  62. {
  63. if (reportViewer.LocalReport.DataSources.Count != 0)
  64. {
  65. reportViewer.PrintDialog();
  66. }
  67. }
  68. }
  69. }