using Microsoft.Reporting.WinForms;
using System;
using System.IO;
using System.Linq;
namespace Muchinfo.PC.Common.Helpers
{
public class ExportHelper
{
///
/// 导出
///
/// The report viewer.
/// Name of the file.
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 = @"
" + ext + @"
21cm
29.7cm
0in
0in
0in
0in
";
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("导出失败!");
}
}));
}
}
///
/// 打印
///
///
public static void Printeport(ReportViewer reportViewer)
{
if (reportViewer.LocalReport.DataSources.Count != 0)
{
reportViewer.PrintDialog();
}
}
}
}