将RDLC渲染为pdf输出控制台应用程序

时间:2013-10-15 05:31:56

标签: c# reporting-services console-application rdlc rdl

我已经在本网站上引用了一些文章,用于使用控制台应用程序将.rdlc渲染到.pdf输出。对C#.net新建一个应用程序,如下所示给出错误说法 :> Rdclrender.exe!Rdclrender.Program.Main(string [] args = {string [0]})第28行 我的课程如下:

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重命名为.rdl,将一个已添加的.rdlc项目重命名为项目。

1 个答案:

答案 0 :(得分:3)

好的,以编程方式完成最简单的解决方案是:

使用报告数据填充DataTable,并将数据表命名为“Sales”(如报告中的DataSource名称。

请注意这是伪代码,它不起作用,但应该给你一个想法。

var myDataSet = new DataSet(); 
var da = new SqlDataAdapter("Select * from Sales", "yourConnectionstring");

da.Fill(myDataSet);
DataTable table = myDataSet.Tables[0];
table.TableName = "Sales";

将DataTable作为数据源添加到您的报告中:

viewer.LocalReport.DataSources.Add(table);