【发布时间】:2019-12-02 18:31:09
【问题描述】:
我正在尝试使用 angularJS 导出 excel 的 html 表。我能够做到这一点。但是当我导出到 excel 时,我想删除一些 tr 和 td。如何完成这个任务。提前致谢 下面是我的角度代码
angular.module('xxxApp').factory('Excel',function($window){
var uri='data:application/vnd.ms-excel;base64,',
template='<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>',
base64=function(s){return $window.btoa(unescape(encodeURIComponent(s)));},
format=function(s,c){return s.replace(/{(\w+)}/g,function(m,p){return c[p];})};
return {
tableToExcel:function(tableId,worksheetName){
var table=$(tableId),
ctx={worksheet:worksheetName,table:table.html()},
href=uri+base64(format(template,ctx));
return href;
}
};
})
.controller('MyExcelCtrl',function(Excel,$timeout,$scope){
$scope.exportToExcel=function(tableId){ // ex: '#my-table'
var exportHref=Excel.tableToExcel(tableId,'AdminUsers');
var a = document.createElement('a');
a.href = exportHref;
a.download = 'Admin_Users.xls';
a.click();
}
$scope.isExportToExcel= false;
});
请帮我解决这个问题
【问题讨论】:
-
你有什么选择导出 JSON 而不是导出 html 表吗?我猜你有一个 json 数组,它又被渲染为 html 表
-
我无法控制正在呈现为 html 的 JSON。我只需要玩 html 表格。
-
我现在面临的另一个问题。我无法在 IE 和 FireFox 浏览器中下载导出的 excel 但它在 Chrome 浏览器中运行良好。为了浏览器的可比性,我需要在代码中添加什么?
标签: javascript angularjs excel