简体   繁体   English

以指定格式导出到csv

[英]Export to csv in specified format

After trying answers of almost every related question on SO, I am still not getting it. 在尝试了关于SO的几乎所有相关问题的答案之后,我仍然没有得到它。

HTML File: HTML文件:

<li><a ng-click="exportToCsv('#total');">
<i class="fa fa-file-o mr-right7"></i>Export to CSV</a></li>

Controller File: 控制器文件:

$scope.exportToCsv=function(tableId){ // ex: '#my-table'
          $scope.exportHref=Csv.tableToExcel(tableId,'List');
            $timeout(function(){location.href=$scope.exportHref;},100);

And my function in controller file itself. 我的功能在控制器文件本身。

gModule.factory('Csv',function($window){
        var uri='data:text/comma-separated-values;base64,',
            template='<table>{table}</table>',
            base64=function(s){return $window.btoa(unescape(encodeURIComponent(s)));},
            format=function(s,c){return s.replace(/{(\w+)}/g,function(m,p){return c[p];})};

        return {
            tableToExcel:function(tableId,worksheetName){
                var table=$(tableId),
                    ctx={worksheet:worksheetName,table:table.html()},
                    href=uri+base64(format(template,ctx));
                return href;
            }
        };
});

I need to export the data in the table in csv format but I am getting the whole html code instead. 我需要以csv格式导出表中的数据,但我得到了整个html代码。

I am supposed to make changed in this format only. 我应该只改变这种格式。 Please Guide. 请指南。 Many thanks for help. 非常感谢您的帮助。

It's generally best not to roll your own when it comes to pre-existing formats. 在预先存在的格式方面,通常最好不要自己动手 There are a lot of intricities which may not be immediately obvious with CSV files, like delimiters, escaping, nulls, the unicode BOM, and others. CSV文件可能不会立即出现很多复杂问题,例如分隔符,转义,空值,unicode BOM等。 Use an existing library which has already dealt with all of the edge cases. 使用已处理所有边缘情况的现有库。

I'd recommend looking at ng-csv which will do what you need. 我建议你看一下你所需要的ng-csv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM