Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在angular js中导出excel而不使用第三方库_Javascript_Jquery_Angularjs_Export To Excel - Fatal编程技术网

Javascript 在angular js中导出excel而不使用第三方库

Javascript 在angular js中导出excel而不使用第三方库,javascript,jquery,angularjs,export-to-excel,Javascript,Jquery,Angularjs,Export To Excel,我有范围内的项目列表,即使用ng repeat in table data在html表格中显示UI,下面我尝试将表格数据导出到excel,但仅显示第一页行,我希望显示列表中的所有记录。我拥有范围内所有我必须使用的数据。是否有其他wat来实现这一点 app.controller("ErrorDetailController", [ "$scope", "$location", "$routeParams", "messageService", "errorService", "repository

我有范围内的项目列表,即使用ng repeat in table data在html表格中显示UI,下面我尝试将表格数据导出到excel,但仅显示第一页行,我希望显示列表中的所有记录。我拥有范围内所有我必须使用的数据。是否有其他wat来实现这一点

app.controller("ErrorDetailController", [
"$scope", "$location", "$routeParams", "messageService", "errorService", "repositoryService", , "sharedPageService",

function ($scope, $location, $routeParams, messageService, errorService, repositoryService,sharedPageService, **Excel, $timeout**)
     {      $scope.exportToExcel = function (tableId) { // ex: '#my-table'

        debugger;
        var exportHref = Excel.tableToExcel(tableId, 'sheet name');
        $timeout(function () { location.href = exportHref; }, 100); // trigger download
       }
     }
]); 
app.factory('Excel', function ($window) {
var uri = 'data:application/vnd.ms-excel;base64,',
    template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>',
    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;
    }
  };
 })
app.controller(“ErrorDetailController”[
“$scope”、“$location”、“$routeParams”、“messageService”、“errorService”、“repositoryService”、“sharedPageService”,
函数($scope、$location、$routeParams、messageService、errorService、repositoryService、sharedPageService、**Excel、$timeout**)
{$scope.exportToExcel=function(tableId){//ex:'#我的表'
调试器;
var exportHref=Excel.tableToExcel(tableId,“工作表名称”);
$timeout(函数(){location.href=exportHref;},100);//触发器下载
}
}
]); 
应用程序工厂('Excel',函数($window){
var uri='data:application/vnd.ms excel;base64',
模板=“{table}”,
base64=函数{return$window.btoa(unescape(encodeURIComponent));},
格式=函数(s,c){返回s.replace(/{(\w+)}/g,函数(m,p){返回c[p];})};
返回{
tableToExcel:函数(tableId、工作表名称){
变量表=$(表ID),
ctx={工作表:工作表名称,表:table.html()},
href=uri+base64(格式(模板,ctx));
返回href;
}
};
})

我已经完成了下面的代码,它可能对某些人有所帮助。谢谢

$scope.exportExcel = function () {

    var header = ["columnName1","columnName2"];

    var column = ["data1","data2"];

     Excel.jsonToExcel($scope.list, header, column, 'sheetname', 'filename.xls');
       };
 };
eaiApp.factory('Excel', function ($window) {
var uri = 'data:application/vnd.ms-excel;base64,',
    template = '<?xml version="1.0"?>\n' +
                    '<ss:Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"\n' +
            'xmlns:o="urn:schemas-microsoft-com:office:office"\n' +
            'xmlns:x="urn:schemas-microsoft-com:office:excel"\n' +
            'xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"\n' +
            'xmlns:html="http://www.w3.org/TR/REC-html40">\n' +
                    '<ss:Styles>\n' +
                    '<ss:Style ss:ID="s62">\n' +
                    '<ss:Borders>\n' +
                    '<ss:Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>\n' +
                    '<ss:Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>\n' +
                    '<ss:Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>\n' +
                    '<ss:Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>\n' +
                    '</ss:Borders>\n' +
                    '</ss:Style>\n' +
                    '<ss:Style ss:ID="s63">\n' +
                    '<ss:Borders>\n' +
                    '<ss:Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>\n' +
                    '<ss:Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>\n' +
                    '<ss:Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>\n' +
                    '<ss:Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>\n' +
                    '</ss:Borders>\n' +
                    '<ss:Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000" ss:Bold="1"/>\n' +
                    '<ss:Interior ss:Color="#F4B084" ss:Pattern="Solid"/>\n' +
                    '</ss:Style>\n' +
                    '</ss:Styles>\n' +
                   '<ss:Worksheet ss:Name="{SheetName}">\n' +
                   '<ss:Table>\n',
  };
return {

    jsonToExcel: function (scopeData, header, column,sheetName, fileName) {

        var data = typeof scopeData != "object" ? JSON.parse(scopeData) : scopeData;

        if (data.length != 0) {
            var headerRow = '<ss:Row>\n';
            for (var i = 0; i < header.length; i++) {
                delete data[0].$$hashKey;
                headerRow += '  <ss:Cell ss:StyleID="s63">\n';
                headerRow += '    <ss:Data ss:Type="String">';
                headerRow += header[i] + '</ss:Data>\n';
                headerRow += '  </ss:Cell>\n';
            }
            headerRow += '</ss:Row>\n';
            var xml = template.replace('{SheetName}', sheetName);
            xml += headerRow;

            for (var row = 0; row < data.length; row++) {
                delete data[row].$$hashKey;
                xml += '<ss:Row>\n';
                for (var col = 0; col < column.length; col++) {
                    xml += '  <ss:Cell ss:StyleID="s62">\n';
                    xml += '    <ss:Data ss:Type="String">';
                    xml += data[row][column[col]] + '</ss:Data>\n';
                    xml += '  </ss:Cell>\n';
                }
                xml += '</ss:Row>\n';
            }

            xml += '\n</ss:Table>\n' +
                   '</ss:Worksheet>\n' +
                   '</ss:Workbook>\n';

            var contentType = 'application/octet-stream';
            var uri = 'application/octet-stream,' + escape(xml);
            var link = document.createElement("a");
            var blob = new Blob([xml], {
                'type': contentType
            });
            var myNav = navigator.userAgent.toLowerCase();
            var isIE = (myNav.indexOf('msie') != -1) ? parseInt(myNav.split('msie')[1]) : false;
            if (navigator.appVersion.toString().indexOf('.NET') > 0) {
                window.navigator.msSaveOrOpenBlob(blob, fileName);
            }
            else {

                link.href = window.URL.createObjectURL(blob);
                link.download = fileName;
                document.body.appendChild(link);
                link.click();
                document.body.removeChild(link);
            }
        }
        else
            alert("No records to export.");

        return;
    }
};
$scope.exportExcel=函数(){
变量头=[“columnName1”,“columnName2”];
变量列=[“数据1”,“数据2”];
jsonToExcel($scope.list,header,column,'sheetname','filename.xls');
};
};
eaiApp.factory('Excel',函数($window){
var uri='data:application/vnd.ms excel;base64',
模板='\n'+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”,
};
返回{
jsonToExcel:函数(范围数据、标题、列、表名、文件名){
var data=typeof scopeData!=“object”?JSON.parse(scopeData):scopeData;
如果(data.length!=0){
var headerRow='\n';
对于(变量i=0;i0){
window.navigator.msSaveOrOpenBlob(blob,文件名);
}
否则{
link.href=window.URL.createObjectURL(blob);
link.download=文件名;
document.body.appendChild(链接);
link.click();
document.body.removeChild(link);
}
}
其他的
警报(“没有要导出的记录”);
回来
}
};