- 1.從工具箱拉出表或者矩陣(本次使用的是矩陣)
- 2.選擇需要的數據集,沒有就新建一個數據集,名稱自己起好,下面有用到
- 3.將行組和行列顯示出來(右擊報表--試圖=>)
- 4.雙擊行組下的RowGroup組=>常規=>組表達式=>分組方式,點擊FX 選擇類別=>字段(DbSetName)=>雙擊右邊的值(選擇你要的分組依據),或者直接點擊頁面矩陣上行右上角的圖標添加分組依據(第3步圖)。
- 5.右擊行組--RowGroup組,添加組=>子組,同第3步一樣 fx 之后的步驟
6.如果在這個組下有其他數據要展示,右擊你添加好的行組所在視圖的文本框=>插入列=>組內部-右側(位置左右自己挑)
7.后台代碼
var list=XXX,在數據層拿到T-SQL數據
直接輸出為PDF下載到本地
excel格式:
var bytes = viewer.LocalReport.Render("Pdf")中把 “PDF”替換”EXCEL“;
Response.ContentEncoding = Encoding.GetEncoding("GB2312"); 把 “GB2312”替換"application/vnd.ms-excel";

1 protected void Page_Load(object sender, EventArgs e) 2 { 3 4 if (IsPostBack) return; 5 try 6 { 7 var list = new reportBL().report(); 8 if (list.Count == 0) 9 { 10 Response.Write("沒有信息!"); 11 return; 12 } 13 DataTable dt = ListToDataTable(list); 14 var viewer = new ReportViewer(); 15 viewer.LocalReport.ReportPath = @"Rpt\Rdlc\SampleForm.rdlc"; 16 viewer.ProcessingMode = ProcessingMode.Local; 17 //這里把建好的數據集的名稱替換掉DataSetName 18 var rds = new ReportDataSource("DataSetName", dt); 19 viewer.LocalReport.EnableExternalImages = true; 20 viewer.LocalReport.DataSources.Clear(); 21 viewer.LocalReport.DataSources.Add(rds); 22 viewer.LocalReport.EnableExternalImages = true; 23 errCode = 8; 24 viewer.LocalReport.Refresh(); 25 var bytes = viewer.LocalReport.Render("Pdf"); 26 errCode = 10; 27 Response.ContentType = "application/pdf"; 28 //設定編碼方式,若輸出的excel有亂碼,可優先從編碼方面解決 29 Response.Charset = "gb2312"; 30 //Response.Charset = "utf-8"; 31 Response.ContentEncoding = Encoding.GetEncoding("GB2312"); 32 //關閉ViewState,此屬性在Page中 33 EnableViewState = false; 34 //filenames是自定義的文件名 35 Response.AppendHeader("Content-Disposition", string.Format("attachment;filename={0}.pdf", DateTime.Now.ToString("yyyyMMddHHmmssffff"))); 36 //content是步驟1的html,注意是string類型 37 Response.BinaryWrite(bytes); 38 Response.End(); 39 40 41 } 42 catch (Exception ex) 43 { 44 45 Response.Write(ex.Message + ":" + errCode); 46 } 47 48 } 49 public static DataTable ListToDataTable<T>(IList<T> list, params string[] propertyName) 50 { 51 var propertyNameList = new List<string>(); 52 if (propertyName != null) 53 propertyNameList.AddRange(propertyName); 54 var result = new DataTable(); 55 if (list.Count > 0) 56 { 57 PropertyInfo[] propertys = list[0].GetType().GetProperties(); 58 foreach (PropertyInfo pi in propertys) 59 { 60 if (propertyNameList.Count == 0) 61 { 62 DataColumn dc = new DataColumn(); 63 dc.AllowDBNull = true; 64 dc.ColumnName = pi.Name; 65 dc.DataType = pi.PropertyType; 66 result.Columns.Add(dc); 67 } 68 else 69 { 70 if (propertyNameList.Contains(pi.Name)) 71 result.Columns.Add(pi.Name, pi.PropertyType); 72 } 73 } 74 75 for (var i = 0; i < list.Count; i++) 76 { 77 var tempList = new ArrayList(); 78 foreach (PropertyInfo pi in propertys) 79 { 80 if (propertyNameList.Count == 0) 81 { 82 object obj = pi.GetValue(list[i], null); 83 tempList.Add(obj); 84 } 85 else 86 { 87 if (propertyNameList.Contains(pi.Name)) 88 { 89 var obj = pi.GetValue(list[i], null); 90 tempList.Add(obj); 91 } 92 } 93 } 94 object[] array = tempList.ToArray(); 95 result.LoadDataRow(array, true); 96 } 97 } 98 return result; 99 }