我引用了本网站中的一些文章,使用控制台应用程序将 .rdlc 渲染为 .pdf 输出。我是 C# 的新手。net 构建了一个与下面相同的应用程序,给出了一个错误: :>Rdclrender.exe!Rdclrender.Program.Main(string[] args = {string[0]}) 第 28 行 我的类(class)如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Reporting.WinForms;
namespace Rdclrender
{
class Program
{
static void Main(string[] args)
{
// Variables
Warning[] warnings;
string[] streamIds;
string mimeType = string.Empty;
string encoding = string.Empty;
string extension = string.Empty;
// Setup the report viewer object and get the array of bytes
ReportViewer viewer = new ReportViewer();
viewer.ProcessingMode = ProcessingMode.Local;
viewer.LocalReport.ReportPath = "Report.rdlc";
byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
using (System.IO.FileStream fs = new System.IO.FileStream("output.pdf", System.IO.FileMode.Create))
{
fs.Write(bytes, 0, bytes.Length);
}
// Now that you have all the bytes representing the PDF report, buffer it and send it to the client.
/* Response.Buffer = true;
Response.Clear();
Response.ContentType = mimeType;
Response.AddHeader("content-disposition", "attachment; filename=" + fileName + "." + extension);
Response.BinaryWrite(bytes); // create the file
Response.Flush(); // send it to the client to download*/
}
}
}
这是从 .rdl 创建 pdf 的方法吗?我已将 .rdl 手动重命名为 .rdlc,并将 .rdlc 项目添加到项目中。
最佳答案
可以以编程方式完成,最简单的解决方案是:
使用报表数据填充数据表,并将数据表命名为“Sales”(如报表中的数据源名称。
请注意,这是伪代码,无法正常工作,但应该可以给您一个想法。
var myDataSet = new DataSet();
var da = new SqlDataAdapter("Select * from Sales", "yourConnectionstring");
da.Fill(myDataSet);
DataTable table = myDataSet.Tables[0];
table.TableName = "Sales";
将数据表作为数据源添加到您的报告中:
viewer.LocalReport.DataSources.Add(table);
关于c# - 将 RDLC 渲染为 pdf 输出控制台应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19373932/