gpt4 book ai didi

c# - mvc3 应用程序中的 RDLC 报告

转载 作者:太空狗 更新时间:2023-10-30 00:34:36 25 4
gpt4 key购买 nike

需要在我的 MVC3 应用程序中生成报告,我如何在 mvc3 中使用 RDLC。请任何人给我一个示例性的解释和指南,以便在我的 MVC3 应用程序中创建 RDLC 报告。

谢谢

最佳答案

我最近在 MVC3 应用程序中使用 RDLC 报告将结果导出到 Excel 电子表格中。

    private class ExcelReport 
{
private string encoding;
private string[] streams;
private Warning[] warnings;
private string fileNameExtension;
private string mimeType;
public ExcelReport()
{
this.ReportDataSources = new List<ReportDataSource>();
}
public string ExportFileName { get; set; }
public string MimeType
{
get
{
return mimeType;
}
private set
{
mimeType = value;
}
}
public string Encoding
{
get
{
return encoding;
}
private set
{
encoding = value;
}
}
public string FileNameExtension
{
get
{
return fileNameExtension;
}
private set
{
fileNameExtension = value;
}
}
public string[] Streams
{
get
{
return streams;
}
private set
{
streams = value;
}
}
public Warning[] Warnings
{
get
{
return warnings;
}
private set
{
warnings = value;
}
}
public string ReportPath { get; set; }

public IList<ReportDataSource> ReportDataSources { get; set; }
public byte[] GetReport()
{

LocalReport localReport = new LocalReport();
localReport.ReportPath = this.ReportPath;

foreach (var source in this.ReportDataSources)
{
localReport.DataSources.Add(source);
}

string reportType = "Excel";

//The DeviceInfo settings should be changed based on the reportType
//http://msdn2.microsoft.com/en-us/library/ms155397.aspx
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>Excel</OutputFormat>" +
" <PageWidth>21cm</PageWidth>" +
" <PageHeight>29cm</PageHeight>" +
" <MarginTop>1cm</MarginTop>" +
" <MarginLeft>2cm</MarginLeft>" +
" <MarginRight>2cm</MarginRight>" +
" <MarginBottom>1cm</MarginBottom>" +
"</DeviceInfo>";


//Render the report
return localReport.Render(
reportType,
deviceInfo,
out mimeType,
out encoding,
out fileNameExtension,
out streams,
out warnings
);
}
}

Controller

    public ActionResult GetReport(string reportParameter1) 
{
/*Get data for your report that matches the dataset format in the report.*/
IEnumerable<ReportData> list = new IEnumerable<ReportData> /*Your Report Data*/
{
new ReportData{Id = 1, Code="ABC"},
new ReportData{Id = 2, Code="DEF"}

};
var excelReport = new ExcelReport
{
ExportFileName = "Your File Name",
ReportPath = Server.MapPath("~/Content/Reports/YourReport.rdlc")

};
var ds = new ReportDataSource("Main", list); /* Main is the name of the dataset inside the report*/
excelReport.ReportDataSources.Add(ds);

var report = excelReport.GetReport();

Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.{1}", excelReport.ExportFileName, excelReport.FileNameExtension));
Response.ContentType = "application/vnd.ms-excel";

return File(report, excelReport.MimeType);
}

最终结果应该是您将报告导出为 excel 文档。

关于c# - mvc3 应用程序中的 RDLC 报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7156737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com