ASP.NET 中 ReportViewer 2008 有兩種匯出格式:Execl 與 PDF,Report viewer 的預設格式是【
選取格式】,因此你要匯出 PDF 時,匯出前需要手動選取 PDF 格式,如果想要畫面第一次載入時就把匯出模式定位在指定的格式,像是 PDF (如下),卻沒有可以設定的地方。
ReportViewer 元件本身沒提供匯出模式預設值設定。
在 Report Viewer 裡有兩個元件【
dropdown 】、【
匯出】,這兩元件最後都會被轉譯成 HTML,顯示在使用者的瀏覽器,因此我們可以將【
匯出】的 ciick 事件註冊一個新的 function,來進行匯出選項的順序和定位處理。
以下的程式碼將會把預設格式設定在 PDF:
首先我們新增一個 report viewer,ID 為
rpvMain <rsweb:ReportViewer ID="rpvMain" runat="server" Font-Names="Trebuchet MS,arial"
BackColor="White" InternalBorderColor="204, 204, 204"
LinkDisabledColor="Black" ShowPrintButton="False" ShowFindControls="False"
Font-Size="12px" Width="970px"
Style="text-align: left;display:inline;" Height="400px">
<LocalReport ReportPath="Report1.rdlc">
</LocalReport>
</rsweb:ReportViewer>接著增加 triggerExport 的 javascript function,並註冊到 ID=rpvMain_ctl01_ctl05_ctl002 的表單元件,這就是匯出項目的下拉選單,ID=[reportViewer ID] + "_ctl01_ctl05_ctl002",以下是詳細的程式:
<script language="javascript" type="text/javascript">
function triggerExport() {
var formatDropDown = document.getElementById('rpvMain_ctl01_ctl05_ctl00');
if (formatDropDown.selectedIndex == 0)
return false;
window.open(document.getElementById('rpvMain').ClientController.m_exportUrlBase
+ encodeURIComponent(formatDropDown.value), '_blan')
//修改Downlist裡Excel的索引為適當值
formatDropDown.selectedIndex = 2;
document.getElementById('rpvMain_ctl01_ctl05_ctl01').Controller.SetViewerLinkActive
(document.getElementById('rpvMain_ctl01_ctl05_ctl00').selectedIndex != 0);
return false;
}
if (document.getElementById('rpvMain_ctl01_ctl05_ctl00') != null) {
// 修改Downlist裡的索引值 ,這邊指到PDF
document.getElementById('rpvMain_ctl01_ctl05_ctl00').selectedIndex = 2;
document.getElementById('rpvMain_ctl01_ctl05_ctl01').onclick = null;
//註冊triggerExport到dropdownlist的click事件
document.getElementById('rpvMain_ctl01_ctl05_ctl01').onclick = triggerExport;
}
</script>如此就大功告成了,如果點下拉選單仍無法觸發 triggerExport,可以點檢視原始碼,查看一下 title="匯出格式" 的Dropdownlist 的 ID (如下)。
再修改以上所有 rpvMain_ctl01_ctl05_ctl01 為正確 ID。
以上不適用 report viewer 2010。
文章編號: 2660952 - 上次校閱: 2014年5月19日 - 版次: 1.3
這篇文章中的資訊適用於:
- Microsoft Report Viwer Redistributable 2008
| kbprb kbtshoot kbstepbystep kbgraphxlink kbmvp kbexpertisebeginner KB2660952 |
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。