我创建了一个 RDLC 报告,当我在网络浏览器中显示它时它工作正常。但是我的客户现在希望我将 PDF 文件发送到浏览器/客户端,而不是在 Web 浏览器中显示报告。当我添加代码以创建报表的 PDF 并将其发送到客户端/浏览器时,我遇到了错误。这是我将 PDF 发送到客户端/浏览器的代码:
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetData"
TypeName="CourrierExpress.ExpressCourierDataSetTableAdapters.uspFrontEnd_Invoice_PrintInvoiceTableAdapter"
OldValuesParameterFormatString="original_{0}">
<SelectParameters>
<asp:QueryStringParameter Name="InvoiceId" QueryStringField="InvoiceId" Type="Int64"
DefaultValue="" />
</SelectParameters>
</asp:ObjectDataSource>
我在页面加载后调用此代码:
Int64 invoiceId = int.Parse(Request.QueryString["InvoiceId"]);
Warning[] warnings;
string[] streamIds;
string mimeType;
string encoding;
string extension;
var rds = new ReportDataSource { DataSourceId = "ObjectDataSource1", Name = "DataSet1" };
var viewer = new ReportViewer();
viewer.ProcessingMode = ProcessingMode.Local;
viewer.LocalReport.DataSources.Clear();
viewer.LocalReport.ReportPath = "Reports/ReportInvoicePrint.rdlc"; //This is your rdlc name.
viewer.LocalReport.DataSources.Add(rds);
var bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension,
out streamIds, out warnings);
Response.Buffer = true;
Response.Clear();
Response.ContentType = mimeType;
Response.AddHeader("content-disposition", "attachment; filename= filename" + "." + extension);
Response.OutputStream.Write(bytes, 0, bytes.Length); // create the file
Response.Flush(); // send it to the client to download
Response.End();
RDLC 报告具有名为“DataSet1”的数据集。错误截图是: