教學課程:將文字格式化 (報表產生器)
在本教學課程中,您將能練習以各種不同的方式格式化文字。 使用資料來源和資料集設定空白報表之後,您可以挑選想要探索的步驟來進行。
下圖顯示報表,與您將要建立的報表相似。
您在某個步驟故意出錯,所以知道錯誤的原因是什麼。 接著您要更正錯誤以便達到想要的效果。
您在本教學課程中建立的增強版報表可做為範例SQL Server 2014 Report Builder 報表使用。 如需下載此範例報表和其他報表的詳細資訊,請參閱Report Builder範例報表。
學習內容
設定報表
隨意挑選
完成此教學課程的估計時間:20 分鐘。
需求
如需需求的詳細資訊,請參閱教學課程的必要條件 (報表產生器)。
建立含資料來源和資料集的空白報表
建立空白報表
按一下[開始],指向 [程式],指向Microsoft SQL Server 2014Report Builder,然後按一下[Report Builder]。
注意
[使用者入門] 對話方塊應會隨即出現。 如果沒有出現,請從 [報表產生器] 按鈕按一下 [新增]。
在 [使用者入門] 對話方塊的左窗格中,確認已選取 [新增報表] 。
在右窗格中,按一下 [空白報表] 。
建立資料來源
在 [報表資料] 窗格中,按一下 [新增],然後按一下 [資料來源]。
在 [名稱] 方塊中,輸入:TextDataSource
按一下 [使用內嵌於報表中的連接] 。
確認連線類型為 Microsoft SQL Server,接著在 [連接字串] 方塊中輸入 Data Source = <servername>
注意
例如 < Report001 運算式 servername > 會指定安裝 SQL Server Database Engine 實例的電腦。 本教學課程不需要特定資料;它只需要與 SQL Server 2014 資料庫的連線。 如果 [資料來源連線] 底下已列有資料來源連線,就可以選取該連線並移至下一個程序「建立資料集」。如需詳細資訊,請參閱取得資料連線的替代方式 (報表產生器)。
按一下 [確定] 。
建立資料集
在 [報表資料] 窗格中,按一下 [ 新增],然後按一下 [ 資料集]。
確認資料來源為 TextDataSource。
在 [名稱] 方塊中,輸入:TextDataset。
確認已選取 [文字] 查詢類型,然後按一下 [查詢設計工具] 。
按一下 [當成文字編輯] 。
將下列查詢貼入查詢窗格中:
SELECT CAST('2009-01-05' AS date) as SalesDate, 'Lauren Johnson' as FullName,'Central' as Territory, 'Accessories' as Subcategory,'Carrying Case' as Product, CAST(16996.60 AS money) AS Sales, 68 as Quantity, 'Installing Report Builder' as LinkText, 'https://go.microsoft.com/fwlink/?LinkId=154882' AS URL UNION SELECT CAST('2009-01-05' AS date) as SalesDate, 'Warren Pal' as FullName,'North' as Territory, 'Accessories' as Subcategory, 'Carrying Case' as Product, CAST(13747.25 AS money) AS Sales, 55 as Quantity, 'Getting Started with Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=160556' AS URL UNION SELECT CAST('2009-01-05' AS date) as SalesDate, 'Fernando Ross' as FullName,'South' as Territory, 'Accessories' as Subcategory,'Carrying Case' as Product, CAST(9248.15 AS money) As Sales, 37 as Quantity, 'What is New in Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=165064' AS URL UNION SELECT CAST('2009-01-05' AS date) as SalesDate, 'Lauren Johnson' as FullName,'Central' as Territory, 'Accessories' as Subcategory,'Tripod' as Product, CAST(1350.00 AS money) AS Sales, 18 as Quantity, 'Installing Report Builder' as LinkText, 'https://go.microsoft.com/fwlink/?LinkId=154882' AS URL UNION SELECT CAST('2009-01-05' AS date) as SalesDate, 'Warren Pal' as FullName,'North' as Territory, 'Accessories' as Subcategory,'Tripod' as Product, CAST(1800.00 AS money) AS Sales, 24 as Quantity, 'Getting Started with Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=160556' AS URL UNION SELECT CAST('2009-01-05' AS date) as SalesDate, 'Fernando Ross' as FullName,'South' as Territory, 'Accessories' as Subcategory,'Tripod' as Product, CAST(1125.00 AS money) AS Sales, 15 as Quantity, 'What is New in Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=165064' AS URL UNION SELECT CAST('2009-01-05' AS date) as SalesDate, 'Lauren Johnson' as FullName,'Central' as Territory, 'Accessories' as Subcategory,'Lens Adapter' as Product, CAST(1147.50 AS money) AS Sales, 17 as Quantity, 'Installing Report Builder' as LinkText, 'https://go.microsoft.com/fwlink/?LinkId=154882' AS URL UNION SELECT CAST('2009-01-05' AS date) as SalesDate, 'Warren Pal' as FullName,'North' as Territory, 'Accessories' as Subcategory, 'Lens Adapter' as Product, CAST(742.50 AS money) AS Sales, 11 as Quantity, 'Getting Started with Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=160556' AS URL UNION SELECT CAST('2009-01-05' AS date) as SalesDate, 'Fernando Ross' as FullName,'South' as Territory, 'Accessories' as Subcategory,'Lens Adapter' as Product, CAST(1417.50 AS money) AS Sales, 21 as Quantity, 'What is New in Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=165064' AS URL UNION SELECT CAST('2009-01-06' AS date) as SalesDate, 'Lauren Johnson' as FullName,'Central' as Territory, 'Accessories' as Subcategory, 'Carrying Case' as Product, CAST(13497.30 AS money) AS Sales, 54 as Quantity, 'Installing Report Builder' as LinkText, 'https://go.microsoft.com/fwlink/?LinkId=154882' AS URL UNION SELECT CAST('2009-01-06' AS date) as SalesDate, 'Warren Pal' as FullName,'North' as Territory, 'Accessories' as Subcategory, 'Carrying Case' as Product, CAST(11997.60 AS money) AS Sales, 48 as Quantity, 'Getting Started with Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=160556' AS URL UNION SELECT CAST('2009-01-06' AS date) as SalesDate, 'Fernando Ross' as FullName,'South' as Territory, 'Accessories' as Subcategory, 'Carrying Case' as Product, CAST(10247.95 AS money) As Sales, 41 as Quantity, 'What is New in Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=165064' AS URL UNION SELECT CAST('2009-01-06' AS date) as SalesDate, 'Lauren Johnson' as FullName,'Central' as Territory, 'Accessories' as Subcategory, 'Tripod' as Product, CAST(1200.00 AS money) AS Sales, 16 as Quantity, 'Installing Report Builder' as LinkText, 'https://go.microsoft.com/fwlink/?LinkId=154882' AS URL UNION SELECT CAST('2009-01-06' AS date) as SalesDate, 'Warren Pal' as FullName,'North' as Territory, 'Accessories' as Subcategory,'Tripod' as Product, CAST(2025.00 AS money) AS Sales, 27 as Quantity, 'Getting Started with Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=160556' AS URL UNION SELECT CAST('2009-01-06' AS date) as SalesDate, 'Fernando Ross' as FullName,'South' as Territory, 'Accessories' as Subcategory,'Tripod' as Product, CAST(1425.00 AS money) AS Sales, 19 as Quantity, 'What is New in Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=165064' AS URL UNION SELECT CAST('2009-01-06' AS date) as SalesDate, 'Lauren Johnson' as FullName,'Central' as Territory, 'Accessories' as Subcategory,'Lens Adapter' as Product, CAST(887.50 AS money) AS Sales, 13 as Quantity, 'Installing Report Builder' as LinkText, 'https://go.microsoft.com/fwlink/?LinkId=154882' AS URL UNION SELECT CAST('2009-01-06' AS date) as SalesDate, 'Warren Pal' as FullName,'North' as Territory, 'Accessories' as Subcategory, 'Lens Adapter' as Product, CAST(607.50 AS money) AS Sales, 9 as Quantity, 'Getting Started with Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=160556' AS URL UNION SELECT CAST('2009-01-06' AS date) as SalesDate, 'Fernando Ross' as FullName,'South' as Territory, 'Accessories' as Subcategory,'Lens Adapter' as Product, CAST(1215.00 AS money) AS Sales, 18 as Quantity, 'What is New in Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=165064' AS URL UNION SELECT CAST('2009-01-05' AS date) as SalesDate, 'Lauren Johnson' as FullName,'Central' as Territory, 'Digital' as Subcategory,'Compact Digital' as Product, CAST(10191.00 AS money) AS Sales, 79 as Quantity, 'Installing Report Builder' as LinkText, 'https://go.microsoft.com/fwlink/?LinkId=154882' AS URL UNION SELECT CAST('2009-01-05' AS date) as SalesDate, 'Warren Pal' as FullName,'North' as Territory, 'Digital' as Subcategory, 'Compact Digital' as Product, CAST(8772.00 AS money) AS Sales, 68 as Quantity, 'Getting Started with Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=160556' AS URL UNION SELECT CAST('2009-01-05' AS date) as SalesDate, 'Fernando Ross' as FullName,'South' as Territory, 'Digital' as Subcategory, 'Compact Digital' as Product, CAST(10578.00 AS money) AS Sales, 82 as Quantity, 'What is New in Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=165064' AS URL UNION SELECT CAST('2009-01-05' AS date) as SalesDate, 'Lauren Johnson' as FullName,'Central' as Territory,'Digital' as Subcategory, 'Slim Digital' as Product, CAST(7218.10 AS money) AS Sales, 38 as Quantity, 'Installing Report Builder' as LinkText, 'https://go.microsoft.com/fwlink/?LinkId=154882' AS URL UNION SELECT CAST('2009-01-05' AS date) as SalesDate, 'Warren Pal' as FullName,'North' as Territory,'Digital' as Subcategory, 'Slim Digital' as Product, CAST(8357.80 AS money) AS Sales, 44 as Quantity, 'Getting Started with Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=160556' AS URL UNION SELECT CAST('2009-01-05' AS date) as SalesDate, 'Fernando Ross' as FullName,'South' as Territory,'Digital' as Subcategory,'Slim Digital' as Product, CAST(9307.55 AS money) AS Sales, 49 as Quantity, 'What is New in Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=165064' AS URL UNION SELECT CAST('2009-01-06' AS date) as SalesDate, 'Lauren Johnson' as FullName,'Central' as Territory, 'Digital' as Subcategory,'Compact Digital' as Product, CAST(3870.00 AS money) AS Sales, 30 as Quantity, 'Installing Report Builder' as LinkText, 'https://go.microsoft.com/fwlink/?LinkId=154882' AS URL UNION SELECT CAST('2009-01-06' AS date) as SalesDate, 'Warren Pal' as FullName,'North' as Territory, 'Digital' as Subcategory,'Compact Digital' as Product, CAST(5805.00 AS money) AS Sales, 45 as Quantity, 'Getting Started with Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=160556' AS URL UNION SELECT CAST('2009-01-06' AS date) as SalesDate, 'Fernando Ross' as FullName,'South' as Territory, 'Digital' as Subcategory, 'Compact Digital' as Product, CAST(8643.00 AS money) AS Sales, 67 as Quantity, 'What is New in Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=165064' AS URL UNION SELECT CAST('2009-01-06' AS date) as SalesDate, 'Lauren Johnson' as FullName,'Central' as Territory, 'Digital' as Subcategory, 'Slim Digital' as Product, CAST(9877.40 AS money) AS Sales, 52 as Quantity, 'Installing Report Builder' as LinkText, 'https://go.microsoft.com/fwlink/?LinkId=154882' AS URL UNION SELECT CAST('2009-01-06' AS date) as SalesDate, 'Warren Pal' as FullName,'North' as Territory, 'Digital' as Subcategory, 'Slim Digital' as Product, CAST(12536.70 AS money) AS Sales, 66 as Quantity, 'Getting Started with Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=160556' AS URL UNION SELECT CAST('2009-01-06' AS date) as SalesDate, 'Fernando Ross' as FullName,'South' as Territory, 'Digital' as Subcategory, 'Slim Digital' as Product, CAST(6648.25 AS money) AS Sales, 35 as Quantity, 'What is New in Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=165064' AS URL
按一下 [執行]\( ! ) 來執行查詢。
查詢結果會成為可供報表顯示的資料。
按一下 [確定] 。
將欄位加入至報表設計介面
如果希望擷取自資料集的欄位出現在報表中,您可能會不加思索地直接將欄位拖曳到設計介面。 此練習將點出為何這樣做無效,以及應該改用的方法。
將欄位加入至報表 (會得到錯誤的結果)
將 [FullName] 欄位從 [報表資料] 窗格拖曳到設計介面。
Report Builder會在其中建立具有運算式的文字方塊,表示為 <Expr > 。
按一下 [執行] 。
請注意,只有一筆記錄 Fernando 擷取,這是查詢中第一筆記錄的字母順序。 欄位並未重複成顯示該欄位內的其他記錄。
按一下 [設計] 返回 [設計] 檢視。
選取文字方塊中的運算式 < Expr > 。
在 [屬性] 窗格中,您會看到 [值] 屬性如下 (若未看見 [屬性] 窗格,請檢查 [檢視] 索引標籤上的 [屬性]):
=First(Fields!FullName.Value, "TextDataSet")
First
函數是設計成僅擷取欄位內的第一個值,而其目的也已達到。直接將欄位拖曳到設計介面會建立文字方塊。 文字方塊本身並非資料區,所以不會顯示來自報表資料集的資料。 位於資料區 (例如資料表、矩陣和清單) 內的文字方塊才會顯示資料。
選取文字方塊 (如果您已選取運算式,請按 ESC 鍵選取文字方塊),然後按下 DELETE 鍵。
將欄位加入至報表 (會得到正確的結果)
在功能區的 [插入] 索引標籤上,按一下 [資料區] 區域內的 [清單]。 按一下設計介面,然後進行拖曳以建立寬約 2 英吋且高約 1 英吋的方塊。
將 [FullName] 欄位從 [報表資料] 窗格拖曳到清單方塊中。
這回報表產生器會建立一個內有運算式
[FullName]
的文字方塊。按一下 [執行] 。
請注意,此時方塊已經重複成顯示查詢中的所有記錄。
按一下 [設計] 返回 [設計] 檢視。
選取文字方塊中的運算式。
在 [屬性] 窗格中,您會看到 [值] 屬性如下:
=Fields!FullName.Value
透過將文字方塊拖曳到清單資料區,就可以顯示資料集內的資料。
選取清單方塊,然後按下 DELETE 鍵。
將資料表加入至報表設計介面
建立這個資料表,以便能夠在其中放置超連結和旋轉的文字。
將資料表加入至報表
在 [ 插入] 功能表上,按一下 [ 資料表],然後按一下 [ 資料表精靈]。
在 [新增資料表或矩陣精靈] 的 [ 選擇資料集 ] 頁面上,按一下 [ 選擇此報表中的現有資料集或共用資料集],然後按一下 [此報表] 中的 [TextDataset] () ,然後按 [ 下一步]。
在 [ 排欄欄位 ] 頁面上,將 [領域]、 [LinkText] 和 [ 產品 ] 欄位拖曳至 [ 資料列群組],將 [銷售 ] 欄位拖曳至 [ 值],然後按 [ 下一步]。
在 [ 選擇版面配置 ] 頁面上,清除 [ 展開/折迭群組 ] 核取方塊,讓您可以看到整個資料表,然後按 [ 下一步]。
在 [ 選擇樣式] 頁面上,按一下 [Slate],然後按一下 [ 完成]。
將資料表拖曳到標題區塊下方。
按一下 [執行] 。
資料表看起來似乎沒問題,不過卻有兩個總計資料列。 LinkText欄位不需要 [總計] 資料列。
按一下 [設計] 返回 [設計] 檢視。
以滑鼠右鍵按一下包含
[LinkText]
的文字方塊,然後按一下 [ 分割資料格]。選取儲存格下方的
[LinkText]
空白儲存格,然後按住 SHIFT 鍵,然後選取右邊的兩個儲存格:[產品] 資料行中的[總計] 儲存格和[Sum(Sales)]
[銷售] 資料行中的儲存格。選取這三個儲存格後,以滑鼠右鍵按一下其中一個儲存格,然後按一下 [ 刪除資料列]。
按一下 [執行] 。
加入超連結至報表
在本節中,您要加入超連結指向上一節資料表中的文字。
加入超連結至報表
按一下 [設計] 返回 [設計] 檢視。
以滑鼠右鍵按一下含有
[LinkText]
的資料格,然後按一下 [文字方塊屬性]。在 [ 文字方塊屬性] 方 塊中,按一下 [ 動作]。
按一下 [移至 URL]。
在 [ 選取 URL ] 方塊中,按一下 [URL],然後按一下 [ 確定]。
請注意,文字看起來並無任何改變。 您需要進行調整使其彷彿連結文字。
選取
[LinkText]
。在 [常用]索引標籤的 [字型] 區段中,按一下 [底線] 按鈕,然後按一下 [色彩] 按鈕旁的下拉箭號,然後按一下[藍色]。
按一下 [執行] 。
文字現在看起來就像一個連結。
按一下該連結。 如果您的電腦已連接至網際網路,瀏覽器將會開啟報表產生器說明主題。
旋轉報表中的文字
在本節中,您要旋轉前幾節資料表中的部分文字。
旋轉文字
按一下 [設計] 返回 [設計] 檢視。
按一下包含下列項目的資料格:
[Territory].
在 [主資料夾] 索引標籤的 [字型] 區段中,按一下 [粗體] 按鈕。
如果 [屬性] 窗格並未開啟,請選取 [檢視] 索引標籤上的 [屬性] 核取方塊。
在 [屬性] 窗格中找出 WritingMode 屬性。
注意
當 [屬性] 窗格中的屬性組織成類別目錄時,WritingMode 會位於 [當地語系化] 類別目錄中。 請確定您已選取資料格,而不是文字。 WritingMode 是文字方塊的屬性,並非文字的屬性。
在清單方塊中,按一下 [Rotate270]。
在 [段落] 區段的 [首頁] 索引標籤上,按一下 [中間] 和 [中心] 按鈕,以垂直和水準方式找出儲存格中央的文字。
按一下 [執行]\( ! )。
如今 [Territory]
資料格中的文字已呈垂直方向,從資料格底部往上書寫。
顯示 HTML 格式的文字
顯示格式化為 HTML 的文字
按一下 [設計] ,切換到 [設計] 檢視。
在 [插入] 索引標籤上,按一下 [文字方塊],然後在設計介面上按一下並拖曳,以在資料表底下建立大約 4 英吋寬、3 英吋高的文字方塊。
複製以下文字並將其貼入文字方塊中:
<h4>Limitations of cascading style sheet attributes</h4> <p>Only a basic set of <b>cascading style sheet (CSS)</b> attributes are defined:</p> <ul><li> text-align, text-indent </li><li> font-family, font-size </li><li> color </li><li> padding, padding-bottom, padding-top, padding-right, padding-left </li><li> font-weight </li></ul>
選取文字方塊中的所有文字。
因為這是文字的屬性,而非文字方塊屬性,您可以在單一文字方塊中混雜純文字與使用 HTML 標記做為樣式的文字。
以滑鼠右鍵按一下所有選取的文字,然後按一下 [文字屬性]。
在 [ 一般 ] 頁面的 [ 標記類型] 下,按一下 [HTML - 將 HTML 標籤解譯為樣式]。
按一下 [確定]。
按一下 [執行]\( ! ) 預覽報表。
文字方塊中的文字會顯示成標頭、段落和項目符號清單。
將貨幣格式化
將數字格式化為貨幣
按一下 [設計] ,切換到 [設計] 檢視。
按一下含有
[Sum(Sales)]
的最上方資料表資料格,然後按住 SHIFT 鍵,再按一下含有[Sum(Sales)]
的底部資料表資料格。在 [主資料夾] 索引標籤的 [數字] 群組中,按一下 [貨幣] 按鈕。
(選擇性) 在 [ 常用] 索引標籤的 [ 數位 ] 群組中,按一下 [ 預留位置樣式 ] 按鈕,然後按一下 [ 範例值 ] 以查看數位的格式。
(選擇性) 在 [主資料夾] 索引標籤的 [數字] 群組中,按一下[減少小數位數] 按鈕兩次,顯示沒有分的貨幣數字。
按一下 [執行]\( ! ) 預覽報表。
報表如今已顯示格式化的資料,更容易閱讀。
儲存報表
您可以將報表儲存至報表伺服器、SharePoint 文件庫或您的電腦上。
本教學課程會將報表儲存至報表伺服器。 如果您沒有報表伺服器的存取權,請將報表儲存在您的電腦上。
若要將報表儲存在報表伺服器上
在 [報表產生器] 按鈕中,按一下 [另存新檔] 。
按一下 [最近使用的網站和伺服器] 。
選取或輸入您有權儲存報表之報表伺服器的名稱。
「正在連接到報表伺服器」訊息隨即顯示。 連接完成時,您就會看見報表伺服器管理員指定為預設報表位置之報表資料夾的內容。
在 [名稱] 中,將預設名稱取代為您選擇的名稱。
按一下 [檔案] 。
報表就會儲存至報表伺服器。 您連接之報表伺服器的名稱會顯示在視窗底部的狀態列中。
將報表儲存到您的電腦上
在 [報表產生器] 按鈕中,按一下 [另存新檔] 。
按一下 [桌面] 、 [我的文件] 或 [我的電腦] ,然後瀏覽到您要儲存報表的資料夾。
在 [名稱] 中,將預設名稱取代為您選擇的名稱。
按一下 [檔案] 。
後續步驟
Report Builder教學課程:建立自由格式報表 (Report Builder) 有許多方式可以格式化文字,其中包含更多範例。
另請參閱
教學課程 (報表產生器)
設定報表項目的格式 (報表產生器及 SSRS)
SQL Server 2014 中的報表產生器