布萊恩的學習筆記

分享是進步的開始--最近研究重點:MCTS

文章分類

每月文章




Locations of visitors to this page

文章標籤

全部標籤

Client Report(RDLC)如何做到多國語系?!

研究完後,小布又有新的想法:『Client Report(RDLC)如何做到多國語系?』。

 

大家都知道Web Form要做到多國語系支援可以透過VS2005 or 2008的『工具 \ 產生本機資源』,來產生相對應的Resource檔。可是*.rdlc檔沒有這個功能(小布找不到...嗚~~),怎麼辦呢?

 

小布想到一個方法來實作一下,還煩請各位專家們如果有更好的方法,告訴小布一下喔!

 

延續 的範例,我將Report.rdlc複製一份並且改名為 Report.en-us.rdlc,並且修改Report.rdlc內的欄位名稱為中文,當成預設Report。為了避免匯出PDF檔時造成中文字變成亂碼???,在修改rdlc檔的同時,順便將原本的 "新細明體" 改成 "PMingLiU"

 

<FontFamily>新細明體</FontFamily>
改成
<FontFamily>PMingLiU</FontFamily>

 

至於Report.en-us.rdlc的欄位字型的部分我是設定成 <FontFamily>Times New Roman</FontFamily>

 

接著就針對Default.aspx來做『產生本機資源』的動作,產生Default.aspx.resx檔案,開啟resx檔並且新增一個字串名稱為"ReportFile",值為"Report.rdlc",最後跟Report.rdlc檔一樣複製一份成Default.aspx.en-us.resx,並修改"ReportFile"的值為"Report.en-us.rdlc"。

 

最後一個步驟,就是讓Default.aspx中的ReportViewer可以由Resources檔來知道要套用哪一個rdlc檔,方法有兩種:

    1. 改Default.aspx原始碼<LocalReport ReportPath="<%$ Resources:ReportFile%>">(第5行)
          <div>
              <rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana" 
                  Font-Size="8pt" Height="400px" Width="989px" 
                  meta:resourcekey="ReportViewer1Resource1">
                  <LocalReport ReportPath="<%$ Resources:ReportFile%>">
                      <DataSources>
                          <rsweb:ReportDataSource DataSourceId="ObjectDataSource1" 
                              Name="AdventureWorksDataSet_Employee" />
                      </DataSources>
                  </LocalReport>
              </rsweb:ReportViewer>
              <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
                  SelectMethod="GetData" 
                  TypeName="AdventureWorksDataSetTableAdapters.EmployeeTableAdapter">
              </asp:ObjectDataSource>
          </div>
    2. 改Default.aspx.vb檔Page_load事件(第11行)
          Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
              Dim cnn As New SqlConnection(ConfigurationManager.ConnectionStrings("AdventureWorksConnectionString").ConnectionString)
              Dim cmd As New SqlCommand("SELECT * FROM HumanResources.Employee WHERE MaritalStatus='S'", cnn)
              Dim da As New SqlDataAdapter(cmd)
              '將動態Select出來的資料填入Datatable當資料來源
              Dim dt As New DataTable
              da.Fill(dt)
              '將ReportViewer1的DataSources集合清除
              ReportViewer1.LocalReport.DataSources.Clear()
              '由Resource檔中讀出Report檔名
              ReportViewer1.LocalReport.ReportPath = MapPath(GetLocalResourceObject("ReportFile").ToString)
              '然後重新新增一個名稱為"AdventureWorksDataSet_Employee"的ReportDataSource給ReportViewer1
              ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource("AdventureWorksDataSet_Employee", dt))
              ReportViewer1.LocalReport.Refresh()
          End Sub
      兩種方法選擇一種來做就OK囉!!

[整個範例檔案結構]

image

[預設的Report]

image

 

要測試其它語系的網頁效果時,需要到IE的『工具 \ 網際網路選項 \ 一般』中的語言,新增你要測試的語系。在這個範例中小布新增了英文 (美國) [en-US]的語言,並且將它往上移到最上面,變成預設的語言後就可以測試了!

 

image

[英文的Report]

 image

 

Brian 的標籤:,

DotBlogs Tags: ASP.NET SQL Reporting posted on 2008/7/23 14:17 | 我要推薦 | 閱讀數 : 335 | 文章分類 [ ASP.NET Reporting ] 訂閱

Feedback

目前沒有回應.
標題
姓名
電子郵件 (將不會被顯示)
個人網頁
內容
登入後使用進階評論
Please add 6 and 8 and type the answer here: