首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

RDLC 報表實戰系列(一):從零建立第一個本地模式報表

     編輯:關於.NET

RDLC (Report Definition Language Client-side) 是 Visual Studio 內建的免費報表方案,無需 Reporting Services 伺服器即可在本地模式下產生專業報表。本系列開篇,手把手帶你建立第一個 RDLC 報表專案。

專案建立與基礎架構

1. 新建專案與資料夾結構

  • VS 2008+ → ASP.NET Web Application
  • 新增兩個資料夾:DataEntity(存放 DataSet)、ReportTemplate(存放 .rdlc 檔)

新建專案對話框

資料夾結構

2. 參考必要組件

專案右鍵 Add Reference → .NET 標籤 → 勾選:

  • Microsoft.Reporting.WinForms (核心報表引擎)
  • System.Windows.Forms (WinForms 相容性)

加入參考對話框

資料層:建立強型別 DataSet

3. 新增 DataSet (.xsd)

  • 右鍵 DataEntity → Add New Item → DataSet,命名 ReportDataSet.xsd
  • 從 Server Explorer 拖入資料表(或手動新增 DataTable/Columns)

新增 DataSet

從 Server Explorer 拖入資料表

DataSet 設計介面

報表層:設計 .rdlc 模板

4. 新增報表檔案

  • 右鍵 ReportTemplate → Add New Item → Report,命名 LogReport.rdlc

新增 Report 項目

報表設計介面

5. 設定報表資料源

  1. 選單 Report → Data Sources...
  2. 新增資料源,選擇 ReportDataSet 中的 DataTable(如 T_BC_LOGS)

報表資料源設定

選擇 DataTable

6. 版面設計與資料綁定

  • 工具箱拖入 Table 控制項(預設 3 行:標題、資料、頁尾)
  • Table 右鍵 Properties → DataSetName 選擇剛建立的資料集名稱
  • 資料列各欄位右鍵 Expression... → Categories: DataSets → 選擇欄位

Table DataSetName 設定

Expression 選擇欄位

欄位綁定完成

報表設計完成

程式碼層:報表產生與輸出

7. 建立基底頁面類別 CustomPageBase

public class CustomPageBase : Page {
    public void GetReportMultipleDataSourceFile(
        List<ReportDataSource> reportDataSources, 
        string templatePath, 
        List<ReportParameter> parameters, 
        string fileType) 
    {
        var viewer = new ReportViewer {
            ProcessingMode = ProcessingMode.Local,
            LocalReport = { ReportPath = Server.MapPath(templatePath) }
        };
        viewer.LocalReport.DataSources.Clear();
        foreach (var ds in reportDataSources)
            viewer.LocalReport.DataSources.Add(ds);

        if (parameters.Count > 0)
            viewer.LocalReport.SetParameters(parameters);

        string mimeType, encoding, extension, deviceInfo = @"True";
        string[] streamIds; Warning[] warnings;
        byte[] bytes = viewer.LocalReport.Render(fileType, deviceInfo, out mimeType, out encoding, out extension, out streamIds, out warnings);

        Response.Buffer = true; Response.Clear();
        Response.ContentType = mimeType;
        Response.AddHeader("Content-Disposition", $"attachment; filename=Report.{extension}");
        Response.BinaryWrite(bytes); Response.End();
    }
}

8. 頁面繼承並實作產生邏輯

public partial class _Default : CustomPageBase {
    protected void ButtonReportGenerate_Click(object sender, EventArgs e) {
        var reportDataSources = new List<ReportDataSource>();
        var ds = new ReportDataSet();
        string templatePath = "ReportTemplate/LogReport.rdlc";

        using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LoggingConnectionString"].ConnectionString)) {
            var cmd = conn.CreateCommand();
            cmd.CommandText = "SELECT * FROM T_BC_LOGS";
            new SqlDataAdapter(cmd).Fill(ds, "T_BC_LOGS");
        }

        // 關鍵:資料集名稱必須與報表設計時的 DataSetName 完全一致
        reportDataSources.Add(new ReportDataSource("ReportDataSet_T_BC_LOGS", ds.T_BC_LOGS));

        GetReportMultipleDataSourceFile(reportDataSources, templatePath, new List<ReportParameter>(), "pdf");
    }
}

執行結果驗證

瀏覽頁面,點擊按鈕,瀏覽器將下載產生的 PDF 報表。

報表產生下載效果

核心概念總結

層級檔案/類別職責
資料層DataEntity/ReportDataSet.xsd定義報表所需資料結構(DataTable、Columns、Relations)
模板層ReportTemplate/*.rdlc定義報表版面、樣式、資料綁定運算式、參數
程式層CustomPageBase + _Default填充資料、設定參數、呼叫 Render 輸出 PDF/Excel/Word

常見入門坑點提醒

  • 資料集名稱不符:程式碼 new ReportDataSource("Name", data) 的 Name 必須與 .rdlc 設計時的 DataSetName 完全一致(區分大小寫)。
  • 缺少參考組件:部署至 IIS 時需確保 Microsoft.ReportViewer.WebForms 組件已安裝或 Bin 部署。
  • 渲染格式參數:Render("pdf")、"excel"、"word"、"image" 等,對應不同輸出格式。
  • 本地模式 vs 伺服器模式:本文為 Local 模式(ProcessingMode.Local),無需 SSRS 伺服器。

完成第一個報表後,你已掌握 RDLC 核心開發流程:定義資料結構 → 設計模板綁定 → 程式填充渲染。後續將深入參數、圖片、子報表、運算式等進階主題。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved