會員權益 我的購物車 如何買書 訂閱電子報 讀者回函
 
 
 
 
 
 
帳號:
密碼:
 

>>加入會員!

 
忘記密碼? 會員專區
 

ASP.NET MVC 開發心得分享:Action 要小心使用 using

今天在 Code Review 的時候抓到一支程式臭蟲,這個功能是某 ASP.NET MVC 專案中的一個匯出下載檔案功能,該功能的 Action ...more

黃保翕(Will保哥) 2010/08/26
 
小編:Vicky
以獨特的視角,帶你發現生活中的無窮樂趣
生活的美妙並不是因為美麗景物,而是與我們身邊的人之間的互動,所產生的笑容跟剎那的記憶有關。要想將生活中珍貴的一面記憶下來,就只能靠日記或是照片了。

在NAVER網站上活躍的
...more
 
       
胡百敬 許薰尹
鄭淑芬 李啟宏
宋志峰 邱世華
呂高旭 陳永昇
麻理鈴 程杰
趙敏翔 羅慧真
更多部落格
 
回首頁 回上一頁
 
類別 >程式開發
 
Visual Studio 2008-初窺Report Viewer 2008
李漢宗 2008/01/29
在過去Visual Studio2005開發報表的過程中,Report Viewer一直是筆者用來顯示報表的好工具,但是使用上常被問到能不能直接列印。Report Viewer在Server Report上是可以直接列印,但是如果使用Local Report模式就有所限制,無法直接從Report Viewer,當初的解決方法是透過Report Viewer控制項直接轉換rdlc報表樣板檔成PDF或是Excel的檔案格式,然後透過reader上的工具列來直接列印。

現在,新一代的Visual Studio 2008對於報表的功能上做了一些新增以及改進。新增以及改進的地方包含:

1、報表在Windows Form以及Web Form上各提供了一個專案樣板。

  圖1:Windows Form Report專案

 
  圖2:Web Form Report專案
 
當專案建立後,如果選擇的是Windows Form的專案樣板,就會產生一個Windows Form以及一個rdlc檔案,並且在一開始就會顯示Local Report報表精靈來做設定,在設定完成後,在Windows Form中就會有一個Report Viewer可以查看報表內容。如果選擇的是ASP.NET Reports Web Site專案樣板,就會建立一個aspx檔案以及rdlc檔案,並且一開始也會透過報表精靈設定報表相關內容。
2、報表精靈。
過去在Local Report跟Server Report上,報表的設定方式不一樣。為什麼會不一樣呢?這一點也是筆者很納悶的地方。在Visual Studio 2008中,報表設定的方式終於統一了,透過報表精靈,我們可以透過step by step方式來做報表設定 。首先我們會看到報表精靈的歡迎畫面。
 
 
點選下一步後,會看到報表來源的選項,這裡我們選擇從Database中獲取資料。
 
 
然後設定連線字串,這裡我們以Northwind資料庫做為範例資料庫。
 
 
儲存連線字串到.config設定檔中。
 
 
這裡我們以Employees資料表作為範例資料表,勾選後選擇結束。
 
 
這時候已經設定好資料來源,如果還需要新增其他資料來源,可以點擊新增資料來源。如果沒有,就選擇下一步。
這裡我們直接選擇下一步。
 
 
畫面上呈現兩個選項,一個是表格(Tabular),另一個則是矩陣(Matrix),這裡我們選擇Tabular。
 
 
對報表內容作一些設定。並選擇下一步。
 
 
選擇呈現方式,這裡選擇Block。選擇下一步。
 
 
選擇Style格式。
 
 
最後設定報表名稱。並按下結束按鈕。
 
 
最後會顯示Report2.rdlc報表。這裡我們不作任何變更。
 
 
然後在Report Viewer上選擇報表為剛剛輸入的報表名稱(筆者的設定為Report2)。
 

 

 

 
3、Report Viewer直接列印。
按下F5執行程式,Web畫面上顯示出Report Viewer以其內容,這裡要注意的是,工具列上多了一個列印按鈕。按下列印按鈕,會直接叫出列印設定視窗。這個新功能真是太棒了,解決了過去無法直接列印的問題。
 
 
4、PDF壓縮處理。
這裡筆者對於Report Viewer 2005以及Report Viewer 2008所轉出來的PDF的大小做了一個比較,大約是4:1的差距。
 
新版的Report Viewer 2008提供了更方便的功能。但是有些人可能有些疑問,如果不想這麼快升級到Visual Studio 2008,那要如何在Visual Studio 2005中使用Report Viewer 2008?微軟提供了Report Viewer 2008的散發套件來供使用者安裝。首先,我們先到微軟網站(http://www.microsoft.com/Downloads/details.aspx?familyid=CC96C246-61E5-4D9E-BB5F-416D75A1B9EF&displaylang=en)下載Microsoft Report Viewer Redistributable 2008,目前僅提供英文以及日文版本。安裝前,我們先注意到安裝需求,適合的OS為Windows Vista以及Windows XP(但是筆者裝在Windows 2003也可以),還有需要先安裝.NET Framework 3.5。
 
安裝後,我們開啟Visual Studio 2005,然後從Data標籤中拖拉出Report Viewer控制項,依照之前步驟設定完後。我們看到C:\Windows\Assembly內的dll檔案,可以看到有8.0跟9.0的版本,9.0版本就是Report Viewer 2008。
 
 
所以這裡我們去調整web.config中的屬性,將8.0.0.0換成9.0.0.0,這裡還要注意到原本的aspx檔案中,有對於Microsoft.ReportViewer.WebForms組件的參考,為了避免每頁都要作修改,我們將設定移到web.config中設定(這裡要注意的是,要記得把原來頁面中的參考移除):
 

<configuration>
  <appSettings/>
  <connectionStrings/>
  <system.web>
    <!--
    Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.

    Visual Basic options:
Set strict="true" to disallow all data type conversions
where data loss can occur.
Set explicit="true" to force declaration of all variables.

  -->
    <httpHandlers>
 <add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, , Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
validate="false" />
</httpHandlers>
 <compilation debug="false" strict="false" explicit="true">
 <assemblies>
 <add assembly="Microsoft.ReportViewer.WebForms, , Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
 <add assembly="Microsoft.ReportViewer.Common, , Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
 </assemblies>
 <buildProviders>
<add extension=".rdlc" type="Microsoft.Reporting.RdlBuildProvider, Microsoft.ReportViewer.Common, , Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
 </buildProviders>
 </compilation>
    <pages>
       <namespaces>
           <clear/>
           <add namespace="System"/>
           <add namespace="System.Collections"/>
           <add namespace="System.Collections.Specialized"/>
           <add namespace="System.Configuration"/>
           <add namespace="System.Text"/>
           <add namespace="System.Text.RegularExpressions"/>
           <add namespace="System.Web"/>
           <add namespace="System.Web.Caching"/>
           <add namespace="System.Web.SessionState"/>
           <add namespace="System.Web.Security"/>
           <add namespace="System.Web.Profile"/>
           <add namespace="System.Web.UI"/>
           <add namespace="System.Web.UI.WebControls"/>
           <add namespace="System.Web.UI.WebControls.WebParts"/>
           <add namespace="System.Web.UI.HtmlControls"/>
       </namespaces>
<!-- 這一段是從aspx頁面中移過來的 -->

     </pages>
     <!--
     The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.

   -->
     <authentication mode="Windows"/>
     <!--
     The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.

   <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>

   -->
  </system.web>
</configuration>

 
按下執行後,就可以看到Visual Studio 2005中的Report Viewer也可以列印了。
 
 
Visual Studio 2008目前還在RTM版本,但是相關的功能大致底定,筆者會在未來繼續介紹相關內容,讓大家了解Visual Studio 2008功能上有哪些增進。
 

  回首頁關於悅知聯絡我們
電話:02-2719-8811 傳真:02-2719-7980 地址:105台北市松山區復興北路99號12樓
copyright © 精誠資訊股份有限公司 悅知文化 All rights reserved