如何在Qlik Sense Mashup外部实现点击文件导出Excel格式对象?
脱离Qlik Sense Mashup环境实现Excel导出的可行性与实现思路
问题背景
我有一段部署在Qlik Sense Hub中的HTML代码(如下),它可以通过传入应用ID和对象ID自动导出Excel格式的对象数据。现在希望脱离Qlik Sense Mashup环境,实现点击外部文件即可触发Excel格式对象的导出,请问是否具备可行性?请提供实现思路。
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>Qlik Sense Mashup</title> <meta charset="utf-8"> <script src="https://pa-qliksense.aviv.gov.il/sense/app/8a9ba0aa-386b-423d-a0a5-a2305e1e7896"></script> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <meta name="HandheldFriendly" content="True"> <meta name="MobileOptimized" content="320"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <meta http-equiv="cleartype" content="on"> <script src="../../resources/assets/external/requirejs/require.js"></script> </head> <body style="overflow: auto"> <div id="CurrentSelections" class="qvobjects" style="position:relative; top:0; left:0; width:100%; height:38px;"></div> <!-- Qlik Sense object placeholder --> <div id="4ae44ca4-baee-4031-9d93-3b9895052f24"></div> <!-- Automatically trigger export on page load --> <script> var prefix = window.location.pathname.substr(0, window.location.pathname.toLowerCase().lastIndexOf("/extensions") + 1); var config = { host: window.location.hostname, prefix: prefix, port: window.location.port, isSecure: window.location.protocol === "https:" }; require.config({ baseUrl: (config.isSecure ? "https://" : "http://") + config.host + (config.port ? ":" + config.port : "") + config.prefix + "resources" }); require(["js/qlik"], function (qlik) { qlik.setOnError(function (error) { console.error('Error:', error); alert('Error: ' + error.message); }); // Open app var app = qlik.openApp('8a9ba0aa-386b-423d-a0a5-a2305e1e7896', config); // Get table object app.getObject('4ae44ca4-baee-4031-9d93-3b9895052f24').then(function (model) { // Create a table object var table = qlik.table(model); // Export data from the table table.exportData({ download: true, format: 'OOXML' }).then(exportData => { var blob = new Blob([exportData], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }); var url = URL.createObjectURL(blob); // Create a link element var a = document.createElement('a'); a.href = url; a.download = 'exported_data.xlsx'; // Set desired file name // Trigger a click event on the link to initiate download a.click(); // Release the object URL URL.revokeObjectURL(url); }).catch(error => { console.error('Error exporting table data:', error); alert('Error exporting table data. See console for details.'); }); }).catch(error => { console.error('Error getting table object:', error); alert('Error getting table object. See console for details.'); }); }); </script> </body> </html>
可行性分析
完全具备可行性,但需要解决两个核心问题:Qlik Sense API的跨域访问权限和用户身份认证——脱离Mashup环境后,外部文件无法直接继承Qlik Hub的会话,必须通过合法的认证方式调用Qlik Sense的API。
实现思路
1. 基于Qlik Sense REST API实现(推荐)
REST API更适合外部系统调用,无需加载完整的Qlik Sense前端框架:
- 认证方式:
- 企业内部环境可使用Windows集成认证(NTLM/Kerberos),要求客户端机器加入域,用户已登录域账号且拥有Qlik应用的访问权限。
- 通用场景使用API密钥认证:在Qlik Sense管理控制台创建API密钥并关联有权限的用户,调用API时在请求头中携带
Authorization: Bearer <API_KEY>。
- 核心步骤:
- 构造导出请求:调用
/api/v1/apps/{appId}/objects/{objectId}/export/excel接口,指定导出格式为OOXML。 - 处理导出任务:接口返回导出任务ID后,轮询
/api/v1/exports/{exportId}获取状态,当状态为COMPLETED时,下载对应文件。 - 前端触发:在外部HTML文件中添加按钮,点击后通过
fetch或axios发起上述请求,完成下载。
- 构造导出请求:调用
2. 基于Qlik Engine API(原Mashup逻辑改造)
如果想复用原有Mashup的逻辑,需调整配置支持跨域:
- 配置Qlik Sense跨域:在Qlik Sense的
settings.ini中添加AllowCrossDomain=true并重启服务(生产环境需谨慎,建议限制允许的域名)。 - 改造代码:
- 硬编码Qlik Sense的
host、prefix、port、isSecure配置,不再依赖当前页面路径。 - 处理认证:集成认证下浏览器会自动处理;API密钥认证需在
config中添加headers: { 'Authorization': 'Bearer <API_KEY>' }。 - 移除Mashup相关DOM元素(如
CurrentSelections),保留核心导出逻辑,添加按钮触发导出(替换原页面加载自动导出逻辑)。
- 硬编码Qlik Sense的
3. 注意事项
- 权限控制:确保认证账号拥有目标Qlik应用和对象的读取权限及导出权限。
- 跨域安全:生产环境避免全局开启跨域,建议通过Qlik Sense虚拟代理配置允许特定域名的跨域请求。
- 性能优化:大数据量对象建议使用REST API的异步导出方式,避免前端请求超时。
内容的提问来源于stack exchange,提问作者uriel
相关产品推荐
相关产品推荐

