RDLC报表
- 格式:docx
- 大小:21.77 KB
- 文档页数:3
使⽤RDLC报表(三)--向RDLC报表传⼊参数在使⽤报表向客户展⽰结果数据时,实时的在报表中显⽰某些特定的数据是必需的,如:显⽰的部门、打印的⽇期等。
本⽂只简单的演⽰向报表内传⼊⼀个字符值。
如有其它问题,欢迎讨论。
1、新建⼀个⼯程TestReport,⼀个Form窗体,放⼊⼀个TextBox、⼀个Button按钮,再放⼊⼀个ReportViewer控件。
2、在ReportViewer上选择新建⼀个报表3、在打开的报表设计器中,选择⼯具栏的“报表”中的“报表参数”,新加⼀个参数,名称为content,数据类型为string,确定。
4、在报表设计器的页⾯上放⼊⼀个⽂本框,在⽂本框上按⿏标右键->属性,在“⽂本框属性”窗⼝中,选择“常规”选项卡内下部的“值”后⾯的“编辑表达式”按钮(就是那个Fx),在此窗⼝内,左下框内选择参数,在右下框将会出现在上⼀步中设置的参数,双击此参数,在上⾯的框内将出现所需要的表达式:=Parameters!content.Value。
保存此报表。
报表默认名称为Report1.rdlc。
5、在Form窗体内双击按钮,编写如下代码:this.reportViewer1.LocalReport.ReportEmbeddedResource = "TestReport.Report1.rdlc";ReportParameter rp = new ReportParameter("content", this.textBox1.Text);this.reportViewer1.LocalReport.SetParameters(new ReportParameter[] { rp });this.reportViewer1.RefreshReport();6、运⾏⼯程,在⽂本输⼊框内输⼊数据,按下按钮,数据是不是已经传⼊报表了?。
中动态控制 RDLC 报表在 程序中,可以选择使用水晶报表,功能确实强大。
但是 web 版的水晶报表好似存在的问题。
如果所作报表不是复杂的一塌糊涂的话,可以使用微软自带的Rdlc 报表。
Rdlc 优点:1:Rdlc 报表设计简单2:结果存成xml,易于控制3:导出格式作的很不错这里所说的动态控制报表所指的是:在一些时候,制作了报表之后希望在运行中可以动态的做一些小修改,比方说列的位置,用户控制显示那些列等等。
控制方法,尝试了这么几种:1:控制微软提供的报表对象的属性;2:报表全部自动生成3:修改报表源文件,然后加载。
控制微软提供的报表对象的属性:基于这个功能需求,一开始我想到的方法是通过控制微软提供的这些报表对象的属性来实现。
因为这种方法最人道了。
但是事与愿违,微软的ReportViewer对象是用来显示Report 的,自然不行;我使用的report 是自己设计的, localReport,找到Report 对象,里面方法有这个几个:report.GetDefaultPageSettings();report.GetDocumentMap()等,第一个是获取打印纸X德设置,第二个是获取doc 文档〔但是始终出错〕,都是只读属性;所以,第一种尝试失败。
第二种方法就是报表全部自动生成。
可以找到一个完整的例子,在这里:://gotreportviewer/DynamicTable.zip这个例子里面,他把xml结构的rdlc报表写成一个类ReportDefinition,然后通过自定义这个类的内容来得到一个报表。
其实际还是为了自己构造一个报表对象的xml。
这是加载自定义报表的过程: win 下的代码 this.reportViewer1.Reset();this.reportViewer1.LocalReport.LoadReportDefinition(m_rdl);this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("MyData", m_dataSet.Tables[0]));this.reportViewer1.RefreshReport();这是自动生成xml 的代码:privateMemoryStream GenerateRdl(List<string>allFields, List<string>selectedFields){MemoryStream ms = new MemoryStream();RdlGenerator gen = new RdlGenerator();gen.AllFields = allFields;gen.SelectedFields = selectedFields;gen.WriteXml(ms);ms.Position = 0;return ms;}这是完全 ReportDefinition的一局部定义:namespace Rdl {using System.Xml.Serialization;/**////< remarks/ >[System.CodeDompiler.GeneratedCodeAttribute("xsd","2.0.50727.42")][System.SerializableAttribute()][System.Diagnostics.DebuggerStepThroughAttribute()][SystemponentModel.DesignerCategoryAttribute("code")][System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)][System.Xml.Serialization.XmlRootAttribute(Namespace=_"://schemas.microsoft/sqlserver/reporting/2005/01/reportdefinition", IsNullable=false)]public partial class Report {private object[] itemsField;但是几经考虑之后,这个方案也不让人满意,原因是:所有的报表对象都得自己生成,一下子回到了解放前,没有可视化工具的设计既繁琐又复杂。
[原创]RDLC报表系列(五)RDLC报表分组-⼤熊的空间-博客园[原创] RDLC 报表系列(五) RDLC报表分组本⽂只代表作者在⼀定阶段的认识与理解。
⼀、写作前提在我的博客园中我写了关于⼀些RDLC报表的使⽤,请参考这⾥。
因为没有时间,所以没有导⼊到我的个⼈博客中。
在博客园中相关⽂章如下:[原创] RDLC 报表系列(⼀)创建⼀个报表[原创] RDLC 报表系列(⼆)报表中插⼊图⽚[原创] RDLC 报表系列(三)参数、常量及常⽤表达式的使⽤[原创] RDLC 报表系列(四)⼦报表的使⽤⼆、本⽂内容1. 加载RDLC报表数据2. 实现数据分组3. 总结4. 代码下载(下载)三、加载RDLC报表数据我们的⽰例是要从学⽣表中,取出200条学⽣信息,然后加载到RDLC报表中。
01using System;02using System.Collections;03using System.Configuration;04using System.Data;05using System.Linq;06using System.Web;07using System.Web.Security;08using System.Web.UI;09using System.Web.UI.HtmlControls;10using System.Web.UI.WebControls;11using System.Web.UI.WebControls.WebParts;12using System.Data.SqlClient;13using System.Collections.Generic;14using System.Xml.Linq;15using Microsoft.Reporting.WinForms;16using RCLC.DataEntity;1718namespace RCLC19{20 public partial class_Default : CustomPageBase21 {22 protected void Page_Load(object sender, EventArgs e)23 {2425 }2627 protected void ButtonReportGenerate_Click(object sender, EventArgs e)29 List<ReportDataSource> reportDataSource = new List<ReportDataSource>();30 RportDataSet ds = new RportDataSet();31 string templatePath = string.Empty;32 string totalRecords = string.Empty;3334 //获得数据35 SqlConnection conn = newSqlConnection(ConfigurationManager.ConnectionStrings["LoggingConnectionString"].ConnectionString);36 SqlCommand command = conn.CreateCommand();37 mandType = CommandType.Text;38 mandText = "SELECT TOP 200 * FROM T_STUDENT";39 SqlDataAdapter da = new SqlDataAdapter(command);40 da.Fill(ds.T_STUDENT);4142 //指定报表模板43 templatePath = "ReportTemplate/StudentReport.rdlc";4445 //把获取的数据集合提供给在报表中名为RportDataSet_T_STUDENT数据集46 reportDataSource.Add(new ReportDataSource("RportDataSet_T_STUDENT", ds.T_STUDENT));47 List<ReportParameter> parameterList = new List<ReportParameter>();48 ////Generate Report,报表可以⽣成PDF,EXCEL及以其它形式,根据需求去设置49 GetReportMultipleDataSourceFile(reportDataSource, templatePath, parameterList, "pdf");50 }51 }52}上⾯的代码已经获取到200条学⽣的信息,然后把数据从数据库中取出然后加载到StudentReport.rdlc 报表中,下⾯要做的事情就是对数据进⾏分组了。
RDLC系列(⼀)RDLC报表⾃定义数据源最近⼀段时间开发ERP系统中要⽤到不少报表打印,在⽹上找了⼀圈发现想些好⽤的报表控件⼤部分要收费,⼀些⾯免费要么不好⽤要么IE8不兼容,最后还是⽤了微软⾃带的RDLC报表,把⾃⼰遇到的坑和技巧整理分享出来。
⼀般Visaul Studio上新建的的EDLC报表⽂件之后数据源都是按照向导直接连接数据库,⾃动⽣成数据源和数据集的,但是遇到⼀些复杂的就不够灵活。
⼀、新建报表1.新建⼀个空⽩的报表如下2.打开新建好的空报表⽂件,选择报表⽂件右键选择【打开⽅式】→【XML(⽂本)编辑】打开在Page节点下⾯添加DataSources 和DataSets 节点2)添加节点后DataSources4.设计好需要额报表,填充刚才数据集⾥对应的字段5.后台数据源赋值代码public ActionResult PrintProduct(){string reportPath = Server.MapPath("~/Reports/订单信息.rdlc");var localReport = new LocalReport { ReportPath = reportPath };List<ProductOrder> productOrders = new List<ProductOrder>();for (int i = 0; i < 20; i++){ProductOrder productOrder=new ProductOrder{SKUDetail = $"A00300000{i}",Quantity = i,DeliveryDate = DateTime.Now.AddDays(i).ToString("yyyy-MM-dd HH:mm:ss"),DeliveryDateActual = DateTime.Now.AddDays(i+1).ToString("yyyy-MM-dd HH:mm:ss")};productOrders.Add(productOrder);}var dataSource = new ReportDataSource("ProductOrderDs", productOrders);localReport.DataSources.Add(dataSource);var type = "PDF";string reportType = type;string mimeType;string encoding;string fileNameExtension;var deviceInfo = $"<DeviceInfo><OutPutFormat>{type}</OutPutFormat></DeviceInfo>";Warning[] warnings;string[] streams;var renderedBytes = localReport.Render(reportType,deviceInfo,out mimeType,out encoding,out fileNameExtension,out streams,out warnings);return File(renderedBytes, mimeType);}实体代码:public class ProductOrder{public Guid ProductOrderId { get; set; }public string SKUDetail { get; set; }public int Quantity { get; set; }public string DeliveryDate { get; set; }public string DeliveryDateActual { get; set; }public string Bak { get; set; }}注意:报表⽂件DataSet中的⽤到的字段必须和实体字段对应,代码中DataSource 中名字必须和报表中的⼀样最后预览如下:作者出处:——当你的才华还撑不起你的野⼼时,你就应该静下⼼来好好学习~版权声明:本⽂版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在⽂章页⾯明显位置给出原⽂连接,否则保留追究法律责任的权利.。
RDLC报表一、RDLC介绍在VS2005之前,SQL Server Reporting Services中已经提供了一种被称为报表定义语言(Report Definition Language, RDL)的语言;在VS2005中,Microsoft提供了针对这种报表的设计器,并提供了在WinForm和WebForm中使用这种报表的能力。
RDL是Report Definition Language的缩写,C代表Client-side processing,凸显了它的客户端处理能力。
从SQL Server2005开始,微软提供了一个报表服务,即Reporting Service,利用该服务部署在服务器端的报表扩展名为rdl;而通过自主生成一些数据源,在本地展现出来的报表扩展名为rdlc,这种方式称之为Local Report。
二、如何开发下面是LocalReport的开发步骤,对于ServerReport还没有进行研究。
1.新建一个Web工程,新建一个aspx页面。
2.将ReportViewer控件添加到新建的aspx页面中。
(视图—工具箱—数据—ReportViewer)VS2005的数据控件中有名为ReportViewer的控件,用于展现报表。
该控件即可以展现rdl 报表也可以展现rdlc报表。
3.新建报表文件选中web工程—右键添加—新建项—选中报表(如下图)4.创建自己的报表布局(以柱状图为例)工具箱—图表—拖放到报表文件主体中选中图表—右键—图表类型—柱形图—简单柱形图可以直接修改属性调整布局,也可以利用工具栏中的布局按钮布局(包括水平居中、垂直居中、对齐网格等)5.创建数据集(选中web工程—右键添加—新建项—选中数据集)在新建的数据集面板右键—添加—TableAdapter配好数据库连接之后,下一步,有三种选择方式:使用sql语句、新建存储过程、使用现有存储过程。
根据需求选择一种配好自己的数据源。
RDLC报表1.1简介在visual studio 2005之前,微软提供了一个报表服务(Reporting Service),其中也提供了报表自定义语言Report Definition Language, RDL语言),其报表定义为.rdl文件;Microsoft提供了针对这种报表的设计器,并且提供了在并提供了在WinForm和WebForm中使用这种报表的能力.微软定义了RDLC (Report Definition Language Client)也就是报表定义语言的客户端也称本地报表,该报表的后缀为.rdlc.本地报表和服务器报表存在一定的区别。
服务器报表rdl是指那些针对报表服务部署在服务器端的报表。
当报表被许多用户访问、报表以发布在报表服务器上,将其包含在所创建和部署的应用程序中、报表中有复杂的查询或者包含数据量大导致应用程序所在计算机的系统资源超载时,用报表的远程处理比较合适。
本地报表rdlc是指在本地的一些报表,在使用报表不一定要依赖于SQL Server,而是可以自主生成一些数据源然后以报表的方式显现出来。
本地处理模式的功能不及远程处理功能强大,适合中小型好报表和数据集的应用程序。
如果和rdl比较,rdlc可以看成是一个轻量级的报表。
在展现报表的时候我们需要使用到的一个控件叫ReportViewer,这种控件可以展现rdl报表,也可以展现rdlc报表。
从其属性可以看出,ReportView.ServerReport是针对rdl报表的,而ReportViewer.LocalReport是针对rdlc报表的。
1.2RDLC功能RDLC报表提供了以下主要功能:1、含有简单易用尤其是Table控件,非常方便字段在报表上的排列。
2、灵活的可定制性,用XML来实现通用的报表打印,可以用XML完全可以实现一个基于RDLC的报表设计器,这样可以让终端用户参与到报表的设计中,至少可以使他们能够修改报表中一些标题、表头等3、高度可编程性,在你的项目中,甚至不需要有一个报表文件,通过代码就可以实现报表生成、预览和打印等一系列操作;4、支持数据钻取功能;钻取报表是通过设置Navigation(HyperLink)和Parameters 来实现的,而具有子报表的钻取报表实现的功能类似Excel中数据透视表(Pivot Table)的功能,在一个复杂的交叉表中可以进行时间和商品两个维度的向下钻取。
RDLC报表的纵向横向设置
RDLC报表的纵向横向设置
⽤ReportViewer 加载RDLC报表,若报表内容宽度超出⽤张A4纸的纵向宽度,那么报表靠右部分打印出来会被分割到下⽤页如果⽤户每次打印都需设置纵向横向,这样⽤户会感到⽤烦。
如何设置RDLC报表的纵向横向呢?
⽤先打开RDLC报表设计器页⽤。
在灰⽤部分点右键 -> 报表属性。
报表的默认设置都可以在此界⽤修改。
当选择纵向时,报表宽度21cm,⽤度29.7cm。
当选择横向时,宽度29.7cm,⽤度21cm 查看报表的XML代码,可发现页⽤设置部分在Page节点下。
<Page>
<PageHeight>21cm</PageHeight>
<PageWidth>29.7cm</PageWidth>
<LeftMargin>2cm</LeftMargin>
<RightMargin>2cm</RightMargin>
<TopMargin>2cm</TopMargin>
<BottomMargin>2cm</BottomMargin>
<ColumnSpacing>0.13cm</ColumnSpacing>
<Style />
</Page>
在C# 程序中,需判断报表的内容宽度是否超出默认的21cm,若超出则只需修改<PageHeight> 和<PageWidth>的属性值即可。
RDLC使用总结1、为什么要使用RDLC报表(简介)VS .NET开发中,用什么进行报表设计?可能的回答是Crystal Report,当然,必须承认Crystal Report的功能还是非常强大的,被Business Object收购以后,商业的成分也在逐渐增加,也形成了一定规模的用户群。
Visual Studio .NET进入2005版本以后,Crystal Report与IDE的结合更“紧密”了,至少我们看不到那个讨厌的注册窗口了。
但是,Microsoft似乎并不容忍在自己的超级工具中竟然没有报表工具,于是Report Viewer Control出现了,我把它的报表称为RDLC报表。
在VS .NET 2005之前,SQL Server Reporting Services中已经提供了一种被称为报表定义语言(Report Definition Language, RDL)的语言;在VS .NET 2005中,Microsoft提供了针对这种报表的设计器,并提供了在WinForm和WebForm中使用这种报表的能力。
Microsoft将这种报表的后缀定为RDLC,RDL仍然是Report Definition Language的缩写,那么C代表什么呢?C代表Client-side processing,凸显了它的客户端处理能力。
这种报表的易用性和可定制性让我们完全有理由放弃Crystal Report,让我们来看看它的强大功能吧:1)简单易用的控件,特别是Table控件,非常方便字段在报表上的排列;2)灵活的可定制性,用XML来描述一个报表相关的一切,不用说了吧;3)高度可编程性,在你的项目中,甚至不需要有一个报表文件,通过代码就可以实现报表生成、预览和打印等一系列操作;4)支持DrillThrough数据钻取功能;5)导出的Excel文件格式非常完美,而且并不需要安装Excel;6)数据源处理极其方便,开发人员可以自己接管数据库连接、取数,然后将数据结果赋值给RDLC的数据集即可。
Rdlc报表数据汇总分组展⽰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";1protected void Page_Load(object sender, EventArgs e)2 {34if (IsPostBack) return;5try6 {7var list = new reportBL().report();8if (list.Count == 0)9 {10 Response.Write("没有信息!");11return;12 }13 DataTable dt = ListToDataTable(list);14var viewer = new ReportViewer();15 viewer.LocalReport.ReportPath = @"Rpt\Rdlc\SampleForm.rdlc";16 viewer.ProcessingMode = ProcessingMode.Local;17//这⾥把建好的数据集的名称替换掉DataSetName18var 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();25var 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();394041 }42catch (Exception ex)43 {4445 Response.Write(ex.Message + ":" + errCode);46 }4748 }49public static DataTable ListToDataTable<T>(IList<T> list, params string[] propertyName)50 {51var propertyNameList = new List<string>();52if (propertyName != null)53 propertyNameList.AddRange(propertyName);54var result = new DataTable();55if (list.Count > 0)56 {57 PropertyInfo[] propertys = list[0].GetType().GetProperties(); 58foreach (PropertyInfo pi in propertys)59 {60if (propertyNameList.Count == 0)61 {62 DataColumn dc = new DataColumn();63 dc.AllowDBNull = true;64 dc.ColumnName = ;65 dc.DataType = pi.PropertyType;66 result.Columns.Add(dc);67 }68else69 {70if (propertyNameList.Contains())71 result.Columns.Add(, pi.PropertyType);72 }73 }7475for (var i = 0; i < list.Count; i++)76 {77var tempList = new ArrayList();78foreach (PropertyInfo pi in propertys)79 {80if (propertyNameList.Count == 0)81 {82object obj = pi.GetValue(list[i], null);83 tempList.Add(obj);84 }85else86 {87if (propertyNameList.Contains())88 {89var obj = pi.GetValue(list[i], null);90 tempList.Add(obj);91 }92 }93 }94object[] array = tempList.ToArray();95 result.LoadDataRow(array, true);96 }97 }98return result;99 }View Code。
RDLC 报表开发一、打开Visual Studio 2005,新建 网站,添加数据集,会自动调出数据集配置窗口TableAdapter如果上面的窗口没有自动调出,可以如下图可以调出上面的TableAdapter 窗口二、新建立数据库连接下面的这一步会将数据库连接保存到config 文件中下面的这一步可以,选择生成SQL的方式让我们先回到SQL Server Query Analyzer打开SQL Server 查询分析器在SQL程序中创建如下图的存储过程然后回到V isual Studio 2005,选择使用现在的存储过程,下一步,在这里,我们只需要选择所需要的存储过程,如刚才建立的EmployeeReport 即可完成TableAdapter向导的配置三、添加报表项创建报表项后,从左边工具栏拖动一个Table 到报表设计器中,选择网站数据集中的字段,把它拖动到表格的列中,通过页眉页脚可以设置复杂的打印样式。
四、新建立一个ASPX页面,拖动一个Report V iewer控件到页面中,在ReportViewer任务窗口中,选择刚才建立的rdl c文件,生成解决方案,然后执行就完成了报表开发。
[ 注意:如果此处报“未能找到在ObjectDataSource ObjectDataSource1 的TypeName 属性中指定的类型”的错误,说明数据源配置不正确,可以选择正确的配置即可。
]五、注意1. 刚才的SQL 脚本CREA TE PROC EmployeeReportASSELECT * FROM EmployeeGO实际的报表开发中,一定不要用SELECT * ,只取报表中需要查看的字段。
2. 有时候,可能需要用户选择一些条件,有选择性的查看报表。
而不是全部绑定数据如上图,用户可能只需要查看2008-9-29至2008-9-30时间段之间的数据则作法如下:先建立好如上图的ASPX页面,在View Report 事件中写如下的程序ReportViewer1.LocalReport.ReportPath = AppDomain.CurrentDomain.BaseDirectory + "/Report/Request.rdlc";DateTime dtFrom =Convert.ToDateTime(txtDateFrom.Text);DateTime dtTo =Convert.ToDateTime(txtDateTo.Text);string requester = txtRequester.Text;string dept = txtRequestDept.Text;string material = ddlMaterial.SelectedV alue;string iprstatus = ddlStatus.SelectedV alue;DataTable reqrpt = ReportDB.RequestReport(dtFrom, dtTo, material, dept,requester, iprstatus);if (reqrpt != null){ReportViewer1.LocalReport.DataSources.Clear();ReportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource("Request_RequestReport", reqrpt)); ReportViewer1.LocalReport.Refresh();}ReportViewer1.LocalReport.Refresh();根据用户所选的参数,把数据值传到SQL语句中即可.下面是RequestReport方法的源码DataTable RequestReport(DateTime dtFrom, DateTime dtTo, string pMaterial, string pDept, string pRequester, string pIPRStatus) {string MySQL = Purchase;string whDate = " RequestDate BETWEEN '{0}' AND '{1}' ";MySQL = MySQL + string.Format(whDate, dtFrom, dtTo);string whMaterial = " AND MaterialCode='{0}' ";if (pMaterial != "ALL"){MySQL = MySQL + string.Format(whMaterial, pMaterial);}string whDept = " AND RequestDepartment='{0}' ";MySQL = MySQL + string.Format(whDept, pDept);string whRequester=" AND Requester='{0}' ";if(pRequester!="ALL")MySQL = MySQL + string.Format(whRequester, pRequester);string whIPRStatus = " AND IPRStatus={0} ";if (pIPRStatus != "ALL"){MySQL = MySQL + string.Format(whIPRStatus, pIPRStatus);}IDataProvider privider = DataProvider.CreateDataProvider();DataSet ds = privider.RetriveDataSet(MySQL);if (ds != null && ds.Tables.Count > 0)return ds.Tables[0];elsereturn null;}const string Purchase="SELECT SerialNO,LedgerAcc,CostCenter,Requester,"+" RequestDate,RequestDepartment,MaterialCode, " +" Brand,Specifications,Unit,Quantity,Usage, "+" ExpectedDeliveryDate,Currency "+" ,Quotation1Supplier,Quotation1UnitPrice,Quotation1Amount, "+" Quotation2Supplier,Quotation2UnitPrice,Quotation2Amount, "+" Quotation3Supplier, Quotation3UnitPrice, Quotation3Amount, "+" ProposedQuotationSupplier, ProposedQuotationUnitPrice, "+" ProposedQuotationAmount,QuotationRemarks ,IPRStatus,QtyTo, UnitPriceTo FROM IPR WHERE ";3. 设计报表时,可以用上述的方法,实际运行时,可以替换成SQL 语句,传到ReportDataSource中即可,只要相应的表结构字段是存在的。
rdlc报表随笔⼼得,基本结构和⼀些表达式。
Dataset Form RDLC主要放数据集的⽂件夹存放窗体的⽂件夹存放各种报表的⽂件夹第⼀部,创建报表结构⾸先添加数据集项添加完成之后我们会看到这个页⾯之后我们在上⾯添加⼀些数据集数据源的连接要⾃⼰设定之后我们可以输⼊要编译的sql语句,因为是报表,主要⽤来查询,⼀般情况只⽤select语句就可以⽣成之后是这样的⼀些模型中间的灰⾊部分可以双击,进去之后我们能⾃定义编写⼀些⽅法//⽤这个⽅法查询时可以进⾏拼sql拼接查询,⼀般不写这个的话是不可以拼接查询的//加粗部分要根据你不同数据集下的⼩的数据模型名字不同进⾏变换partial class MemberTransferShopTableAdapter{internal int FillWhere(MC.MemberTransferShopDataTable dataTable, string whereSql, params SqlParameter[] commandParameters) {string sql = mandCollection[0].CommandText;sql = sql.Substring(0, stIndexOf("WHERE"));sql += " WHERE 1=1 " + whereSql;mandCollection[0].CommandText = sql;this.Adapter.SelectCommand = mandCollection[0];mandCollection[0].Parameters.Clear();foreach (SqlParameter p in commandParameters){mandCollection[0].Parameters.Add(p);}dataTable.Clear();int returnValue = this.Adapter.Fill(dataTable);return returnValue;}}下⾯展⽰两种Form中的连接数据的⽅法this.MemberTransferShopTableAdapter.Fill(this.cashier.MemberTransferShopDtl, A参数, A参数,A参数); //参数是你写语句时的参数,报表普遍⽤这种⽅法以下是Fillwhere拼接的⽅法,拼接⽅法省去了在数据库⾥判断的过程,可以减少where后查询的判断,从⽽加快⼀定速度。
RDLC报表使用步骤主要介绍使用业务对象数据进行报表设计的过程。
(1)在项目上右击,添加报表。
(2)打开报表属性对话框(3)添加Model程序集引用(4)重新生成Model,这样的话,点击菜单上的“数据”,才会有添加数据新数据源和显示数据源项。
(5)若界面上没报表数据工具条,则点击菜单上的视图,报表数据,这样就调出了报表数据工具条。
若界面上有的话,此步省略。
(6)在菜单上选择数据,添加数据源,在弹出的对话框选择对象,然后即可选择Model 下的字段了。
(7)在报表工具表上,点击新建,添加数据集,弹出对话框,给数据集命名,并选择相应的Model。
(8)到此为止就可以报表的设计了。
报表的布局,想怎么弄就怎么弄吧。
(9)报表设计好了,需要给一个浏览报表的窗口,提供这个功能的就ReportView控件,在工具箱中可以找到,托到一个Web页面上即可。
还要加一个”ScripManager”控件,这个控件是在AJAX Extensions项下面,只要托到页面上即可,不用管它了。
(10)接下来需要给ReportView控件配置数据源了。
有两种方法。
一种是像给GirdView、FormView选择数据源一样,点击ReportView控件右侧的三角形符号,弹出一个小窗口,选择新建数据源,选择对象,然后选择BLL下的方法即,这将自动将ObjectDataSource加入到这个页面来。
第二种方法,是通过手写代码来配置数据源,这种方法适应比较复杂的情况,比如你需要对List集合下的字段进行改动,再配置给ReportView。
后面我给了一小段代码,这段代码是如何将报表按照数据库中的址将图片加载到报表中的,很典型。
(11)这样就做好了,接下来是发布了。
(12)注意,发布后,必须将RDLC报表拷到发布的目录下,覆盖掉发布过程中生成的RDLC 文件(这个文件只有1K)。
存在的问题RDLC这种报表,不支持直接打印。
必须将报表导出到PDF WORD EXCEL中。
RDLC报表格式设置RDLC报表格式设置设置数字格式下表列出了常用的 .NET 数字格式设置字符串。
格式字符串名称C 或c 货币D 或d 小数E 或e 科学记数法F 或f 固定点G 或g 常规N 或n 数量P 或p 百分比R 或r 往返X 或x 十六进制您可以将许多格式字符串修改为包含精度说明符,该说明符用于定义小数点后的位数。
例如,格式设置字符串D0 将数字格式设置为小数点后没有数字。
您还可以使用自定义的格式设置字符串,例如#,###。
设置日期格式下表列出了常用的 .NET Framework 日期格式设置字符串。
格式字符串名称d 短日期[2008.08.08]D 长日期[2008年08月08日]t 短时间T 长时间f 完整日期/时间(短时间)F 完整日期/时间(长时间)g 常规日期/时间(短时间)[2008.08.08 8:8]G 常规日期/时间(长时间)M 或m 月日R 或r RFC1123 模式Y 或y 年月您还可以使用自定义的格式设置字符串,例如dd/MM/yy。
有关 .NET Framework 格式设置字符串的详细信息,请参阅Formatting Types。
例:说明:如果参考进价>=100000,则保留1位小数,否则保留2位小数.=IIF(Fields!参考进价.Value>=100000,"F1" , "F2")Public Sub Main()' Display string representations of numbers for en-us culture Dim ci As New CultureInfo("en-us")' Output floating point valuesDim floating As Double = 10761.937554Console.WriteLine("C: {0}", _floating.ToString("C", ci)) ' Displays "C: $10,761.94" Console.WriteLine("E: {0}", _floating.ToString("E03", ci)) ' Displays "E: 1.076E+004" Console.WriteLine("F: {0}", _floating.ToString("F04", ci)) ' Displays "F: 10761.9376"Console.WriteLine("G: {0}", _floating.ToString("G", ci)) ' Displays "G: 10761.937554"Console.WriteLine("N: {0}", _floating.ToString("N03", ci)) ' Displays "N: 10,761.938"Console.WriteLine("P: {0}", _(floating/10000).ToString("P02", ci)) ' Displays "P: 107.62 %"Console.WriteLine("R: {0}", _floating.ToString("R", ci)) ' Displays "R: 10761.937554"Console.WriteLine()' Output integral valuesDim integral As Integer = 8395Console.WriteLine("C: {0}", _integral.ToString("C", ci)) ' Displays "C: $8,395.00" Console.WriteLine("D: {0}", _integral.ToString("D6")) ' Displays D: 008395"" Console.WriteLine("E: {0}", _integral.ToString("E03", ci)) ' Displays "E: 8.395E+003" Console.WriteLine("F: {0}", _integral.ToString("F01", ci)) ' Displays "F: 8395.0" Console.WriteLine("G: {0}", _integral.ToString("G", ci)) ' Displays "G: 8395" Console.WriteLine("N: {0}", _integral.ToString("N01", ci)) ' Displays "N: 8,395.0" Console.WriteLine("P: {0}", _(integral/10000).ToString("P02", ci)) ' Displays "P: 83.95 %" Console.WriteLine("X: 0x{0}", _integral.ToString("X", ci)) ' Displays "X: 0x20CB" Console.WriteLine()End SubPublic Shared Sub Main()Dim msgShortDate As String = "(d) Short date: . . . . . . . " Dim msgLongDate As String = "(D) Long date:. . . . . . . . " Dim msgShortTime As String = "(t) Short time: . . . . . . . " Dim msgLongTime As String = "(T) Long time:. . . . . . . . " Dim msgFullDateShortTime As String = _"(f) Full date/short time: . . "Dim msgFullDateLongTime As String = _"(F) Full date/long time:. . . "Dim msgGeneralDateShortTime As String = _"(g) General date/short time:. "Dim msgGeneralDateLongTime As String = _"(G) General date/long time (default):" & vbCrLf & _" . . . . . . . . . . . . . "Dim msgMonth As String = "(M) Month:. . . . . . . . . . "Dim msgRFC1123 As String = "(R) RFC1123:. . . . . . . . . "Dim msgSortable As String = "(s) Sortable: . . . . . . . . "Dim msgUniSortInvariant As String = _"(u) Universal sortable (invariant):"& vbCrLf & _" . . . . . . . . . . . . . "Dim msgUniFull As String = "(U) Universal full date/time: "Dim msgYear As String = "(Y) Year: . . . . . . . . . . "Dim msgRoundtripLocal As String = "(o) Roundtrip (local):. . . . "Dim msgRoundtripUTC As String = "(o) Roundtrip (UTC):. . . . . "Dim msgRoundtripUnspecified As String = "(o) Roundtrip (Unspecified):. "Dim msg1 As String = "Use ToString(String) and the current thread culture." & vbCrLfDim msg2 As String = "Use ToString(String, IFormatProvider) and a specified culture." & vbCrLfDim msgCulture As String = "Culture:"Dim msgThisDate As String = "This date and time: {0}" & vbCrLfDim thisDate As DateTime = DateTime.NowDim utcDate As DateTime = thisDate.ToUniversalTime()Dim unspecifiedDate As DateTime = new DateTime(2000, 3, 20, 13, 2, 3, 0, DateTimeKind.Unspecified)Dim ci As CultureInfo' Format the current date and time in various ways.Console.Clear()Console.WriteLine("Standard DateTime Format Specifiers:" & vbCrLf)Console.WriteLine(msgThisDate, thisDate)Console.WriteLine(msg1)' Display the thread current culture, which is used to format the values. ci = Thread.CurrentThread.CurrentCulture Console.WriteLine("{0,-30}{1}" & vbCrLf, msgCulture, ci.DisplayName)Console.WriteLine(msgShortDate & thisDate.ToString( "d")) Console.WriteLine(msgLongDate & thisDate.ToString( "D")) Console.WriteLine(msgShortTime & thisDate.ToString( "t")) Console.WriteLine(msgLongTime & thisDate.ToString( "T")) Console.WriteLine(msgFullDateShortTime & thisDate.T oString( "f"))Console.WriteLine(msgFullDateLongTime & thisDate.T oString( "F"))Console.WriteLine(msgGeneralDateShortTime & thisDate.T oString("g"))Console.WriteLine(msgGeneralDateLongTime & thisDate.T oString( "G"))Console.WriteLine(msgMonth & thisDate.ToString( "M"))Console.WriteLine(msgRFC1123 & utcDate.ToString( "R"))Console.WriteLine(msgSortable & thisDate.ToString( "s"))Console.WriteLine(msgUniSortInvariant & utcDate.T oString( "u"))Console.WriteLine(msgUniFull & thisDate.ToString( "U"))Console.WriteLine(msgYear & thisDate.ToString( "Y"))Console.WriteLine(msgRoundtripLocal & thisDate.T oString( "o"))Console.WriteLine(msgRoundtripUTC & utcDate.T oString( "o"))Console.WriteLine(msgRoundtripUnspecified & unspecifiedDate.ToString("o"))Console.WriteLine()' Display the same values using a CultureInfo object. The CultureInfo class' implements IFormatProvider.Console.WriteLine(msg2)' Display the culture used to format the values.ci = New CultureInfo("de-DE")Console.WriteLine("{0,-30}{1}" & vbCrLf, msgCulture, ci.DisplayName)Console.WriteLine(msgShortDate & thisDate.T oString( "d", ci))Console.WriteLine(msgLongDate & thisDate.T oString( "D", ci))Console.WriteLine(msgShortTime & thisDate.T oString( "t", ci))Console.WriteLine(msgLongTime & thisDate.T oString( "T", ci))Console.WriteLine(msgFullDateShortTime & thisDate.T oString( "f", ci))Console.WriteLine(msgFullDateLongTime & thisDate.T oString( "F", ci))Console.WriteLine(msgGeneralDateShortTime & thisDate.T oString("g", ci))Console.WriteLine(msgGeneralDateLongTime & thisDate.T oString( "G", ci))Console.WriteLine(msgMonth & thisDate.ToString( "M", ci))Console.WriteLine(msgRFC1123 & utcDate.ToString( "R", ci)) Console.WriteLine(msgSortable & thisDate.ToString( "s", ci)) Console.WriteLine(msgUniSortInvariant & utcDate.T oString( "u", ci))Console.WriteLine(msgUniFull & thisDate.ToString( "U", ci)) Console.WriteLine(msgYear & thisDate.ToString( "Y", ci))Console.WriteLine(msgRoundtripLocal & thisDate.T oString( "o"), ci)Console.WriteLine(msgRoundtripUTC & utcDate.T oString( "o"), ci)Console.WriteLine(msgRoundtripUnspecified & unspecifiedDate.ToString("o"), ci)Console.WriteLine()End Sub 'MainEnd Class 'Sample''This code example produces the following results:''Standard DateTime Format Specifiers:''This date and time: 4/17/2006 2:29:09 PM''Use ToString(String) and the current thread culture.''Culture: English (United States)''(d) Short date: . . . . . . . 4/17/2006'(D) Long date:. . . . . . . . Monday, April 17, 2006'(t) Short time: . . . . . . . 2:29 PM'(T) Long time:. . . . . . . . 2:29:09 PM'(f) Full date/short time: . . Monday, April 17, 2006 2:29 PM '(F) Full date/long time:. . . Monday, April 17, 2006 2:29:09 PM '(g) General date/short time:. 4/17/2006 2:29 PM'(G) General date/long time (default):' . . . . . . . . . . . . . 4/17/2006 2:29:09 PM'(M) Month:. . . . . . . . . . April 17'(R) RFC1123:. . . . . . . . . Mon, 17 Apr 2006 21:29:09 GMT'(s) Sortable: . . . . . . . . 2006-04-17T14:29:09'(u) Universal sortable (invariant):' . . . . . . . . . . . . . 2006-04-17 21:29:09Z'(U) Universal full date/time: Monday, April 17, 2006 9:29:09 PM '(Y) Year: . . . . . . . . . . April, 2006'(o) Roundtrip (local):. . . . 2006-04-17T14:29:09.3011250-07:00 '(o) Roundtrip (UTC):. . . . . 2006-04-17T21:29:09.3011250Z '(o) Roundtrip (Unspecified):. 2000-03-20T13:02:03.0000000 ''Use ToString(String, IFormatProvider) and a specified culture. ''Culture: German (Germany)''(d) Short date: . . . . . . . 17.04.2006'(D) Long date:. . . . . . . . Montag, 17. April 2006'(t) Short time: . . . . . . . 14:29'(T) Long time:. . . . . . . . 14:29:09'(f) Full date/short time: . . Montag, 17. April 2006 14:29'(F) Full date/long time:. . . Montag, 17. April 2006 14:29:09 '(g) General date/short time:. 17.04.2006 14:29'(G) General date/long time (default):' . . . . . . . . . . . . . 17.04.2006 14:29:09'(M) Month:. . . . . . . . . . 17 April'(R) RFC1123:. . . . . . . . . Mon, 17 Apr 2006 21:29:09 GMT'(s) Sortable: . . . . . . . . 2006-04-17T14:29:09'(u) Universal sortable (invariant):' . . . . . . . . . . . . . 2006-04-17 21:29:09Z'(U) Universal full date/time: Montag, 17. April 2006 21:29:09 '(Y) Year: . . . . . . . . . . April 2006'(o) Roundtrip (local):. . . . 2006-04-17T14:29:09.3011250-07:00 '(o) Roundtrip (UTC):. . . . . 2006-04-17T21:29:09.3011250Z '(o) Roundtrip (Unspecified):. 2000-03-20T13:02:03.0000000 rdlc报表的时间格式关键字FormatDateTime类型函数——日期和时间示例表达式=FormatDateTime(Fields!字段名.Value, DateFormat.ShortDate)=FormatDateTime(Fields!字段名.Value, vbShortDate)备注该函数用于返回表示日期/时间值的字符串表达式。
csdn rdlc报表使用手册RDLC(Report Definition Language Client)是微软提供的一种报表定义语言,用于创建和部署本地报表。
RDLC报表使用基于XML的RDLC文件格式,可以在客户端应用程序中使用,不需要依赖于服务器端的报表服务。
RDLC报表使用手册一、RDLC报表的基本概念1.报表设计器:RDLC报表的设计工具,可以在Visual Studio中打开和编辑。
2.数据集:报表所需的数据源。
可以是数据表、数据集以及通过查询语句获取的数据。
3.数据源:报表绑定的数据源对象。
4.数据绑定:将报表中的控件与数据源中的字段进行关联。
5.表格:报表中用于显示数据的主要控件,可以设置列、行、组和总计等。
6.图表:报表中用于可视化数据的控件,可以用于展示柱状图、折线图、饼图等。
7.矩阵:报表中用于显示交叉表格的控件,可以实现多维数据的展示。
8.列表:报表中用于显示动态数据的控件,可以根据数据集的行数重复显示。
二、RDLC报表的创建和设计步骤1.打开Visual Studio,并创建一个新的Windows Forms项目。
2.在项目中添加RDLC报表文件,可以右键项目,选择添加->新建项,在模板中选择RDLC文件。
3.在报表设计器中,可以添加数据源和数据集。
选择数据源对象,并设置查询语句或连接字符串。
4.在报表设计器中,通过拖拽控件的方式添加表格、图表、矩阵或列表等控件。
5.设置控件的属性和样式,如字体、颜色、边框等。
6.设置数据绑定,将报表中的控件与数据源中的字段关联起来。
7.设置表格、图表、矩阵或列表等的相关属性,如分组、排序、总计等。
8.预览或发布报表,可以在报表设计器中预览报表效果,也可以在应用程序中调用报表进行展示。
三、RDLC报表的数据绑定和常用控件1.数据绑定:可以通过设置控件的Data Source属性,选择报表的数据集。
然后通过设置控件的Value属性,将控件与数据集中的字段进行绑定。
1.创建报表首先,创建数据源(连接数据库或者直接在工程添加DataSet),在此以DataSet 为例。
向工程添加数据集DataSet,再向数据集添加数据表DataTable,在表中添加字段(点击某一字段,选择属性可以修改字段类型)。
这样数据源就那立好了。
如下图所示:然后,在工程添加rdlc报表,在报表中添加表,在表中设置相应的显示字段如下图所示:最后,Form中添加报表浏览器ReportView,并且在ReportView对象的右上角点击小三角型,设置上面做好的rdlc报表如下图所示:如果数据源为DataSet,就要手动添加数据参考代码段如下:DataRow dr6 = ds1.Tables[0].NewRow();dr6.SetField("Name", "陈娇");dr6.SetField("Address", "广东增城新塘");dr6.SetField("Sex", "女");ds1.Tables[0].Rows.Add(dr6);this.reportViewer1.LocalReport.DataSources.Clear();this.reportViewer1.LocalReport.DataSources.Add(newMicrosoft.Reporting.WinForms.ReportDataSource("DataSet1", ds1.Tables[0]));this.reportViewer1.RefreshReport();2.添加字段(列)注意:在数据源的数据表中添加字段后,要在“视图”—“报表数据”弹出的界面中刷新数据源才能在报表只使用3.报表分组3.1基本分组在报表中右键点击数据单元格(设置好数据字段的单元格)选择“添加组”—“下方相邻”,如下图所示:然后就设置新添加的组,旧的字段就可以手动删掉了另外也可以右键点击行组,进行添加分组,如下图所示:3.2分组统计右键点击某一组的数据字段,操作界面如下图所示:然后在添加的行上右键点击“文本框属性”设置表达式,操作界面如下图所示:最终效果如下图所示:。
微软RDLC报表,动态加载图片文件(二维码)我是一个程序员,有时候莫名其妙的遇到一些不好解决的问题,于是我就记录了下来,希望对大家有用!开发环境:VS2010(中文),IIS7.0,IE8及以上1、首先讲一下,我的动态的图片是一个二维码。
(1)引用一下ThoughtWorks.QRCode.dll这个DLL,如果没有的话去网上下载一个。
或ThoughtWorks.QRCode.rar者我这个word文档里面是有一个的(2)然后呢,当然要有一个存放二维码的文件夹,用来存放生成之后的二维码,这个名字随便起(3)最后,当然是报表后台的代码了,不过RDLC报表是动态生成的,要写在aspx.cs 里面,写的这么清楚了,大家不会不明白吧:private void create_two(string nr)//nr 这个参数当然是需要生成二维码的文字或字段{Bitmap bt;string enCodeString = nr;QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();bt = qrCodeEncoder.Encode(enCodeString, Encoding.UTF8);string filename = nr;bt.Save(Server.MapPath("~/QRImage/") + filename + ".jpg");}(4)当然如果报错了,报红线了,那当然要去解析一下,system.drawing神马的,去引用一下吧,当然你加进来的ThoughtWorks也要去引用一下。
(例如using houghtWorks.QRCode.Codec;)2、当然是要把生成的这个二维码放到恶心的RDLC报表上,这里讲解的是VS2010,如果环境不对,请绕路。
(1)我觉得注解的够清楚了。
(2)(3)后台代码绑定protected void Page_Load(object sender, EventArgs e){if (!IsPostBack){string sid = Request.QueryString["id"];ReportViewer1.ProcessingMode = ProcessingMode.Local;ReportViewer1.LocalReport.ReportPath =erver.MapPath("~/Report/Report/SDBXZ.rdlc");ReportViewer1.LocalReport.EnableExternalImages = true;StringBuilder sb = new StringBuilder();sb.Append("select * from SDBXZ_View where JGJSID='" + sid + "'");string con =System.Configuration.ConfigurationManager.ConnectionStrings["CostManagementSystem"].ToString();DataSet ds = new DataSet();SqlConnection sqlcon = new SqlConnection(con);SqlCommand sqlcom = new SqlCommand(sb.ToString(), sqlcon);SqlDataAdapter sqlad = new SqlDataAdapter();sqlad.SelectCommand = sqlcom;sqlad.Fill(ds, "sql");///取得一个字段,我取得是GUIDstring guid =ds.Tables["sql"].Rows[0]["JGJSID"].ToString().Replace("-","").Trim();///生成二维码,并保存到指定目录,在最上面这个方法已经写在那里了create_two(guid);///拼接一个URL(存放图片的URL)string url = "file:///" + Server.MapPath("~/QRImage/") + guid + ".jpg";LocalReport report = ReportViewer1.LocalReport;///把URL付给参数EWM(图片绑定的参数)report.SetParameters(new ReportParameter("EWM", url)); ReportViewer1.LocalReport.DataSources.Add(newReportDataSource("DataSet1", ds.Tables["sql"]));ReportViewer1.DataBind();this.ReportViewer1.ServerReport.Refresh();}}3、测试下吧,大功告成!。
动态生成rdlc报表(原创)动态生成rdlc 报表(原创)因为公司需求研究微软的Reportviewer 因为有许多特别要求所以动态调用比较灵活我的需求是根据数据不同的合并表头(参考了随心所欲的博客文档再次表示感谢)string cCount = "";string dCount = "";string jCount = "";protected void Page_Load(object sender, EventArgs e){if (!IsPostBack){string id = Request.QueryString["OrderID"] == null ? "1" : Request.QueryString["OrderID"].ToString();SqlConnection con = new SqlConnection("server=CHEN ZQ;uid=sa;pwd=luca623;database=luca");SqlDataAdapter sda = new SqlDataAdapter("select * fro m view_Order where C_orderID='" + id + "'", con);DataSet ds = new DataSet();sda.Fill(ds);cCount = ds.Tables[0].Rows[0]["C_CTime"].ToString();dCount = ds.Tables[0].Rows[0]["C_TTime"].ToString();jCount = ds.Tables[0].Rows[0]["C_DTime"].ToString();复制代码//这段代码是最重要ReportViewer1.Reset();this.ReportViewer1.LocalReport.LoadReportDefinition(G enerateRdlc());ReportViewer1.LocalReport.DataSources.Clear();复制代码//Orders_DataTable1 数据源名字必须和此报表原绑定的数据源名相同this.ReportViewer1.LocalReport.DataSources.Add(new R eportDataSource("Orders_DataTable1", ds.Tables[0]));this.ReportViewer1.LocalReport.Refresh();}}//这个方法就是自定义报表的样式public MemoryStream GenerateRdlc(){XmlDocument sourceDoc = new XmlDocument();string path = AppDomain.CurrentDomain.BaseDirectory + "Orders.rdlc";//c_CTime = ds.Tables[0].Rows[0]["C_CTime"].ToString();sourceDoc.Load(path);//下面就是xml操作了没必要看我的根据自己的需求而做XmlNode xHeader = sourceDoc.ChildNodes.Item(1).Chil dNodes.Item(13).ChildNodes.Item(1).ChildNodes.Item(0).Chi ldNodes.Item(4);XmlNode xCells = xHeader.ChildNodes.Item(0).ChildNod es.Item(0).ChildNodes.Item(0);//建设期XmlNode xmlCell = xCells.ChildNodes.Item(1);XmlElement xeCol = sourceDoc.CreateElement("ColSpan ");xeCol.InnerText = cCount;xeCol.InnerXml = cCount;xmlCell.InnerXml += xeCol.OuterXml;XmlNode xmlCellValue = xmlCell.ChildNodes.Item(0).Chi ldNodes.Item(0).ChildNodes.Item(4);xmlCellValue.InnerXml = "建设期";xmlCellValue.InnerText = "建设期";XmlNode xnRemove;for (int i = 0; i <int.Parse(cCount) - 1; i++){xnRemove = xCells.ChildNodes.Item(2);xCells.RemoveChild(xnRemove);}//投产期XmlNode xmlCellT = xCells.ChildNodes.Item(2);XmlElement xeColT = sourceDoc.CreateElement("ColSpa n");xeColT.InnerText = dCount;xeColT.InnerXml = dCount;xmlCellT.InnerXml += xeColT.OuterXml;XmlNode xmlCellValueT = xmlCellT.ChildNodes.Item(0).C hildNodes.Item(0).ChildNodes.Item(4);xmlCellValueT.InnerXml = "投产期";xmlCellValueT.InnerText = "投产期";for (int j = 0; j < int.Parse(dCount) - 1; j++){xnRemove = xCells.ChildNodes.Item(3);xCells.RemoveChild(xnRemove);}//生产期XmlNode xmlCellC = xCells.ChildNodes.Item(3);XmlElement xeColC = sourceDoc.CreateElement("ColSpa n");xeColC.InnerText = jCount.ToString();xeColC.InnerXml = jCount.ToString();xmlCellC.InnerXml += xeColC.OuterXml;XmlNode xmlCellValueC = xmlCellC.ChildNodes.Item(0). ChildNodes.Item(0).ChildNodes.Item(4);xmlCellValueC.InnerXml = "生产期";xmlCellValueC.InnerText = "生产期";for (int j = 0; j < int.Parse(jCount) - 1; j++){xnRemove = xCells.ChildNodes.Item(4);xCells.RemoveChild(xnRemove);}MemoryStream ms = new MemoryStream();XmlSerializer serializer = new XmlSerializer(typeof(XmlD ocument));serializer.Serialize(ms, sourceDoc);ms.Position = 0;return ms;}复制代码技巧可以先在报表里自己设计好需要的格式用XmlDocument sourceDoc = new XmlDocument();string path = AppDomain.CurrentDomain.BaseDirectory + " Orders.rdlc";然后用sourceDoc .save()保存生成xml可以看到此xml你需要改的格式是哪个地方。
RDLC报表使用步骤
主要介绍使用业务对象数据进行报表设计的过程。
(1)在项目上右击,添加报表。
(2)打开报表属性对话框
(3)添加Model程序集引用
(4)重新生成Model,这样的话,点击菜单上的“数据”,才会有添加数据新数据源和显示数据源项。
(5)若界面上没报表数据工具条,则点击菜单上的视图,报表数据,这样就调出了报表数据工具条。
若界面上有的话,此步省略。
(6)在菜单上选择数据,添加数据源,在弹出的对话框选择对象,然后即可选择Model 下的字段了。
(7)在报表工具表上,点击新建,添加数据集,弹出对话框,给数据集命名,并选择相应的Model。
(8)到此为止就可以报表的设计了。
报表的布局,想怎么弄就怎么弄吧。
(9)报表设计好了,需要给一个浏览报表的窗口,提供这个功能的就ReportView控件,在工具箱中可以找到,托到一个Web页面上即可。
还要加一个”ScripManager”控件,这个控件是在AJAX Extensions项下面,只要托到页面上即可,不用管它了。
(10)接下来需要给ReportView控件配置数据源了。
有两种方法。
一种是像给GirdView、FormView选择数据源一样,点击ReportView控件右侧的三角形符号,弹出一个小窗口,选择新建数据源,选择对象,然后选择BLL下的方法即,这将自动将
ObjectDataSource加入到这个页面来。
第二种方法,是通过手写代码来配置数据源,这种方法适应比较复杂的情况,比如你需要对List集合下的字段进行改动,再配置
给ReportView。
后面我给了一小段代码,这段代码是如何将报表按照数据库中的址将图片加载到报表中的,很典型。
(11)这样就做好了,接下来是发布了。
(12)注意,发布后,必须将RDLC报表拷到发布的目录下,覆盖掉发布过程中生成的RDLC 文件(这个文件只有1K)。
存在的问题
RDLC这种报表,不支持直接打印。
必须将报表导出到PDF WORD EXCEL中。
在浏览器中显示的表格右不对齐,当表格中没有数据,是右对齐的,当有数据后不右
对齐了,也就是说由于表格的大小不能固定。
但导出的PDF没问题。
始终是对齐的。
解决方案:选中文本框,在属性中->常规->Cangrow->false
报表的主体尺才必须要小于页面设计尺寸。
如:在报表属性里,页面设为A4,宽21cm,
长29.7cm。
那么报表的主体的宽度必须要小于21cm。
否则导出的PDF有空页。
文本框中文字竖排
解决方案:选中文本框,在属性中->本地化->WritingMode ->Verticle
典型代码
问题描述:一般情况,图片的地址是存放在数据库中。
而图片是存放在磁盘上的。
报表需要将磁盘上的图片展示出来,如何做呢?请看下面的例子。
// ReportView 绑定数据源
protected void BindReportViewer()
{
try
{
//接收文档编号的ID
Guid documentId = new Guid(Request.QueryString["id"].ToString());
//获取定期检查记录照片的list集合
TaiAnBridge.BLL.Examine.RecordRegularExamine rre = new
TaiAnBridge.BLL.Examine.RecordRegularExamine();
List<Model.RecordRegularExamine_Photo> list = rre.GetPhoto(documentId);
UpdateImgURL(list);//修改图片相对路径为物理路径,因为数据库存放的是图片的相对地址,只有获取到图片的物理地址才能找到图片。
数据库中存放的地址格式如下:
“~//UploadedImages//Examine//511505401632011.jpg”
this.ReportViewer1.LocalReport.EnableExternalImages = true;//设定EnableExternalImages 属性为TRUE,允许使用外部图片,默认是不允许的。
//实例化报表查看器的数据源
//在RDLC定义的数据集对象为"RegularRecord_phot",每个对象需要给配一个数据源
ReportDataSource rds = new ReportDataSource("RegularRecord_phot", list);
this.ReportViewer1.LocalReport.DataSources.Add(rds);
this.ReportViewer1.LocalReport.Refresh();
}
catch (Exception err)
{
Utility.WriteLog(Server.MapPath("~/"),err.ToString());
}
}
//修改图片相对路径为物理路径
protected void UpdateImgURL(List<Model.RecordRegularExamine_Photo> list) {
foreach (Model.RecordRegularExamine_Photo cplr in list)
{
if (cplr.adress != null && cplr.adress.Trim().Length > 0)
//将相对路径改为实际路径
cplr.adress = Server.MapPath(cplr.adress);
}
}。