C# 以编程方式生成和设计Rdlc文件
我正在做mvc报告,我对它非常陌生。我正在尝试创建一个报告,我已经使用rdlc完成了。一切都很好,它可以导出到各种格式。我的问题是,当使用rdlc时,我们首先需要设计和绑定它。我如何创建一个空的rdlc模板,以编程方式设计并将其与dataset绑定 到目前为止,我的工作(使用空rdlc模板-刚刚创建了没有任何表的文件) 控制器文件,C# 以编程方式生成和设计Rdlc文件,c#,sql-server,asp.net-mvc,ado.net,rdlc,C#,Sql Server,Asp.net Mvc,Ado.net,Rdlc,我正在做mvc报告,我对它非常陌生。我正在尝试创建一个报告,我已经使用rdlc完成了。一切都很好,它可以导出到各种格式。我的问题是,当使用rdlc时,我们首先需要设计和绑定它。我如何创建一个空的rdlc模板,以编程方式设计并将其与dataset绑定 到目前为止,我的工作(使用空rdlc模板-刚刚创建了没有任何表的文件) 控制器文件, public ActionResult Report(string id) { DB.Open(); LocalReport
public ActionResult Report(string id)
{
DB.Open();
LocalReport lr1 = new LocalReport();
string path1 = Path.Combine(Server.MapPath("~/Report"), "TestEmptyReport.rdlc");
lr1.ReportPath = path1;
DataTable pc2a = new DataTable();
pc2a = DB.getDataSet().Tables[0];
pc2a.Columns.Add("Product Name");
pc2a.Columns.Add("Price");
pc2a.Columns.Add("Quantity");
ReportDataSource rdc = new ReportDataSource("DataSet1", pc2a);
lr1.DataSources.Add(rdc);
string reportType = id;
string mimeType;
string encoding;
string fileNameExtension;
string deviceInfo =
"<DeviceInfo>" +
"<OutputFormat>" + id + "</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;
renderedBytes = lr1.Render(
reportType,
deviceInfo,
out mimeType,
out encoding,
out fileNameExtension,
out streams,
out warnings);
return File(renderedBytes, mimeType);
}
查看文件,
public DataSet getDataSet()
{
string query = "SELECT * FROM tblproduct";
if (con.State.ToString() == "Open")
{
SqlDataAdapter ad = new SqlDataAdapter(query, con);
DataSet ds = new DataSet("tblproduct");
ad.Fill(ds);
return ds;
}
else
{
return null;
}
}
<div style="padding: 10px; border: 1px solid black">
<div><a href="@Url.Action("Report", new { id = "PDF" })">Get PDF Report</a></div>
<div><a href="@Url.Action("Report", new { id = "Excel" })">Get Excel Report</a></div>
<div><a href="@Url.Action("Report", new { id = "Word" })">Get Word Report</a></div>
<div><a href="@Url.Action("Report", new { id = "Image" })">Get Image Report</a></div>
数据在那里,但我不知道如何将其与rdlc连接。意味着基于数据创建列,并使用从sql server调用的数据填充该列
先进的TQM。解释和示例或任何其他方法都会有帮助。如果我正确理解您的问题,您希望从空白RDLC创建报告。您必须在设计时将数据告知RDLC文件。您可以在设计时通过添加列或其他表中的列或进行联接来自定义报表
然而,它可以从RDLC动态生成报告。由于完整的ReportingEngine是定制的,但非常通用。复制粘贴可能有助于生成报告。您的问题意味着您需要在运行时模式下生成RDLC报告。您应该记住的事项:
Microsoft.ReportViewer.WebForms
和Microsoft.Reporting.WebForms
名称空间,它们利用WebForms逻辑代码绑定和呈现报表。您可以使用充当ASPX页面容器的部分视图(使用单个页面或带有代码隐藏的页面)在MVC视图页面中呈现报告
注意:您可以在控制器操作方法中使用ReportViewer
实例将RDLC呈现为PDF文件并返回FileContentResult
(请参阅)using Microsoft.Reporting.WebForms;
protected void Form_Load(Object sender, EventArgs e)
{
if (!IsPostBack)
{
ReportViewer1.ProcessingMode = ProcessingMode.Local;
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");
// add your DataSet here
var data = new DataTable(); // example data source
var dataSource = new ReportDataSource(DataSetName, data);
ReportViewer1.LocalReport.DataSources.Add(dataSource);
// put rendering stuff here
}
}
然后,使用示例中的步骤(WebForms在事件处理使用方面类似于WinForms,因此可能适用于ASPX页面)创建相应的XML标记,生成报告结构,总结如下
数据集
,以便在数据源
元素中使用数据集中的表结构创建Tablix
和TablixCorner
元素
TablixColumn
、TablixRow
和TablixCell
元素,这些元素包括Textbox
控件,具体取决于每个列的数据类型。文本框
控件中的值
元素应包含表达式=字段!(ColumnName).Value
用于数据库绑定列LoadReportDefinition
添加到ASPX页面XmlWriter
类可以生成从头开始构建报告所需的元素,然后将生成的XML重命名为RDLC扩展,并将其绑定到ReportViewer
控件(构建每个RDLC元素似乎需要一些时间)
其他参考资料:
您想解决的真正问题是什么?当你连看都看不到的时候,你就不能真实地设计一个报告,这就是为什么报告设计者很重要的原因。不过,您可以将其绑定到不同的数据源。您是否试图在运行时更改列?另一方面,RDLC只是XML。您可以以与任何其他XML文件相同的方式生成其内容,例如对现有XML模板文件使用XSLT转换,使用Linq到XML进行相同的转换等。不过最好在构建时这样做,以确保结果看起来正常。获得正确的颜色、宽度和边距可能相当困难annoying@PanagiotisKanavos对不起,我的问题不清楚。我想象它是如何工作的,我创建了一个空的rdlc文件,从数据库中读取数据,并根据接收到的数据创建列。是的,正如你所说,我试图在运行时更改列,因为如果要创建各种不同的报表,那么根据接收到的数据库表创建单独的rdlc文件效率很低。我完全忘记了报表组件是完全可编程的。您可以在运行时创建它,就像通过添加控件创建表单一样。修改XML更容易,但也可以使用构建脚本从模板报表生成多个报表。另一方面,可以基于表达式(可以使用报表参数)隐藏单个报表上的列。您还可以在呈现之前从现有模板报表中删除列。您可以链接到一个示例,该示例显示可以使用代码从头开始生成!也可以直接渲染为PDF。无需使用iTextSharp,它比报表引擎复杂得多。实际答案在链接文章中。这种解释令人困惑。本质上它说“链接有答案,去拷贝”。这并不比一个只有链接的答案好多少