- 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";
protected void Page_Load(object sender, EventArgs e)
{ if (IsPostBack) return;
try
{
var list = new reportBL().report();
if (list.Count == )
{
Response.Write("没有信息!");
return;
}
DataTable dt = ListToDataTable(list);
var viewer = new ReportViewer();
viewer.LocalReport.ReportPath = @"Rpt\Rdlc\SampleForm.rdlc";
viewer.ProcessingMode = ProcessingMode.Local;
//这里把建好的数据集的名称替换掉DataSetName
var rds = new ReportDataSource("DataSetName", dt);
viewer.LocalReport.EnableExternalImages = true;
viewer.LocalReport.DataSources.Clear();
viewer.LocalReport.DataSources.Add(rds);
viewer.LocalReport.EnableExternalImages = true;
errCode = ;
viewer.LocalReport.Refresh();
var bytes = viewer.LocalReport.Render("Pdf");
errCode = ;
Response.ContentType = "application/pdf";
//设定编码方式,若输出的excel有乱码,可优先从编码方面解决
Response.Charset = "gb2312";
//Response.Charset = "utf-8";
Response.ContentEncoding = Encoding.GetEncoding("GB2312");
//关闭ViewState,此属性在Page中
EnableViewState = false;
//filenames是自定义的文件名
Response.AppendHeader("Content-Disposition", string.Format("attachment;filename={0}.pdf", DateTime.Now.ToString("yyyyMMddHHmmssffff")));
//content是步骤1的html,注意是string类型
Response.BinaryWrite(bytes);
Response.End(); }
catch (Exception ex)
{ Response.Write(ex.Message + ":" + errCode);
} }
public static DataTable ListToDataTable<T>(IList<T> list, params string[] propertyName)
{
var propertyNameList = new List<string>();
if (propertyName != null)
propertyNameList.AddRange(propertyName);
var result = new DataTable();
if (list.Count > )
{
PropertyInfo[] propertys = list[].GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
if (propertyNameList.Count == )
{
DataColumn dc = new DataColumn();
dc.AllowDBNull = true;
dc.ColumnName = pi.Name;
dc.DataType = pi.PropertyType;
result.Columns.Add(dc);
}
else
{
if (propertyNameList.Contains(pi.Name))
result.Columns.Add(pi.Name, pi.PropertyType);
}
} for (var i = ; i < list.Count; i++)
{
var tempList = new ArrayList();
foreach (PropertyInfo pi in propertys)
{
if (propertyNameList.Count == )
{
object obj = pi.GetValue(list[i], null);
tempList.Add(obj);
}
else
{
if (propertyNameList.Contains(pi.Name))
{
var obj = pi.GetValue(list[i], null);
tempList.Add(obj);
}
}
}
object[] array = tempList.ToArray();
result.LoadDataRow(array, true);
}
}
return result;
}