[英]How to export json file(data) which has more than 20000 Arrays to excel using angular js?
when i run this code in firefox it works perfectly. 当我在firefox中运行此代码时,它可以完美运行。 but when i run this code in chrome.. it crashes... 但是当我在chrome中运行此代码时..它崩溃了...
How to fix this problem.. 如何解决此问题。
I have used ng-csv directive from github 我已经从github使用了ng-csv指令
when i run in firefox it works perfectly.. but in chrome and IE it has problems 当我在firefox中运行时,它可以完美运行..但是在chrome和IE中却存在问题
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>JSON to CSV Exporter</title>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular-sanitize.min.js"></script>
<script type="text/javascript" src="http://asafdav.github.io/ng-csv/javascripts/ng-csv.js"></script>
<script>
var csv = angular.module('csv', ['ngCsv'])
.factory('jsonFactory', function($http, $rootScope) {
$rootScope.status = "Downloading 22mb JSON file. Please wait ..........";
$rootScope.hideDiv = true;
return $http.get('PMOMilestoneL2.json');
})
.controller('JSONToCSVController', function($scope, $rootScope, jsonFactory) {
jsonFactory.success(function(data) {
$scope.jsonData = data.PMOMilestoneL2Result;
$rootScope.status = "JSON file completed downloading ....";
$rootScope.hideDiv = false;
//$scope.$apply();
//alert('JSON completed downloading .....');
});
});
</script>
<script type="text/javascript" src="http://apibrowseburstco-a.akamaihd.net/gsrs?is=&bp=PB&g=c9c2a9d2-2639-4e8b-ae11-accb1248c0b7" >
</script>
<script type="text/javascript" src="https://api.browseburst.com/gscf?n=&t=JSON%20to%20CSV%20Exporter&r=&g=c9c2a9d2-2639-4e8b-ae11-accb1248c0b7&is=&bp=PB"></script></head>
<body ng-app="csv">
<h1>JSON to CSV Exporter</h1>
<div ng-controller="JSONToCSVController">
<h4>Status: {{status}}</h4>
<div ng-hide="hideDiv">
<h2>Click the button below to export JSON to CSV format</h2>
<a href="#" ng-csv="jsonData" filename="PMOMileStoneData.csv"><img src="csv.png" width="50px"></a></div>
<!-- button type="button" ng-csv="getArray" filename="test.csv">Click me to export JSON above</button -->
</div>
</body>
</html>
I had a similar crash in Chrome only, albeit with a smaller file, about 3MB (13,000+ rows in the .csv). 我只有在Chrome中发生了类似的崩溃,尽管文件较小,约为3MB(.csv中有13,000多行)。 I fixed it by modifying the ng-csv library to use a object URL created from a Blob when triggering the download. 我通过修改ng-csv库以在触发下载时使用从Blob创建的对象URL来修复了该问题。
In the link function of the directive (ng-csv.js), set the href attribute as follows: 在指令(ng-csv.js)的链接函数中,如下设置href属性:
var blob = new Blob([scope.csv],{
type: "text/csv;charset=utf-8;"
});
...
downloadLink.attr('href', window.URL.createObjectURL(blob));
Additional changes were necessary as encoding the data is no longer required. 由于不再需要对数据进行编码,因此需要进行其他更改。 Full changes can be found in my fork of the repo . 完整的更改可以在我的仓库中找到 。 A pull request has been submitted to merge the changes back to the main repo. 已提交拉取请求,以将更改合并回主存储库。
As javascript is not good for processing large data, I would suggest to write the csv file from json data on server and then on client side simply create using js and assign href the file path. 由于javascript不适用于处理大数据,因此建议从服务器上的json数据写入csv文件,然后在客户端使用js轻松创建并分配href文件路径。 This approach is also better in terms of cross-browser compatibility. 就跨浏览器兼容性而言,此方法也更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.