gpt4 book ai didi

javascript - 在Angular JS中导出Excel而不使用第三方库

转载 作者:行者123 更新时间:2023-12-03 03:39:50 24 4
gpt4 key购买 nike

我有范围内的项目列表,即在表数据中使用 ng-repeat 在 html 表中显示 UI,下面我尝试将表数据导出到 Excel,但只显示第一页行,我想显示所有记录在列表中。我拥有我必须做的范围内的所有数据。还有其他方法可以实现这一目标吗?

app.controller("ErrorDetailController", [
"$scope", "$location", "$routeParams", "messageService", "errorService", "repositoryService", , "sharedPageService",

function ($scope, $location, $routeParams, messageService, errorService, repositoryService,sharedPageService, **Excel, $timeout**)
{ $scope.exportToExcel = function (tableId) { // ex: '#my-table'

debugger;
var exportHref = Excel.tableToExcel(tableId, 'sheet name');
$timeout(function () { location.href = exportHref; }, 100); // trigger download
}
}
]);
app.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;
}
};
})

最佳答案

我已经完成了下面的代码,它可能对某些人有帮助。谢谢....

$scope.exportExcel = function () {

var header = ["columnName1","columnName2"];

var column = ["data1","data2"];

Excel.jsonToExcel($scope.list, header, column, 'sheetname', 'filename.xls');
};
};
eaiApp.factory('Excel', function ($window) {
var uri = 'data:application/vnd.ms-excel;base64,',
template = '<?xml version="1.0"?>\n' +
'<ss:Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"\n' +
'xmlns:o="urn:schemas-microsoft-com:office:office"\n' +
'xmlns:x="urn:schemas-microsoft-com:office:excel"\n' +
'xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"\n' +
'xmlns:html="http://www.w3.org/TR/REC-html40">\n' +
'<ss:Styles>\n' +
'<ss:Style ss:ID="s62">\n' +
'<ss:Borders>\n' +
'<ss:Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>\n' +
'<ss:Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>\n' +
'<ss:Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>\n' +
'<ss:Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>\n' +
'</ss:Borders>\n' +
'</ss:Style>\n' +
'<ss:Style ss:ID="s63">\n' +
'<ss:Borders>\n' +
'<ss:Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>\n' +
'<ss:Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>\n' +
'<ss:Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>\n' +
'<ss:Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>\n' +
'</ss:Borders>\n' +
'<ss:Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000" ss:Bold="1"/>\n' +
'<ss:Interior ss:Color="#F4B084" ss:Pattern="Solid"/>\n' +
'</ss:Style>\n' +
'</ss:Styles>\n' +
'<ss:Worksheet ss:Name="{SheetName}">\n' +
'<ss:Table>\n',
};
return {

jsonToExcel: function (scopeData, header, column,sheetName, fileName) {

var data = typeof scopeData != "object" ? JSON.parse(scopeData) : scopeData;

if (data.length != 0) {
var headerRow = '<ss:Row>\n';
for (var i = 0; i < header.length; i++) {
delete data[0].$$hashKey;
headerRow += ' <ss:Cell ss:StyleID="s63">\n';
headerRow += ' <ss:Data ss:Type="String">';
headerRow += header[i] + '</ss:Data>\n';
headerRow += ' </ss:Cell>\n';
}
headerRow += '</ss:Row>\n';
var xml = template.replace('{SheetName}', sheetName);
xml += headerRow;

for (var row = 0; row < data.length; row++) {
delete data[row].$$hashKey;
xml += '<ss:Row>\n';
for (var col = 0; col < column.length; col++) {
xml += ' <ss:Cell ss:StyleID="s62">\n';
xml += ' <ss:Data ss:Type="String">';
xml += data[row][column[col]] + '</ss:Data>\n';
xml += ' </ss:Cell>\n';
}
xml += '</ss:Row>\n';
}

xml += '\n</ss:Table>\n' +
'</ss:Worksheet>\n' +
'</ss:Workbook>\n';

var contentType = 'application/octet-stream';
var uri = 'application/octet-stream,' + escape(xml);
var link = document.createElement("a");
var blob = new Blob([xml], {
'type': contentType
});
var myNav = navigator.userAgent.toLowerCase();
var isIE = (myNav.indexOf('msie') != -1) ? parseInt(myNav.split('msie')[1]) : false;
if (navigator.appVersion.toString().indexOf('.NET') > 0) {
window.navigator.msSaveOrOpenBlob(blob, fileName);
}
else {

link.href = window.URL.createObjectURL(blob);
link.download = fileName;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
else
alert("No records to export.");

return;
}
};

关于javascript - 在Angular JS中导出Excel而不使用第三方库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45676888/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com