BIRT (Business Intelligence and Reporting Tools) 是 Eclipse 基金會主導的開源 Java 報表引擎。常規流程需在瀏覽器渲染再點擊導出,體驗割裂。本文提供一種**純後端直輸 Excel** 的方案:前端一鍵調用,瀏覽器直接下載 `.xls` 文件,無需渲染中間頁,極大簡化用戶操作。
function exportProjects() {
var filename = "ProjectReport";
var exportUrl = "https://" + window.location.host
+ "/birt/frameset?__report=report/project_report.rptdesign"
+ "&__title=&__showtitle=false&__toolbar=true&__navigationbar=true"
+ "&__format=xls&__locale=zh_CN&__clean=true"
+ "&__filename=" + filename
+ "&status=" + status
+ "&category=" + category
+ "&__dpi=96&__asattachment=true&__overwrite=false";
window.open(exportUrl);
}
| 參數 | 作用 | 示例值 |
|---|---|---|
| `__report` | 報表設計文件路徑(相對 BIRT 資源目錄) | `report/project_report.rptdesign` |
| `__format` | 導出格式 | `xls` (Excel 97-2003)、`xlsx`、`pdf`、`doc` |
| `__filename` | 下載文件名(不含副檔名) | `ProjectReport` |
| `__asattachment` | `true` 觸發瀏覽器下載而非預覽 | `true` |
| `__locale` | 區域設置,決定日期/數字格式 | `zh_CN` |
| `__clean` | `true` 清理臨時生成文件 | `true` |
| `status`、`category` | 報表參數,需與 `.rptdesign` 定義一致 | 業務動態值 |