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);
}