我已经从 angular 2 网站创建了我的数据表。现在我想使用angular 2框架将我的json数据导出为PDF,excel。
请告诉我如何获得该链接或任何链接。
此致
答案 0 :(得分:0)
答案 1 :(得分:0)
(function () {
'use strict';
angular.module('ngJsonExportExcel', [])
.directive('ngJsonExportExcel', function () {
return {
restrict: 'AE',
scope: {
data : '=',
filename: '=?',
reportFields: '=',
separator: '@'
},
link: function (scope, element) {
scope.filename = !!scope.filename ? scope.filename : 'SalesReport';
var fields = [];
var header = [];
var separator = scope.separator || ',';
angular.forEach(scope.reportFields, function(field, key) {
if(!field || !key) {
throw new Error('error json report fields');
}
fields.push(key);
header.push(field);
});
element.bind('click', function() {
var bodyData = _bodyData();
var strData = _convertToExcel(bodyData);
var blob = new Blob([strData], {type: "text/plain;charset=utf-8"});
return saveAs(blob, [scope.filename + '.csv']);
});
function _bodyData() {
var data = scope.data;
var body = "";
angular.forEach(data, function(dataItem) {
var rowItems = [];
angular.forEach(fields, function(field) {
if(field.indexOf('.')) {
field = field.split(".");
var curItem = dataItem;
// deep access to obect property
angular.forEach(field, function(prop){
if (curItem !== null && curItem !== undefined) {
curItem = curItem[prop];
}
});
data = curItem;
}
else {
data = dataItem[field];
}
var fieldValue = data !== null ? data : ' ';
if (fieldValue !== undefined && angular.isObject(fieldValue)) {
fieldValue = _objectToString(fieldValue);
}
if(typeof fieldValue == 'string') {
rowItems.push('"' + fieldValue.replace(/"/g, '""') + '"');
} else {
rowItems.push(fieldValue);
}
});
body += rowItems.join(separator) + '\n';
});
return body;
}
function _convertToExcel(body) {
return header.join(separator) + '\n' + body;
}
function _objectToString(object) {
var output = '';
angular.forEach(object, function(value, key) {
output += key + ':' + value + ' ';
});
return '"' + output + '"';
}
}
};
});
})();
答案 2 :(得分:0)
也许您应该查找如何将data-table
导出为JSON
格式对象,然后有大量示例如何将JSON转换为PDF / CSV并且可以在原生{{1}中使用}或JavaScript
。
这些链接可以帮助您:
Converting json to pdf using js frameworks和
http://jsfiddle.net/hybrid13i/JXrwM/
答案 3 :(得分:0)
我终于被这个用了
function downloadExcelFile(ev) {
connectionService.getExcelExport(function (response) {
if (response.status == 200) {
/**
* Export data to csv file
*/
let blob = new Blob([response.data], {type: 'text/csv'});
let filename = `export.csv`;
if(window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveBlob(blob, filename);
}
else{
let elem = window.document.createElement('a');
elem.href = window.URL.createObjectURL(blob);
elem.download = filename;
document.body.appendChild(elem);
elem.click();
document.body.removeChild(elem);
}
console.log(`export excel file is successful.`);
}
})
}
答案 4 :(得分:0)
我可以部分回答您的问题,以便您可以将 JSON
数据导出到 excel
。您可以将 XLSX 库用于 Angular 2+
。
假设您的 JSON
数据类似于分配给 readyToExport
的数据,因此将显示如何导出您的 JSON
数据。
public export() {
const readyToExport = [
{id: 1, name: 'a', address: 'x'},
{id: 2, name: 'b', address: 'y'},
{id: 3, name: 'c', address: 'z'}
];
const workBook = XLSX.utils.book_new(); // create a new blank book
const workSheet = XLSX.utils.json_to_sheet(readyToExport);
XLSX.utils.book_append_sheet(workBook, workSheet, 'data'); // add the worksheet to the book
XLSX.writeFile(workBook, 'temp.xlsx'); // initiate a file download in browser
}