顯示具有 Reporting 標籤的文章。 顯示所有文章
顯示具有 Reporting 標籤的文章。 顯示所有文章

2024年3月4日 星期一

[筆記]RDLC TO PDF

透過reportview轉byte[]
byte[] bytes = reportViewer.LocalReport.Render( "PDF", null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings); 最後輸出成PDF
  • 參考:
    • https://stackoverflow.com/questions/2684221/creating-a-pdf-from-a-rdlc-report-in-the-background

2013年6月26日 星期三

LocalReport - 使用子報表

專案架構如下,專案下產生兩個報表(父報表與子報表)。

image

新增TypedDataSet(Ex:dsReport.xsd),範例定義如下。

image

在myReport.rdlc報表中,設定資料集屬性。

SNAGHTMLf854e7

在myReport.rdlc報表中,新增子報表。

image

在myReport.rdlc報表中,設定[子報表屬性]。

SNAGHTML1016ff6

設定要傳給子報表的參數。

SNAGHTML1022bc6

在mySubReport.rdlc報表中,設定資料集屬性。

SNAGHTML1041846

在mySubReport.rdlc報表中,設定參數,參數名稱要和[子報表參數]設定一致(EX:OrderID)。

image

SNAGHTML1074638

程式部分:

打包:下載

2013年6月18日 星期二

NPOI與RDLC衝突

VS2010在寫報表時,因為有同時使用NPOI。

結果在編輯報表時,加入[資料表]時,整個專案就關掉了。

錯誤是發生在選擇資料集(加入資料表會去選資料集)的時候。

查了一下Google發現有相同的問題

簡易解法:

  • NPOI某版本與RDLC互相衝突,換新版本就會好。
  • 或是Report獨立另外一個專案。

2013年1月10日 星期四

如何使用LocalReport - WebForm (3)


實作部分,報表繫結與報表下載

/// <summary>
        /// 報表繫結
        /// </summary>
        /// <param name="rpt">報表檢視器</param>
        /// <param name="dsName">資料集名稱</param>
        /// <param name="RptPath">報表路徑</param>
        /// <param name="dt">繫結之資料表</param>
        public static void ReportBind(
            ReportViewer rpt, 
            string dsName, 
            string RptPath, 
            DataTable dt)
        {
            //Report存放路徑,EX:~/bin/Report
            string Path = ConfigurationManager.AppSettings["RptPath"].ToString();
            rpt.LocalReport.DataSources.Clear();
            rpt.ProcessingMode = ProcessingMode.Local;
            rpt.LocalReport.ReportPath = 
                HttpContext.Current.Server.MapPath(
                string.Format("{0}\\{1}", Path, RptPath));
            rpt.LocalReport.DataSources.Add(new ReportDataSource(dsName, dt));
            rpt.LocalReport.Refresh();

        }
        /// <summary>
        /// 報表繫結
        /// </summary>
        /// <param name="rpt">報表檢視器</param>
        /// <param name="dsName">資料集名稱</param>
        /// <param name="RptPath">報表路徑</param>
        /// <param name="dt">繫結之資料表</param>
        /// <param name="PType">PDF,Excel,Word,Image</param>
        public static void ReportExport(
            ReportViewer rpt,
            string dsName,
            string RptPath,
            DataTable dt,
            string PType)
        {
            ReportBind(rpt, dsName, RptPath, dt);
            //
            Microsoft.Reporting.WebForms.Warning[] tWarnings;
            string[] tStreamids;
            string tMimeType;
            string tEncoding;
            string tExtension;
            //呼叫ReportViewer.LoadReport的Render function,將資料轉成想要轉換的格式,並產生成Byte資料
            byte[] tBytes =
                rpt.LocalReport.Render(
                PType, null, out tMimeType,
                out tEncoding, out tExtension,
                out tStreamids, out tWarnings);
            //將Byte內容寫到Client
            string DisplayName =
                rpt.LocalReport.DisplayName == "" ?
                "report" : rpt.LocalReport.DisplayName;
            if (HttpContext.Current.Request.Browser.Browser == "IE")
            {
                DisplayName =
                    HttpContext.Current.Server.UrlPathEncode(
                    DisplayName);
            }
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = tMimeType;
            HttpContext.Current.Response.
                AppendHeader("Content-Disposition",
                String.Format("attachment; filename={0}.{1}",
                DisplayName, tExtension));
            HttpContext.Current.Response.BinaryWrite(tBytes);
            HttpContext.Current.Response.End();
        }

建置或發行時需要注意的設定:
  • 報表相關的DLL需一設定複製到本機為True
  • 報表專案的報表檔,需要也需要設定為複製
image
Web.config設定
  • 報表繫結程式中,取得報表檔的方法會參考此路徑,因為報表專案在發行後會將報表檔案(RDLC)複製到bin目錄下。
  • 在此我們可以將此發行後的檔案隨意搬移,如將檔案移到Report的資料夾,就可以自行定義。
image
顯示報表與下載程式碼如下:

protected void btnShowReport_Click(object sender, EventArgs e)
{
UtilityUI.ReportBind(
this.ReportViewer1
,"TB1"
,"rpt110.rdlc"
, GetReportData()
);
}

protected void btnDownload_Click(object sender, EventArgs e)
{
this.ReportViewer1.LocalReport.DisplayName = "訂單明細";
UtilityUI.ReportExport(
this.ReportViewer1
, "TB1"
, "rpt110.rdlc"
, GetReportData()
,"PDF"
);
}


結果畫面如下:
image

如何使用LocalReport - WebForm (2)

開始建立報表,在Report專案中,加入新項目,選擇 Reporting > 報表

SNAGHTMLc65a43

加入TypeDataSet(資料集),選擇 資料 > 資料集

SNAGHTMLc8be43

在資料集編輯畫面,可以透過伺服器總管新增資料庫table或是自訂table,以下範例為自訂

image

    • 在畫面上按右鍵 加入 DataTable,命名為TB

image

在TB中按右鍵,加入資料行,以下使用者就可自行定義。

image

接著就可以開始進行報表設計

image

在報表編輯畫面上面,使用資料表元件,之後會要求使用者選擇資料集。在此選擇剛剛建立的資料集

  • 名稱:自訂資料集名稱
    • 註:這邊對應dsName
  • 資料來源:建立好的dsReport
  • 可用資料集:TB;dsReport中的DataTable

SNAGHTML11ad78f

設定完成之後畫面如下,資料列即可選到要顯示的欄位

image

2013年1月9日 星期三

如何使用LocalReport - WebForm (1)

 

建立專案架構如下:

image

  • Web參考Data與Report兩個專案

image

  • 並參考DLL層下面的一顆DLL:Microsoft.ReportViewer.ProcessingObjectModel.dll

image

image

  • 另外Web加入參考

image

參考完成後,Web層的參考如下

image

以上設定WinForm雷同,不同處在參考Microsofrt.ReportViewer.WinForms。

在開發報表時,WebForm與WinForm需要ReportViewer才可瀏覽製作完成的報表檔。

image

專案部屬後,要正常瀏覽Report,部署主機需要安裝

  • 報表檢視器2010 可轉散發套件
  • 年代久遠套件可能找不太到。請參考近期的套件安裝。

如今我們參考了

    • Microsofrt.ReportViewerProcessingObjectModel.DLL
    • Microsofrt.ReportViewer.WebForm.DLL

就可避免掉在部署的主機上安裝套件。

2013年1月6日 星期日

LocalReport插入圖片

image

想要在報表中插入圖片,有幾種模式,在此介紹使用來源為資料庫的模式

  • 在影像屬性中設定如下
    • 名稱:自訂
    • 影像來源:資料庫
    • 使用此欄位:在運算式中加入 =System.Convert.FromBase64String(欄位值)
    • image
    • 使用此MIME類型:可選擇image/jpeg,image/png…

SNAGHTML2a4fffd

 

C#:

Byte[] img = GetImageContent();

datatable.rows[0][“IMAGECODE2”]=Convert.ToBase64String(img);

  • 不管影像的格式存放位置在哪裡,實作取得的方法,並且將圖片內容轉為Base64塞入定義好的欄位中。

 

不同於嵌入影像的方式,可以嘗試使用此方式,較有彈性。