2

我已经在本网站上引用了一些文章,使用控制台应用程序将 .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 重命名为 .rdlc ,并将 .rdlc 项目添加到项目中。

4

1 回答 1

3

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

用报表的数据填充 DataTable,并将 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);
于 2013-10-15T11:40:06.830 回答