AngularJS NG-CSV 文件无法下载

标签 angularjs export-to-csv

我对 AngularJS 相当陌生,我正在尝试使用 ng-csv 从数组生成 .csv 文件。 现在我已经尝试了所有方法,但没有生成文件,我什至尝试了我在互联网上看到的最简单的示例。 我在错误控制台中没有看到任何错误,但仍然没有生成文件。 我在 Windows 下使用 XAMPP 工作。

    <!DOCTYPE html>
<html ng-app="APP">
<head>
    <meta charset="UTF-8">
    <title>angular-count-to example</title>
    <style type="text/css">
        .element{cursor:pointer;float:left;background-color:#ccc;padding:4px;}
    </style>

</head>
<body ng-controller="ExampleController">
    <p>{{data}}</p>
    <button type="button" ng-csv="data" filename="test.csv">Export</button>
    <script type="text/javascript" src="bower_components/angular/angular.min.js"></script>
    <script type="text/javascript" src="bower_components/angular-sanitize/angular-sanitize.min.js"></script>
    <script type="text/javascript" src="bower_components/ng-csv/src/ng-csv/ng-csv.js"></script>
    <script>
        angular.module('APP',["ngSanitize","ngCsv"]).
        controller('ExampleController', function ($scope) {
            $scope.filename = "test";
            $scope.data = [{a: 1, b:2}, {a:3, b:4}];       
        });
    </script>
</body>
</html>

上面是我尝试过的最简单的示例,但是即使这样也不起作用。

最佳答案

尝试纯 HTML5 解决方案。这是我不久前做的一个代码块。尝试通过排除无用的参数来进行自定义

function (JSONData, ReportTitle, ShowLabel, reportType, reportName) {
        //If JSONData is not an object then JSON.parse will parse the JSON string in an Object
        var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;

        angular.forEach(arrData, function (data, index) {
            if (data.date != undefined)
                data.date = dateFormat(data.date)
        });


        var CSV = '';
        //Set Report title in first row or line

        CSV += ReportTitle + '\r\n\n';

        //This condition will generate the Label/Header
        if (ShowLabel) {
            var row = "";

            //This loop will extract the label from 1st index of on array
            for (var index in arrData[0]) {
                row += index + ';';
            }

            row = row.slice(0, -1);

            //append Label row with line break
            CSV += row + '\r\n';
        }

        //1st loop is to extract each row
        for (var i = 0; i < arrData.length; i++) {
            var row = "";

            //2nd loop will extract each column and convert it in string comma-seprated
            for (var index in arrData[i]) {

                //var temp = arrData[i][index].toString().replace('.', ',');
                //arrData[i][index] = temp;

                row += '"' + arrData[i][index] + '";';
            }

            row = row.split('.').join(",");
            row.slice(0, row.length - 1);

            //add a line break after each row
            CSV += row + '\r\n';
        }

        if (CSV == '') {
            alert("Invalid data");
            return;
        }

        //Generate a file name
        var fileName = "MyReport_";
        //this will remove the blank-spaces from the title and replace it with an underscore
        fileName += ReportTitle.replace(/ /g, "_");

        //Initialize file format you want csv or xls
        var uri = 'data:text/csv;charset=utf-8,' + escape(CSV);

        // Now the little tricky part.
        // you can use either>> window.open(uri);
        // but this will not work in some browsers
        // or you will not get the correct file extension

        //this trick will generate a temp <a /> tag
        var link = document.createElement("a");
        link.href = uri;

        //set the visibility hidden so it will not effect on your web-layout
        link.style = "visibility:hidden";
        link.download = fileName + ".csv";

        //this part will append the anchor tag and remove it after automatic click
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
    };

关于AngularJS NG-CSV 文件无法下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27041800/

相关文章:

angularjs - 如何在我的 angular 指令 bower 包中使用单独的模板?

javascript - 我的 Angular 路线在我在 app.js 中定义的实际 URL 之前有一个 '#%2F'

c# - CsvHelper 不将数据写入内存流

ruby-on-rails - 在 ruby​​ 中有效地将 Excel 转换为 CSV

java - 将 SQLite 数据导出到 CSV 仅输出 CSV 文件中数据库的最后一个条目

javascript - 从 Web 应用程序生成设计好的 PDF 报告

javascript - 如何访问 'then'之外的数据

javascript - AngularJS : get translate key in app. js

r - 使用write.csv时将字符转换为日期

r - 在 CSV 文件中附加一个向量作为一行