當前為10.0版本文檔,更多實例內容將在最新幫助文檔中展現,點選跳轉至 最新版幫助文檔

自定義批量導出按鈕

1. 概述

1.1 問題描述

當需要根據不同用戶不同地區導出多個報表時,不希望分别導出相應格式進行備份,比較麻煩,因此希望可以把多個報表同時導出成某種格式。下面詳細介紹使用步驟:

1.2 實現思路

利用「window.open」來打開帶導出參數 URL 的方式來實現 Excel 導出。相關參數如下所示:

  參數示例  
 http://ip:port/webroot/decision/view/report?reportlets=   http://localhost:8075/webroot/decision/view/report?reportlets=
1)報表中無參數時:
[{reportlet:"/doc/reportname.cpt"},{reportlet:"/doc/reportname.cpt"},{...}] 
 
其中{reportlet:"/doc/reportname.cpt"},是模板的訪問路徑
[{reportlet:"/doc/Primary/CrossReport/Cross.cpt"},{reportlet:"/doc/Primary/DetailReport/Details.cpt"}] 
2)報表中有參數時:
[{reportlet:"/doc/reportname.cpt",paraname1:"paravalue1",paraname2:"paravalue2",...},{reportlet:"/doc/reportname.cpt",paraname1:"paravalue1",paraname2:"paravalue2",...},{...}]
paraname1:"paravalue1",是指導出各模板内容時,對應的參數名和參數值,多個參數中間用逗号隔開

 [{reportlet:"/doc/Primary/Parameter/Parameter.cpt",地區:"華北"},{reportlet:"/doc/Primary/CrossReport/Cross.cpt"}]
  &format=XX,format後面的 XX,是指導出文件的格式 &format=excel 批量導出時導出的是一個文件,如果是eexcel,則會将多個報表導出在多個 sheet 中
 &__filename__=name,對導出的文件重命名"name"就是自定義的文件名&__filename__=export 重命名爲 export

完整示例:導出地區參數爲華北的 Parameter.cpt 和無參數 Cross.cpt 到 Excel 中,其中 sheet1 顯示 Parameter 裏的内容,sheet2 顯示 Cross 裏的内容;并且 Excel 重命名爲 export,如下所示:

http://localhost:8075/webroot/decision/view/report?reportlets=
[{reportlet:"/doc/Primary/Parameter/Parameter.cpt",地區:"華北"},
{reportlet:"/doc/Primary/CrossReport/Cross.cpt"}]
&format=excel&__filename__=export 

注:URL 不能直接訪問,浏覽器可能不支持[] / {} 特殊符号,因此需要進行 編碼轉換 後才能直接訪問使用。上述示例編碼後爲:

http://localhost:8075/webroot/decision/view/report?reportlets=%5B%7Breportlet%3A%22%2Fdoc%2FPrimary%2FParameter%2FParameter.cpt
%22%2C%E5%9C%B0%E5%8C%BA%3A%22%E5%8D%8E%E5%8C%97%22%7D%2C%7Breportlet
%3A%22%2Fdoc%2FPrimary%2FCrossReport%2FCross.cpt%22%7D%5D
&format=excel&__filename__=export

2. 操作步驟

2.1 示例

增加一段 JS 代碼,實現點擊按鈕後導出地區參數爲「華北」的 Parameter.cpt 和無參數 Cross.cpt 到 Excel 中,代碼如下:

var reportlets = "[{reportlet: '/doc/Primary/Parameter/Parameter.cpt',地區: '華北'}, {reportlet: '/doc/Primary/CrossReport/Cross.cpt'}]"
function clickEvent({
    window.open(encodeURI('/webroot/decision/view/report?reportlets=' + reportlets + '&format=excel'));
}

注:自定義批量導出時,如果在導出的同時還在下載多個文件,推薦将浏覽器的安全級别降低,避免被浏覽器将導出文件判定爲不安全文件。

完整代碼如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>FineReport自定義導出</title>
</head>
<script type="text/javascript">
    var reportlets = "[{reportlet: '/doc/Primary/Parameter/Parameter.cpt',地區: '華北'}, {reportlet: '/doc/Primary/CrossReport/Cross.cpt'}]"
    function clickEvent({
        window.open(encodeURI('/webroot/decision/view/report?reportlets=' + reportlets + '&format=excel'));
    }
</script>
<body>
<button
        type="button"
        onclickclickEvent()>
導出[Excel]
</button>
</body>
</html>
顯示代碼

2.2 效果預覽

已完成示例請參照%FR_HOME%/webapps/webroot/help/page_demo/export.html

啓動設計器,輸入http://localhost:8075/webroot/help/page_demo/export.html,點擊「導出 Excel」,即可導出 Excel,效果如下圖所示:
1601012701187559.gif

3. 已完成模板

完整代碼可參考:%FR_HOME%\webapps\webroot\help\page_demo\export.html

示例 HTML文件如下:

export.html


附件列表


主題: 原簡體文檔
  • 有幫助
  • 沒幫助
  • 只是瀏覽

文 檔回 饋

滑鼠選中內容,快速回饋問題

滑鼠選中存在疑惑的內容,即可快速回饋問題,我們將會跟進處理。

不再提示

10s後關閉

反饋已提交

網絡繁忙