1,新建一个webForm页面,在该页面拖入ScriptManager和ReportViewer
2,在网站下面添加一个文件夹,例子(Reports文件夹)
3,在Reports文件夹中,选择新建项,添加一个“数据集”。。。后缀名是xsd的(例子。order.xsd)
4,在order.xsd拖放2个datatable控件,也就是相当于数据库的表。只要把想要的数据字段写进去,前提字段名字要和数据库表的列名一样
datatable的名字建议和数据库表的名字一致(例子:Order OrderDetail),最后还需要修改添加字段的数据类型,在属性处选择
5,在Reports文件夹中继续添加新建项,选择报表(Report1.rdlc)
7,在Report1.rdlc的工具箱中拖放控件,一般使用表和文本框
8,配置好数据集之后再webform窗体load里写代码
using Microsoft.Reporting.WebForms;
if (!IsPostBack) {
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/.rdlc的路径");
ReportDataSource ds = new ReportDataSource();
ds.Name = "";//dataset的名字
ds.Value = "";//可以是一个集合,也可以是对象
ReportViewer1.LocalReport.DataSources.Add(ds);
ReportViewer1.LocalReport.Refresh();
}
9,第八个是webForm的方式,那么要用MVC做报表的话,第八种方式就要改成这样
在控制器里面写一个action
public ActionResult Report(int id) {
LocalReport localReport = new LocalReport();//new 一个报表对象
localReport.ReportPath = Server.MapPath("~/Content/Reports/OrderReport.rdlc");//永远是报表
Order order = orderBiz.FetchByKey(id);
ReportDataSource rds1 = new ReportDataSource("Order", new List<Order> { order });
localReport.DataSources.Add(rds1);
ReportDataSource rds2 = new ReportDataSource("OrderDetail", order.Details);
localReport.DataSources.Add(rds2);
//下面是系统默认设置 直接复制就好
string reportType = "PDF";
string mimeType;
string encoding;
string fileNameExtension;
//The DeviceInfo settings should be changed based on the reportType
//http://msdn2.microsoft.com/en-us/library/ms155397.aspx
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>PDF</OutputFormat>" +
" <PageWidth>8.5in</PageWidth>" +
" <PageHeight>11in</PageHeight>" +
" <MarginTop>0.5in</MarginTop>" +
" <MarginLeft>1in</MarginLeft>" +
" <MarginRight>1in</MarginRight>" +
" <MarginBottom>0.5in</MarginBottom>" +
"</DeviceInfo>";
Warning[] warnings;
string[] streams;
byte[] renderedBytes;
//Render the report
renderedBytes = localReport.Render(
reportType,
deviceInfo,
out mimeType,
out encoding,
out fileNameExtension,
out streams,
out warnings);
//Response.AddHeader("content-disposition", "attachment; filename=NorthWindCustomers." + fileNameExtension);
return File(renderedBytes, mimeType);
}
2,在网站下面添加一个文件夹,例子(Reports文件夹)
3,在Reports文件夹中,选择新建项,添加一个“数据集”。。。后缀名是xsd的(例子。order.xsd)
4,在order.xsd拖放2个datatable控件,也就是相当于数据库的表。只要把想要的数据字段写进去,前提字段名字要和数据库表的列名一样
datatable的名字建议和数据库表的名字一致(例子:Order OrderDetail),最后还需要修改添加字段的数据类型,在属性处选择
5,在Reports文件夹中继续添加新建项,选择报表(Report1.rdlc)
7,在Report1.rdlc的工具箱中拖放控件,一般使用表和文本框
8,配置好数据集之后再webform窗体load里写代码
using Microsoft.Reporting.WebForms;
if (!IsPostBack) {
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/.rdlc的路径");
ReportDataSource ds = new ReportDataSource();
ds.Name = "";//dataset的名字
ds.Value = "";//可以是一个集合,也可以是对象
ReportViewer1.LocalReport.DataSources.Add(ds);
ReportViewer1.LocalReport.Refresh();
}
9,第八个是webForm的方式,那么要用MVC做报表的话,第八种方式就要改成这样
在控制器里面写一个action
public ActionResult Report(int id) {
LocalReport localReport = new LocalReport();//new 一个报表对象
localReport.ReportPath = Server.MapPath("~/Content/Reports/OrderReport.rdlc");//永远是报表
Order order = orderBiz.FetchByKey(id);
ReportDataSource rds1 = new ReportDataSource("Order", new List<Order> { order });
localReport.DataSources.Add(rds1);
ReportDataSource rds2 = new ReportDataSource("OrderDetail", order.Details);
localReport.DataSources.Add(rds2);
//下面是系统默认设置 直接复制就好
string reportType = "PDF";
string mimeType;
string encoding;
string fileNameExtension;
//The DeviceInfo settings should be changed based on the reportType
//http://msdn2.microsoft.com/en-us/library/ms155397.aspx
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>PDF</OutputFormat>" +
" <PageWidth>8.5in</PageWidth>" +
" <PageHeight>11in</PageHeight>" +
" <MarginTop>0.5in</MarginTop>" +
" <MarginLeft>1in</MarginLeft>" +
" <MarginRight>1in</MarginRight>" +
" <MarginBottom>0.5in</MarginBottom>" +
"</DeviceInfo>";
Warning[] warnings;
string[] streams;
byte[] renderedBytes;
//Render the report
renderedBytes = localReport.Render(
reportType,
deviceInfo,
out mimeType,
out encoding,
out fileNameExtension,
out streams,
out warnings);
//Response.AddHeader("content-disposition", "attachment; filename=NorthWindCustomers." + fileNameExtension);
return File(renderedBytes, mimeType);
}