| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- using Microsoft.Reporting.WinForms;
- using System;
- using System.IO;
- using System.Linq;
- namespace Muchinfo.PC.Common.Helpers
- {
- public class ExportHelper
- {
- /// <summary>
- /// 导出
- /// </summary>
- /// <param name="reportViewer">The report viewer.</param>
- /// <param name="fileName">Name of the file.</param>
- public static void ExportReport(ReportViewer reportViewer, string fileName)
- {
- if (reportViewer.LocalReport.DataSources.Count != 0)
- {
- System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- var file = new System.Windows.Forms.SaveFileDialog();
- file.FileName = fileName;
- file.Filter = "PDF|*.pdf|Word|*.doc|Excel|*.xls"; //保存类型
- if (file.ShowDialog().Equals(System.Windows.Forms.DialogResult.OK))
- {
- if (string.IsNullOrWhiteSpace(file.FileName)) return;
- string ext = Path.GetExtension(file.FileName).Replace(".", ""); //扩展名
- //页面信息
- string devInfo = @"<DeviceInfo>
- <OutputFormat>" + ext + @"</OutputFormat>
- <PageWidth>21cm</PageWidth>
- <PageHeight>29.7cm</PageHeight>
- <MarginTop>0in</MarginTop>
- <MarginLeft>0in</MarginLeft>
- <MarginRight>0in</MarginRight>
- <MarginBottom>0in</MarginBottom>
- </DeviceInfo>";
- devInfo = "";
- RenderingExtension render = null;
- if (ext.Equals("pdf"))
- render = reportViewer.LocalReport.ListRenderingExtensions().FirstOrDefault((r) => r.Name.Equals("PDF"));
- else if (ext.Equals("xls"))
- render = reportViewer.LocalReport.ListRenderingExtensions().FirstOrDefault((r) => r.Name.Equals("Excel"));
- //else if (ext.Equals("xlsx"))
- // render = this._reportViewer.LocalReport.ListRenderingExtensions().FirstOrDefault((r) => r.Name.Equals("EXCELOPENXML"));
- else if (ext.Equals("doc"))
- render = reportViewer.LocalReport.ListRenderingExtensions().FirstOrDefault((r) => r.Name.Equals("WORD"));
- //else if (ext.Equals("docx"))
- // render = this._reportViewer.LocalReport.ListRenderingExtensions().FirstOrDefault((r) => r.Name.Equals("WORDOPENXML"));
- if (render != null)
- reportViewer.ExportDialog(render, devInfo, file.FileName);
- else
- System.Windows.MessageBox.Show("导出失败!");
- }
- }));
- }
- }
- /// <summary>
- /// 打印
- /// </summary>
- /// <param name="reportViewer"></param>
- public static void Printeport(ReportViewer reportViewer)
- {
- if (reportViewer.LocalReport.DataSources.Count != 0)
- {
- reportViewer.PrintDialog();
- }
- }
- }
- }
|