You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在SSIS任务中基于本地RDLC报表与C# List<>生成PDF

在SSIS中使用本地RDLC与C# List<>生成PDF(无需SSRS)

完全可以实现,核心是通过SSIS脚本任务复用你在ASP.NET中掌握的RDLC渲染逻辑,同时拆分数据生成与报表渲染为两个独立步骤。以下是具体实现方案:

步骤1:生成并存储本地数据源

在第一个脚本任务中,生成List<>数据集,并将其存储到SSIS的Object类型变量中,供后续渲染步骤调用。

代码示例(C#脚本任务)

using System;
using System.Collections.Generic;
using Microsoft.SqlServer.Dts.Runtime;

namespace ST_GenerateDataSource
{
    public class ScriptMain : UserComponent
    {
        public override void Main()
        {
            // 定义数据模型(需与RDLC报表的数据源结构完全匹配)
            public class Order
            {
                public int OrderId { get; set; }
                public string CustomerName { get; set; }
                public decimal Amount { get; set; }
            }

            // 生成数据集(实际场景可从数据库/其他本地源获取)
            List<Order> orderList = new List<Order>
            {
                new Order { OrderId = 1, CustomerName = "张三", Amount = 1200.50m },
                new Order { OrderId = 2, CustomerName = "李四", Amount = 850.00m }
            };

            // 将数据集存入SSIS Object变量(需提前在包中定义Object类型变量)
            Dts.Variables["User::ReportDataSource"].Value = orderList;

            Dts.TaskResult = (int)DTSExecResult.Success;
        }
    }
}

步骤2:加载RDLC并渲染为PDF

在第二个脚本任务中,读取存储的List<>数据源,加载本地RDLC文件,完成PDF渲染并保存到指定路径。

准备工作

  1. 在脚本任务的引用中添加以下程序集:
    • Microsoft.ReportingServices.ReportViewerControl.WinForms
    • System.Drawing
    • System.IO
  2. 将RDLC文件放置在SSIS包可访问的路径(建议用SSIS变量存储路径,便于维护)

代码示例(C#脚本任务)

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Reporting.WinForms;
using Microsoft.SqlServer.Dts.Runtime;

namespace ST_RenderReportToPDF
{
    public class ScriptMain : UserComponent
    {
        public override void Main()
        {
            try
            {
                // 读取SSIS变量中的数据源
                List<Order> orderList = Dts.Variables["User::ReportDataSource"].Value as List<Order>;
                if (orderList == null || orderList.Count == 0)
                {
                    Dts.Events.FireError(0, "报表渲染", "数据源为空", string.Empty, 0);
                    Dts.TaskResult = (int)DTSExecResult.Failure;
                    return;
                }

                // 初始化WinForms版ReportViewer(SSIS环境需用WinForms版本)
                ReportViewer reportViewer = new ReportViewer();
                reportViewer.ProcessingMode = ProcessingMode.Local;

                // 加载本地RDLC文件(路径通过SSIS变量传入)
                string rdlcPath = Dts.Variables["User::RDLCFilePath"].Value.ToString();
                reportViewer.LocalReport.ReportPath = rdlcPath;

                // 设置报表数据源(名称需与RDLC中定义的数据源名称完全一致)
                ReportDataSource dataSource = new ReportDataSource("OrderDataSet", orderList);
                reportViewer.LocalReport.DataSources.Clear();
                reportViewer.LocalReport.DataSources.Add(dataSource);

                // 渲染为PDF并保存到指定路径
                string outputPath = Dts.Variables["User::PDFOutputPath"].Value.ToString();
                byte[] pdfBytes = reportViewer.LocalReport.Render(
                    format: "PDF",
                    deviceInfo: null,
                    mimeType: out _,
                    encoding: out _,
                    fileNameExtension: out _,
                    streams: out _,
                    warnings: out _
                );

                File.WriteAllBytes(outputPath, pdfBytes);

                Dts.TaskResult = (int)DTSExecResult.Success;
            }
            catch (Exception ex)
            {
                Dts.Events.FireError(0, "报表渲染失败", ex.Message, string.Empty, 0);
                Dts.TaskResult = (int)DTSExecResult.Failure;
            }
        }

        // 需重复定义数据模型(或提取为共享类库供两个脚本任务引用)
        public class Order
        {
            public int OrderId { get; set; }
            public string CustomerName { get; set; }
            public decimal Amount { get; set; }
        }
    }
}

关键注意事项

  • 数据模型一致性:RDLC报表的数据源结构必须与C#实体类完全匹配,包括字段名、数据类型。
  • RDLC路径问题:确保SSIS运行时能访问到RDLC文件,建议使用绝对路径或结合Dts.Variables["System::PackageDirectory"]获取包所在目录拼接路径。
  • 程序集依赖:若运行时报错找不到ReportViewer相关组件,需将对应的DLL(如Microsoft.ReportViewer.WinForms.dll)复制到SSIS运行目录(通常为C:\Program Files\Microsoft SQL Server\XXXXX\DTS\Binn),或在GAC中注册。
  • 变量权限:脚本任务需设置为可读写对应的SSIS变量(在脚本任务编辑器的「ReadOnlyVariables」/「ReadWriteVariables」中添加)。

内容的提问来源于stack exchange,提问作者Clinemi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 08:17:41