好,我正在使用此代码将http://brianhann.com/exporting-spreadsheets-from-ui-grid/导出链接到表格中的excel ui-grid,但我跳过了这个错误,我通过了发生在我身上的事情。 我在代码中出现错误:
$rows.forEach(function (row, ri) {
ri +=1;
$scope.gridApi.grid.columns.forEach(function (col, ci) {
var loc = XLSX.utils.encode_cell({r: ri, c: ci});
sheet[loc] = {
v: row[ci].value,
t: 's'
};
endLoc = loc;
});
问候,非常感谢。
控制器的代码是:
$scope.exportXLSX = function(){
var data = [
[] // header row
];
var rows = uiGridExporterService.getData($scope.gridApi.grid, uiGridExporterConstants.VISIBLE, uiGridExporterConstants.VISIBLE);
var sheet = {};
$scope.gridApi.grid.columns.forEach(function (col, i) {
if (col.visible) {
var loc = XLSX.utils.encode_cell({r: 0, c: i})
sheet[loc] = {
v: col.displayName
};
}
});
var endLoc;
rows.forEach(function (row, ri) {
ri +=1;
$scope.gridApi.grid.columns.forEach(function (col, ci) {
var loc = XLSX.utils.encode_cell({r: ri, c: ci});
sheet[loc] = {
v: row[ci].value,
t: 's'
};
endLoc = loc;
});
});
sheet['!ref'] = XLSX.utils.encode_range({ s: 'A1', e: endLoc });
var workbook = {
SheetNames: ['Sheet1'],
Sheets: {
Sheet1: sheet
}
};
var wopts = { bookType: 'xlsx', bookSST: false, type: 'binary' };
var wbout = XLSX.write(workbook, wopts);
saveAs(new Blob([s2ab(wbout)], {type: ""}), "test.xlsx");
};
答案 0 :(得分:1)
我建议你使用alasql
https://github.com/agershun/alasql
使用类似的代码:
$scope.exportMyData = function() {
alasql('SELECT c1 AS columnA, c2 AS columnB, c3 AS columnC INTO XLSX("data.xlsx",{headers:true}) FROM ?',
[$scope.myDataSource]);
}
并在HTML中
<button type="button" class="btn btn-primary" ng-click="exportMyData()"> Export </button>