You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用jQuery实现Kendo Grid Excel导出时,如何添加打开(OPEN)选项

How to Add "OPEN" Option to Kendo Excel Export Dialog

Hey there! The issue you're facing is that the default kendo.saveAs() method relies on the browser's native download behavior, which might not always show the "OPEN" option depending on the browser and its settings. To get that option consistently, we can adjust your code to generate a Blob and use a direct download link, which triggers the browser's full download dialog (including the "OPEN" option for most modern browsers).

Here's the modified version of your export function:

exportResultToExcel: function (data) { 
    if (data.length > 0) { 
        var rows = [{ 
            cells: [ 
                { value: "Payer", background: "#1F497D", color: "#fff" }, 
                { value: "Acc No.", background: "#1F497D", color: "#fff" }, 
                { value: "Draft No.", background: "#1F497D", color: "#fff" }, 
            ] 
        }]; 
        for (var i = 0; i < data.length; i++) { 
            var dataItem = data[i]; 
            rows.push({ 
                cells: [ 
                    { value: dataItem.Customer }, 
                    { value: dataItem.AcNo }, // Fixed a typo here: removed extra closing parenthesis
                    { value: dataItem.DtNo }, 
                ] 
            }) 
        } 
        var workbook = new kendo.ooxml.Workbook({ 
            sheets: [ 
                { 
                    columns: [ 
                        { autoWidth: false }, 
                        { autoWidth: false }, 
                        { autoWidth: false }, 
                    ], 
                    rows: rows 
                } 
            ] 
        }); 

        // Replace kendo.saveAs with Blob-based download logic
        var excelData = new Uint8Array(atob(workbook.toDataURL().split(',')[1]).length);
        excelData.set(atob(workbook.toDataURL().split(',')[1]).split('').map(char => char.charCodeAt(0)));
        var blob = new Blob([excelData], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
        
        // Create a temporary download link
        var link = document.createElement('a');
        link.href = URL.createObjectURL(blob);
        link.download = "Export Result.xlsx";
        
        // Trigger the download
        document.body.appendChild(link);
        link.click();
        
        // Clean up to avoid memory leaks
        document.body.removeChild(link);
        URL.revokeObjectURL(link.href);
    } 
}

Key Changes Explained:

  • Fixed a typo: Removed an extra closing parenthesis in dataItem.AcNo) that would have caused a JavaScript error.
  • Blob Conversion: Converted the workbook's data URI into a Blob, which is a more efficient way to handle binary file data in browsers.
  • Temporary Download Link: Created a hidden <a> tag pointing to the Blob's URL. When clicked, this triggers the browser's native download dialog, which should include the "OPEN" option (most modern browsers like Chrome, Firefox, and Edge will show this).
  • Cleanup: Removed the temporary link and revoked the object URL to prevent memory leaks.

Notes:

  • The "OPEN" option's availability still depends on the user's browser settings and whether they have an application associated with .xlsx files (like Microsoft Excel or Google Sheets).
  • For older browsers (like IE11), you might need to add additional handling using navigator.msSaveBlob, but most modern environments won't require this.

内容的提问来源于stack exchange,提问作者Arpita Dutta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 03:22:30