Xgongiveittoya
Xgongiveittoya

Reputation: 753

CSV file does not open correctly in Excel with ngCsv

I was having an issue with the ng-csv module, and I thought I would share.

The ng-csv module takes a javascript object, and converts it into a downloadable .csv file.

Here is the example code shown here: ngmodules site

The significant parts are here:

      <button class="btn btn-default"
              ng-csv="getArray" lazy-load="true" filename="{{ filename }}.csv" field-separator="{{separator}}"
              >Export to CSV (Lazy Load)
      </button>

And Here:

    <script>
        var myapp = angular.module('myapp', ["ngSanitize", "ngCsv"]);

        myapp.controller('myctrl', function ($scope) {
            $scope.filename = "test";
            $scope.getArray = [{a: 1, b:2}, {a:3, b:4}];

          $scope.addRandomRow = function() {
            $scope.getArray.push({a: Math.floor((Math.random()*10)+1), b: Math.floor((Math.random()*10)+1)});
          };

          $scope.getHeader = function () {return ["A", "B"]};
        });
    </script>

A problem can arise, like it did with my code, where IF your header object starts with an ID column, like this:

$scope.getHeader = function () {return ["ID","A", "B"]};

Excel says that it is a bad file format, and sometimes will have UI issues.

The reason is that Excel does not like csv files starting with a capital ID, according to this source: Microsoft Support

Upvotes: 2

Views: 872

Answers (1)

Xgongiveittoya
Xgongiveittoya

Reputation: 753

Changing the header to:

$scope.getHeader = function () {return ["id","A", "B"]};

Fixes this problem.

Excel does not like .csv files that start with a capital ID.

Upvotes: 3

Related Questions