1、下载FastReport客户端,在安装路径下找到如下程序集,并在MVC项目中引用;主要用到FastReport.dll和FastReport.Web.dll;
2、在根目录的Web.config配置文件中新增配置,在system.webServer节点下面添加下面代码:
<handlers>
<add name="FastReportHandler" path="FastReport.Export.axd" verb="*" type="FastReport.Web.Handlers.WebExport"/>
</handlers>
3、在Views视图文件夹下的web.config配置文件中引入命名空间,在namespaces节点下面添加下面代码:
<add namespace="FastReport" />
<add namespace="FastReport.Web" />
4、新增一个通用的Print视图页面
@using FastReport
@using FastReport.Web
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
@WebReportGlobals.Scripts()
@WebReportGlobals.Styles()
<meta name="viewport" content="width=device-width" />
<title>PrintTest</title>
</head>
<body>
<div>
@ViewBag.WebReport.GetHtml()
</div>
</body>
</html>
5、在具体的项目页面,即点击按钮调用打印功能的页面中添加相应引用:
@using FastReport
@using FastReport.Web
<!DOCTYPE html>
<html>
<head>
@WebReportGlobals.Scripts()
@WebReportGlobals.Styles()
</html>
</head>
6、在Controller控制器中引入添加的程序集,在Print方法中添加如下代码:
public ActionResult Print(string jid, string jtype, string btntype)
{
DataTable dt = new DataTable();//Datatable数据源
DataSet dataSet = new DataSet();
dataSet.Tables.Add(dt);
dt.TableName = "Table1";
WebReport webReport = new WebReport();
webReport.Report.RegisterData(dataSet);
string fileName = Request.PhysicalApplicationPath + "FastReport\\XXX.frx";
webReport.Report.Load(fileName);
//绑定数据源
DataBand data = webReport.Report.FindObject("Data1") as DataBand;
data.DataSource = webReport.Report.GetDataSource("Table1");
webReport.Height = 500;
webReport.Width = 1040;
ViewBag.WebReport = webReport;
return View();
}