如何在报表查看器中加载rdlc文件,将参数传递给本地报表 [英] How to load a rdlc file in report viewer passing paramerter to local report
问题描述
我通过将数据集和参数传递给RDLC报告来准备报告。现在我可以将它发送到打印机进行打印。就像我想将它发送到一个RDLC报告viwer预览。我不想将数据集传递给报告viwer。请查看我在下面的代码中评论的行。任何人都可以提供帮助吗?
1.我想从这行删除hardcoe-
report.ReportPath = @F:\Projects \ SaleSlip.rdlc
//
I prepared a report by passing dataset and parameters to a RDLC report. Now I can send it to printer for printing. As like I want to send it to A RDLC report viwer to preview. I don not want to pass dataset to report viwer. Please look into the line I commented in the code bellow. Anyone can help?
1. I want to remove hardcoe from this line-
report.ReportPath = @"F:\Projects\SaleSlip.rdlc"
//
How will it work after deploying my project to other drvie?
2.我想以这种方式加载报告。
reportViewer.LocalReport = report; //我想将报告加载到reportviwer。
2. I want to load a report this way.
reportViewer.LocalReport = report;//I want to load report to reportviwer.
But do not know whether it is possible or not.
我有什么试过:
report = new LocalReport();
report.ReportPath = @F:\Projects \ SaleSlip.rdlc
report.DataSources.Add(new ReportDataSource(SaleSlipData,dtAddItems));
report.DataSources.Add(new ReportDataSource(SaleSlipPaymentData, dtPaymentCollection));
开关(strOption)
{
case 打印:
SendToPrinter(报告);
休息;
案例预览:
SaleSlipViewer frmSaleSlipViwer = new SaleSlipViewer();
ReportViewer reportViewer = frmSaleSlipViwer.rpvSaleSlipVeiwer;
reportViewer.Clear();
reportViewer.LocalReport = report; //我想将报告加载到reportviwer。但是怎么样?
reportViewer.Refresh();
休息;
What I have tried:
report = new LocalReport();
report.ReportPath = @"F:\Projects\SaleSlip.rdlc"
report.DataSources.Add(new ReportDataSource("SaleSlipData", dtAddItems));
report.DataSources.Add(new ReportDataSource("SaleSlipPaymentData", dtPaymentCollection));
switch (strOption)
{
case "Print":
SendToPrinter(report);
break;
case "Preview":
SaleSlipViewer frmSaleSlipViwer = new SaleSlipViewer();
ReportViewer reportViewer = frmSaleSlipViwer.rpvSaleSlipVeiwer;
reportViewer.Clear();
reportViewer.LocalReport = report;//I want to load report to reportviwer. But how?
reportViewer.Refresh();
break;
案例ExporttoPDF:
exportSaleSlipToPDF();
休息;
}
case "ExporttoPDF":
exportSaleSlipToPDF();
break;
}
推荐答案
1。我想从这行中删除hardcoe-
report.ReportPath = @F:\Projects\SaleSlip.rdlc
部署项目后如何工作到其他drvie?
您可以将报告文件复制到应用程序路径。
您可以在构建项目时实现此目的:
a。定位报告文件的复制到输出目录属性。
b。将其更改为始终复制。
然后,使用相对网址:
1. I want to remove hardcoe from this line-
report.ReportPath = @"F:\Projects\SaleSlip.rdlc"
How will it work after deploying my project to other drvie?
You could copy your report files to the application path.
You can achieve this when you build your project:
a. Locale the "Copy to Output Directory" property of the report file.
b. Change it to "Copy always".
Then, use a relative url:
report.ReportPath = "reportName.rdlc";
2.我想以这种方式加载报告。
reportViewer.LocalReport = report; //我想将报告加载到reportviwer
ReportViewer的LocalReport属性是只读的。 />
你可以这样做:
2. I want to load a report this way.
reportViewer.LocalReport = report;//I want to load report to reportviwer
The LocalReport property of the ReportViewer is readonly.
You could do this:
// report = new LocalReport();
SaleSlipViewer frmSaleSlipViwer = new SaleSlipViewer();
ReportViewer reportViewer = frmSaleSlipViwer.rpvSaleSlipVeiwer;
LocalReport report = reportViewer.LocalReport;
report.ReportPath = @"F:\Projects\SaleSlip.rdlc"
report.DataSources.Add(new ReportDataSource("SaleSlipData", dtAddItems));
report.DataSources.Add(new ReportDataSource("SaleSlipPaymentData", dtPaymentCollection));
switch (strOption)
{
case "Print":
SendToPrinter(report);
break;
case "Preview":
// SaleSlipViewer frmSaleSlipViwer = new SaleSlipViewer();
// ReportViewer reportViewer = frmSaleSlipViwer.rpvSaleSlipVeiwer;
reportViewer.Clear();
// reportViewer.LocalReport = report;//I want to load report to reportviwer. But how?
// reportViewer.Refresh();
reportViewer.RefreshReport();
break;
您可以创建一个准备报告的方法,另一个生成pdf或excel等文件的方法。
You could create a method for prepare the report, and another method for generate files like pdf or excel.
// Prepare the report
private void prepareReport(LocalReport report, string reportFilePath, DataTable data)
{
report.ReportPath = reportFilePath;
report.DataSources.Add(new ReportDataSource("SaleSlipData", data));
}
// Generate a file
private void GenerateFile(string fileType, LocalReport report, string savePath)
{
// The FileStream class is in the System.IO namespace.
/*
* The savePath include the file name with the proper file extension.
* If file is a pdf -> filename.pdf
*/
byte[] bytes = report.Render(fileType);
FileStream fs = new FileStream(savePath, FileMode.Create);
fs.Write(bytes, 0, bytes.Length);
fs.Close();
}
// Here the code for a button.
private void btnButton1_Click(object sender, EventArgs e)
{
SaleSlipViewer frmSaleSlipViwer = new SaleSlipViewer();
ReportViewer reportViewer = frmSaleSlipViwer.rpvSaleSlipVeiwer;
/*
* The processing mode of a report viewer is Local by default,
* this line is not necessary.
*/
reportViewer.ProcessingMode = ProcessingMode.Local;
prepareReport(reportViewer.LocalReport, "SaleSlip.rdlc", dtAddItems);
switch (strOption)
{
case "Preview":
reportViewer.RefreshReport();
break;
case "ExporttoPDF":
GenerateFile("PDF", reportViewer.LocalReport, "Reports/report.pdf");
break;
case "ExporttoExcel":
GenerateFile("Excel", reportViewer.LocalReport, "Reports/report.xls");
break;
// Add more cases as needed.
}
}
LocalReport Render方法支持文件类型为PDF,Excel,Word和图像。
有关详细信息,请参阅文档(此处)
这篇关于如何在报表查看器中加载rdlc文件,将参数传递给本地报表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!