2

I am fairly new to AngularJS, i am trying to generate .csv files from an Array using ng-csv. Now i have tried everything but the files are not generated, i even tried the most simple example i could see on the internet. I do not see any errors in the error console but still no files are generated. I am working under windows with 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>

Above is the simplest example i tried, however even this is not working.

2 Answers 2

1

Try out a pure HTML5 Solution. This was a code block i did a while ago. Try customizing for yourself by excluding useless paramaters

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);
    };
Sign up to request clarification or add additional context in comments.

2 Comments

I have come to the conclusion that i could better do this in the backend with PHP. I have an array of milions of words i have to compute so this is to much for the frontend to handle. But thanks for the answer.
i do not think that is too much for the frontend. I suggest you test it before getting a conclusion
1

It looks like you are linking to the wrong ng-csv.js. The ng-csv.js file in the bower_components/ng-csv/src/ng-csv/ folder doesn't contain all of the code, you need to use the build version. Try this:

<script type="text/javascript" src="bower_components/ng-csv/build/ng-csv.js"></script>

2 Comments

@user2668026 Any luck?
sorry been very busy the last few weeks so i haven't tried it yet. I will try it this weekend and report the outcome.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.