1.VS2017 工具>扩展和更新
2.选择联机,在最右侧输入RDLC,选择第一个进行安装
3.在项目下面建一个存放报表模板的文件夹
4.在文件夹上右键添加>报表,根据自己的情况取个名儿,然后就进入到报表模板设计界面了
5.左侧工具箱根据情况自由选择模板控件就好,或者右键>插入,进行选择
插入一张表
6.绑定字段说明,需要先添加相应的数据集与数据源,数据集上右键添加(方式一)
如果没有数据链接,进行新建即可
继续下一步即可
根据自己的情况选择,点完成
然后就是数据集属性,我把他当成这个报表的数据源的名称,之后查询出数据注入数据源的时候有用,别的没啥用,数据源默认选择你刚刚新建
数据源和数据集都有了
对表里的字段进行绑定即可
以上方式感觉单表的时候使用还行,但是多表关联啥的就不行了,就需要自定义查询表结构了
7.自定义查询表结构(方式二)
新建一个存放数据集的文件夹,右键添加新建项,选择数据集
右键添加,选择TableAdapter
根据提示下一步
选第一个
输入自定义的SQL查询就好了,这里貌似只需要表结构,能进行绑定就好,所以不需要查出数据,需要的数据后台查好了直接给他就行了
下一步
完成
得到自定义查询结果表结构
再回到模板添加数据集的地方,右键添加数据集,这里数据源就可以下拉选择刚刚创建的
数据集也能选到刚刚创建的(这些名字自己随意定义,我懒就默认了),右侧就显示查询出来的表字段了
进行字段绑定就OK啦
8.下面这个是我随便搞的模板,到这儿模板这方面就算完成了(模板的设计啥的就不讲了,度娘一大堆)
9.代码这一块的编写,简单,就下面这玩意儿,自己代码查询出数据后,丢给报表就可以了,注意表结构要和绑定的的字段一样哦,数据集(总喜欢叫数据源)的名字不能错哦
public FileResult ExportExcelprint()
{
string ProjectID = "9", searchStatus = "3";
TouSuBaoXiuDAL tsd = new TouSuBaoXiuDAL();
string sqlwhere = "and a.id<470";
if (!string.IsNullOrEmpty(ProjectID) && Convert.ToInt32(ProjectID) > 0)
{
sqlwhere += " and a.ProjectID=" + ProjectID;
}
if (!string.IsNullOrEmpty(searchStatus))
{
sqlwhere += " and a.status=" + searchStatus;
}
//添加数据权限
string roleWhere = returnDateRole();
if (roleWhere != "")
{
sqlwhere += roleWhere;
}
List<TouSuBaoXiu> returnModels = tsd.List(sqlwhere);
//FYSQDal fd = new FYSQDal();
//List<PayOrderEntity> lpoe = fd.GetPayOrderData("", " and a.ID<1300");
LocalReport localReport = new LocalReport();
localReport.ReportPath = Server.MapPath("../Report/TouSuBaoXiu.rdlc");
ReportDataSource reportDataSource = new ReportDataSource("DataSet1", returnModels);
//ReportDataSource reportDataSource1 = new ReportDataSource("DataSet2", dt1);
localReport.DataSources.Add(reportDataSource);
//localReport.DataSources.Add(reportDataSource1);
//string reportType = "Word";
string reportType = "PDF";
string mimeType;
string encoding;
string fileNameExtension;
Warning[] warnings;
string[] streams;
byte[] renderedBytes;
StringBuilder deviceInfo = new StringBuilder();
deviceInfo.AppendLine("<DeviceInfo>");
deviceInfo.AppendLine("<OutputFormat>PDF</OutputFormat>");
deviceInfo.AppendLine("<PageWidth>29.7cm</PageWidth>");
deviceInfo.AppendFormat("<PageHeight>{0}cm</PageHeight>", 21);
deviceInfo.AppendLine("<MarginTop>0.5cm</MarginTop>");
deviceInfo.AppendLine("<MarginLeft>0.5cm</MarginLeft>");
deviceInfo.AppendLine("<MarginRight>0.5cm</MarginRight>");
deviceInfo.AppendLine("<MarginBottom>0.5cm</MarginBottom>");
deviceInfo.AppendLine("</DeviceInfo>");
//Render the report
renderedBytes = localReport.Render(
reportType,
deviceInfo.ToString(),
out mimeType,
out encoding,
out fileNameExtension,
out streams,
out warnings);
//string path = Server.MapPath(@"\a.pdf");
//FileStream fs = new FileStream(path, FileMode.Create);
//fs.Write(renderedBytes, 0, renderedBytes.Length);
//fs.Dispose();
return File(renderedBytes, mimeType);
}
10.前端调用即可,so easy,这是MVC上的用法,其他也差不多
@Html.ActionLink("打印", "ExportExcelprint")
11.用的时候需要引用一下Microsoft.ReportViewer.WebForms.dll,不然报错
12.上面这种方式可应用各种浏览器,不必须是IE了,之前尝试的方式必须是IE,头大啊
试试这个打印
效果还行,这算是预览了,右上角再打印就好了