【发布时间】:2016-11-10 08:21:24
【问题描述】:
我正在做 mvc 报告,我对它很陌生。我正在尝试创建一个报告,并且我已经使用 rdlc 完成了它。一切正常,可以导出为各种格式。我的问题是,在使用rdlc的时候,我们需要先设计和绑定。如何创建一个空的 rdlc 模板,以编程方式设计并将其与数据集绑定。
到目前为止我的工作(使用 empty rdlc 模板 - 刚刚创建了没有任何表格的文件),
控制器文件,
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 调用的数据填充它。
高级 TQVM。解释和示例或任何其他方法都会有所帮助。
【问题讨论】:
-
您要解决什么真正的问题?当您甚至无法查看报表时,您就无法实际设计报表,这就是报表设计师很重要的原因。不过,您可以将其绑定到不同的数据源。您是否尝试在运行时更改列?
-
另一方面,RDLC 只是 XML。您可以使用与任何其他 XML 文件相同的方式生成其内容,例如在现有 XML 模板文件上使用 XSLT 转换,使用 Linq to XML 执行相同操作等。最好在 build-time 执行此操作不过,为了确保结果看起来不错。正确获取颜色、宽度和边距可能会很烦人
-
@PanagiotisKanavos 抱歉,如果我的问题不清楚。我想象它的工作方式是我创建一个空的 rdlc 文件,从数据库中读取数据并根据收到的数据创建列。是的,正如您所说,我试图在运行时更改列,因为如果要创建各种不同的报告,则根据收到的数据库表创建单独的 rdlc 文件效率低下。
-
我完全忘记了 Report 组件是完全可编程的。您可以在运行时创建它,就像通过添加控件来创建表单一样。修改 XML 更容易但是您也可以使用构建脚本从模板报告生成多个报告
-
另一方面,您可以根据表达式(可以使用报告参数)隐藏单个报告中的列。您还可以在呈现之前从现有模板报告中删除列
标签: c# sql-server asp.net-mvc ado.net rdlc