2015年12月29日 星期二

Formatting Numbers and Dates in RDLC Report

引用

Formatting Numbers and Dates in RDLC Report

You can specify a format for numeric and date values by updating the Format property of its text box with a formatting string. For example, you can set a text box for a numeric field to display the number as currency. Reporting Services uses Microsoft .NET Framework formatting strings or you can create a custom formatting string for the Format property.

NoteNote:
To apply formatting to a text box, the text box must contain an expression, for example, =Fields!LineTotal.Value or =1000. If the text box is not an expression, that is, if the text in the text box does not begin with the equal (=) sign, then the text is interpreted as a string and formatting does not apply.

Formatting Numbers

The following table lists common .NET Framework number formatting strings.
Format string
Name
C or cCurrency
D or dDecimal
E or eScientific
F or fFixed-point
G or gGeneral
N or nNumber
P or pPercentage
R or rRound-trip
X or xHexadecimal
You can modify many of the format strings to include a precision specifier that defines the number of digits to the right of the decimal point. For example, a formatting string of D0 formats the number so that it has no digits after the decimal point. You can also use custom formatting strings, for example, #,###.

Formatting Dates

The following table lists common .NET Framework date formatting strings.
Format string
Name
dShort date
DLong date
tShort time
TLong time
fFull date/time (short time)
FFull date/time (long time)
gGeneral date/time (short time)
GGeneral date/time (long time)
M or mMonth day
R or rRFC1123 pattern
Y or yYear month

You can also a use custom formatting strings; for example, dd/MM/yy. For more information about .NET Framework formatting strings, see Formatting Types.

Expression
Result
 =Globals!ExecutionTime
03-07-2012 17:16
 =Format(Globals!ExecutionTime,"dd MMMM yyyy")
07-Mar-12
 =Format(Globals!ExecutionTime,"dd/MM/yyyy")
13-03-2012
 =Format(Globals!ExecutionTime,"dd-MM-yyyy")
13-03-2012
 =Format(Globals!ExecutionTime,"MM-dd-yyyy")
03-13-2012
 =Format(Globals!ExecutionTime,"yyyy-MM-dd HH mm")
13-03-2012 19:11
 =Format(Globals!ExecutionTime,"yyyy-MM-dd HH mm tt")
2012-03-13 19:11 PM
 =Format(Globals!ExecutionTime,"yyyy-MM-dd hh mm tt")
13-03-2012 19:11
 =Format(Globals!ExecutionTime,"dddd, MMMM dd yyyy")
Tuesday, March 13 2012
 =Format(Globals!ExecutionTime,"ddd,MMM d yyyy")
Tue,Mar 13 2012
 =Format(Globals!ExecutionTime,"ddd, MMM d "'"yy")
Sun, Jan 8 '06
 =Format(Globals!ExecutionTime,"dddd, MMMM dd")
Sunday, January 08
 =Format(Globals!ExecutionTime,"M/yy")
01-Jun
 =Format(Globals!ExecutionTime,"d")
01-08-2006
 =Format(Globals!ExecutionTime,"D")
Sunday, January 08, 2006
 =Format(Globals!ExecutionTime,"f")
Sunday, January 08, 2006 11:53 PM
 =Format(Globals!ExecutionTime,"F")
Sunday, January 08, 2006 11:53:52 PM
 =Format(Globals!ExecutionTime,"g")
01-08-2006 23:53
 =Format(Globals!ExecutionTime,"G")
01-08-2006 23:53
 =Format(Globals!ExecutionTime,"m")
Jan-08
 =Format(Globals!ExecutionTime,"r")
Sun, 08 Jan 2006 23:53:52 GMT
 =Format(Globals!ExecutionTime,"s")
2006-01-08T23:53:52
 =Format(Globals!ExecutionTime,"t")
11:53 PM
 =Format(Globals!ExecutionTime,"T")
11:53:52 PM
 =Format(Globals!ExecutionTime,"u")
2006-01-08 23:53:52Z
 =Format(Globals!ExecutionTime,"U")
Monday, January 09, 2006 5:53:52 AM
 =Format(Globals!ExecutionTime,"y")
January, 2006

RDLC报表若要隱藏第一頁或最後一頁的頁首或頁尾

引用

若要隱藏第一頁或最後一頁的頁首或頁尾

  1. 在 [配置] 檢視中,按一下頁首或頁尾。
  2. 在 [屬性] 視窗中,針對 [PrintonFirstPage] 或 [PrintonLastPage] 屬性,選取 [False]。
Microsoft 報表
※ Microsoft 報表 >> 報表設計工具
※ Microsoft 報表 >> ReportViewer 控制項
※ Microsoft 報表 >> Class Library
D:\Download\微軟\ReportViewerSample

※ 網路爬文
◎ CodeProject sirwan mualla -- Direct print for microsoft_report (RDLC) 2014-7-16 ===> RDLC 不過是格式, 有看到讀檔但看不出來何處塞資料, 要再測才知道

※ Microsoft 報表
    1. 報表設計工具
    2. ReportViewer 控制項
    3. ReportViewer 控制項程式設計參考
※ Microsoft 報表 >> 報表設計工具
   1. 報表概觀
    * 報表種類:自由形式, 多重資料行, 鑽研報表, 互動式報表, 簡單報表
    * 報表部份:可插入 文字方塊, 資料表, 矩陣, 圖表, 影像, 子報表, 清單, 矩形, 線條
                        矩陣提供類似於交叉資料表和樞紐資料表的功能
    * 關於報表定義檔案:RDL XML
          ReportViewer 控制項會利用您定義的資料繫結來擷取資料,且會將結果集合併到報表配置中。
         報表會以您正在使用之控制項的原生輸出格式來呈現。如果是 Web 伺服器控制項,輸出格式便是 HTML。
         如果是 Windows Form 控制項,輸出格式便是 Graphical Device Instrumentation (GDI) 格式。
   2. 建立用戶端報表定義檔案 (.rdlc)
   2.1 使用報表精靈建立用戶端報表定義
       樣式範本:Bold, Corporate, Forest, Generic, Ocean, Slate
       可編輯 \Program Files\Microsoft Visual Studio 9.0\Common7\IDE\Private Assemblies\<語言> 資料夾內的 StyleTemplates.xml 檔案,改變範本。
   2.2 建立報表的資料來源
       資料來源只要可提供 DataTable 或 可列舉的集合

     - 如何加入 ReportViewer 報表的資料來源
       . 將資料來源加入應用程式專案中,再設定對於基礎資料的資料連接。
       . 建立資料表 [TabaleAdapter 精靈]
       . 建立商務物件
   2.3 更新和重新繫結資料來源參考
     * ReportViewer 控制項資料來源參考
     - 利用 [ReportViewer 工作] 智慧標籤面板中的 [選擇資料來源] 工作,將資料來源繫結到從伺服器報表定義檔案 (.rdl) 轉換來的用戶端報表定義檔案 (.rdlc)。
     - 利用 [ReportViewer 工作] 智慧標籤面板中的 [重新繫結資料來源] 工作,使控制項和報表定義與修改的資料集同步。
     - 利用 [報表] 功能表的 [報表資料來源] 對話方塊來更新報表定義檔案所儲存的結構描述資訊。當您在圖形設計模式中編輯用戶端報表定義檔案 (.rdlc) 時,可以使用 [報表] 功能表。
     * 報表定義中的資料來源參考
       1. 開啟用戶端報表定義檔案 (.rdlc)
       2. 選取專案資料來源,按一下[加入報表]
           移除現有的資料集參考
           重新命名資料集,您可以利用 [報表資料來源] 對話方塊來更新報表定義中的資料集名稱
       3.按一下 [全部重新整理]
   2.4 定義報表配置
   2.4.1 加入資料區
       * 加入資料表資料區域
          ,以建立表格式、矩陣、圖表和清單報表。
       * 加入清單資料區域
       * 新增矩陣資料區域
       * 在報表中加入圖表
         直條圖, 橫條圖, 折線圖類型, 圓形圖, XY 圖, 泡泡圖, 區域圖, 環圈圖, 股票圖
   2.4.2 加入文字方塊
         ="這是靜態文字標籤"
         =Fields!UnitCost.Value
         =Fields!TotalUnits.Value * Fields!UnitCost.Value
         =Sum(Fields!TotalUnits.Value)
       * 設定文字方塊屬性
         Calendar 日期要用的日曆, Direction 水平寫入方向, Language , NumeralLanguage 數字格式,NumeralVariant 數字變式, UnicodeBiDi 雙向內嵌層級
         CanGrow 依內容展開文字方塊, CanShrink
   2.4.3 加入影像
   2.4.4 加入矩形、線條和框線
   2.4.5 加入互動性、可見性和導覽
         URL, 書籤, 鑙研
       * 文件引導模式
         顯示成在報表旁的目錄, 區段和群組是依照連結的階層來安排
         匯出時, PDF會將文件引導顯示成 [書籤]。Excel會顯示成階層的具名工作表。
         HOW TO:建立文件引導模式
         HOW TO:建立書籤
         HOW TO:在報表項目上隱藏或定義切換
   2.4.6 加入頁首和頁尾
         PrintOnFirstPage, PrintOnLastPage, InteractiveHeight, InteractiveWidth, PageHeight, PageWidth
         不能直接參考頁首或頁尾中之文字方塊的欄位,不能在頁首或頁尾的欄位中,使用彙總函數。
       * 顯示變數資料
         =First(ReportItems!TXT_LastName.Value)   ' 在頁首或頁尾中參考這個文字方塊
         =Globals.PageNumber & " of " & Globals.TotalPages   ' 頁尾通常會顯示頁碼
         =ReportItems!ReportTitle.Value   ' 報表標題
         =Globals.ReportName & ", dated " & Format(Globals.ExecutionTime, "d")    ' 報表名稱, 產生時間戳記
         =Convert.ToBase64String(Fields!Photo.Value)    ' 加入影像, 須是base64編碼, 隱藏在報表主體中
         =Convert.FromBase64String(ReportItems!TXT_Photo.Value)
         =Sum(ReportItems!Textbox1.Value) ' 頁面總計
      
   2.4.7 加入樣式和格式
       * Color 屬性
         =iif(Fields!Profit.Value < 0, "Red", "Black")
       * BackgroundColor 屬性
         =iif(RowNumber(Nothing) Mod 2, "PaleGreen", "White") ' 資料表白綠相間

        * Format 屬性
       - 格式化數字
         C 貨幣 D 十進位 E 科學記號 F 固定點 G 一般 N 數字 P 百分比 R 往返 X 十六進位
         格式化字串 D0 會將數字格式化,成為小數點之後沒有位數, #,###
       - 格式化日期
         d 簡短日期 D 完整日期 t 簡短時間 T 完整時間 f 詳細日期簡短時間 F 詳細日期完整時間
         g 一般日期簡短時間 G 一般日期完整時間 M 月份日期 R RFC1123 Y 年度月份
         自訂格式化字串;如 dd/MM/yy 格式化型別
   2.4.8 在報表中定義報表參數
         若要在執行階段提供資料值給參數,您必須撰寫傳遞值的程式碼。
         在本機處理模式中,ReportViewer 控制項不會提供用來接受使用者參數值的參數區域。
         您必須將欄位加入表單來取得資料值,然後再利用 SetParameters 方法,將這些值傳給 ReportViewer 控制項。
         參閱 Microsoft.Reporting.WebForms.ReportParameterInfo 和 Microsoft.Reporting.WinForms.ReportParameterInfo
   2.4.9 在報表中群組資料
         可以將小計和其他運算式加入群組首和群組尾中。
         在矩陣中,群組會顯示成動態資料行或資料列。
         可以將群組放在其他群組內,成為巢狀結構,再加上小計。
       - 遞迴階層 € D:\@Source\XXXWeb\MSDNReport
         要拉 TableAdapter 才會自動建立報表的資料集
   2.4.10 在報表中排序資料
   2.4.11 HOW TO:篩選報表中的資料
   2.5 最佳化報表 HTML 輸出配置
   2.6 在報表中使用運算式   報表中運算式的全域集合    報表的內建函式
     * 一般運算式
       =Today()
       =DateAdd(DateInterval.Month, 6, Parameters!StartDate.Value)
     * 字串  可使用 System.String 成員
       =Format(Parameters!StartDate.Value, "D") & " through " & Format(Parameters!EndDate.Value, "D")
       =Right(Parameters!User.Value, Len(Parameters!User.Value) - InStr(Parameters!User.Value, "\"))
       =Parameters!User.Value.Substring(Parameters!User.Value.IndexOf("\")+1, Parameters!User.Value.Length-Parameters!User.Value.IndexOf("\")-1)
       =Join(Parameters!MySelection.Value) ' 将参数 MySelection 的所选值串联到单个字符串中(多選)
     * System.Text.RegularExpressions
        =System.Text.RegularExpressions.Regex.Replace(Fields!Phone.Value, "(\d{3})[ -.]*(\d{3})[ -.]*(\d{4})", "($1) $2-$3")
     * 決策
       =Iif(Fields!LineTotal.Value > 100, True, False)
       =Switch(Fields!PctComplete.Value >= .8, "Green", Fields!PctComplete.Value >= .5, "Amber", Fields!PctComplete.Value <.5, "Red")
     * 報表
       =Sum(Fields!LineTotal.Value, "Order")
       =RowNumber(Nothing)
     * 頁首&頁尾
       =Globals.ReportName & ", dated " & Format(Globals.ExecutionTime, "d")
       =Globals.PageNumber & " of " & Globals.TotalPages
       =First(ReportItems!LastName.Value)

※ Microsoft 報表 >> ReportViewer 控制項
‧ ReportViewer 類別 ..WebControl                                    命名空間: Microsoft.Reporting.WebForms
+P
+ Attributes
+ CurrentPage : int default: 0
+ LocalReport : Report
+ ServerReport
+ ShowBackButton                          g/s that whether the Back button is visible on the toolbar.
+ ShowCredentialPrompts
+ ShowDocumentMapButton
+ ShowExportControls
+ ShowFindControls
+ ShowPageNavigationControls
+ ShowParameterPrompts
+ ShowPrintButton
+ ShowPromptAreaButton
+ ShowRefreshButton
+ ShowReportBody
+ ShowToolBar
+ ShowZoomControl
+ SizeToReportContent
+ ZoomMode
+ ZoomPercent
+M
+JumpToBookmark
+JumpToDocumentMapId
+PerformBack
+E
+ Back
+ BookmarkNavigation
‧ Report 類別                                                                  命名空間: Microsoft.Reporting.WebForms
+P
+ DisplayName
+M
+ GetDefaultPageSettings : ReportPageSettings
+ GetDocumentMap : DocumentMapNode
+ GetParameters
+ GetTotalPages
+ ListRenderingExtensions
+ LoadReportDefinition (Stream) (TextReader)
+ Refresh
+ Render
   參數
       format:      XML, NULL, CSV, IMAGE, PDF, HTML4.0, HTML3.2, MHTML, EXCEL, and HTMLOWC
       deviceInfo
       mimeType
       encoding
       fileNameExtension
       streams
       warnings
+ SetParameters


◎ keisuoy的专栏 Asp.net中动态控制RDLC报表 2007-08-08
1. 透過localReport找到Report對象
2. 報表全部自動生成
   reportViewer1.Reset();
   reportViewer1.LocalReport.LoadReportDefinition(m_rdl);
   reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("MyData", m_dataSet.Tables[0]));
   reportViewer1.RefreshReport();
3. 加戴rdlc到XmlDocument, 修改xml, 交給 ReportViewer


◎ .Net下动态报表生成实现(一) 博客园 蓝天_白云 2011/10/16

   ReportXml rex = new ReportXml(destname);
   ...

   foreach (DataRow row in xz._XiangXhen.Rows) {
      Rex.AddParameter
      Rex.AddDataSetColumn
      Rex.AddTableColumn
      Rex.AddTableHaderSecondRowCell
      Rex.AddDetailsCell
      ...
   }

   Rex.EditPageFooterLineWidth

   this.reportViewer.LocalReport.ReportPath = destname;
   this.reportViewer.LocalReport.SetParameters(cc);
   this.reportViewer.RefreshReport();

   public ReportXml(string filename) {
      ... 產出XML
   }

2015年12月28日 星期一

Save RDLC reports as PDF programmatically

引用http://stackoverflow.com/questions/1652203/save-rdlc-reports-as-pdf-programmatically
用命令Dim bytes() As Byte = Me.ReportViewer1.LocalReport.Render("EXCEL", DBNull.Value.ToString, strMimeType, strEncoding, strFileNameExtension, strStreamIds, warnings)是可以导出Excel
Dim warnings As Microsoft.Reporting.WebForms.Warning() = Nothing

    Dim streamids As String() = Nothing

    Dim mimeType As String = Nothing

    Dim encoding As String = Nothing

    Dim extension As String = Nothing

    Dim deviceInfo As String

    Dim bytes As Byte()

    Dim lr As New Microsoft.Reporting.WebForms.LocalReport

    deviceInfo = "True"

    bytes = ReportViewer1.LocalReport.Render("PDF", deviceInfo, mimeType, encoding, extension, streamids, warnings)

    Response.ClearContent()

    Response.ClearHeaders()

    Response.ContentType = "application/pdf"

    Response.BinaryWrite(bytes)

    Response.Flush()

    Response.Close()
Public Sub CreerFichierRapport(ByVal strNomFichier As String, ByVal strFormat As String)
        Dim bytes() As Byte
        Dim strDeviceInfo As String = ""
        Dim strMimeType As String = ""
        Dim strEncoding As String = ""
        Dim strExtension As String = ""
        Dim strStreams() As String
        Dim warnings() As Warning
        Dim oFileStream As FileStream
        _stream = New List(Of Stream)
        Try
            bytes = rptViewer1.LocalReport.Render(strFormat, strDeviceInfo, strMimeType, strEncoding, strExtension, strStreams, warnings)

            oFileStream = New FileStream(strNomFichier, FileMode.Create)
            oFileStream.Write(bytes, 0, bytes.Length)
            _stream.Add(oFileStream)
        Finally
            If Not IsNothing(oFileStream) Then
                oFileStream.Close()
                oFileStream.Dispose()
            End If
        End Try
    End Sub
Imports Microsoft.Reporting.WebForms
Imports System.IO

Public Class RenderToPDF
    Public Sub Save(ByVal viewer As ReportViewer, ByVal savePath As String)
        Dim Bytes() As Byte = viewer.LocalReport.Render("PDF", "", Nothing, Nothing, Nothing, Nothing, Nothing)

        Using Stream As New FileStream(savePath, FileMode.Create)
            Stream.Write(Bytes, 0, Bytes.Length)
        End Using
    End Sub
End Class