【问题标题】:Angularjs Export to Excel - doesn't work in IE, and excel not proper in ff and chromeAngularjs 导出到 Excel - 在 IE 中不起作用,在 ff 和 chrome 中 excel 不合适
【发布时间】:2016-04-04 11:30:32
【问题描述】:

我一直在尝试使用 AngularJs 将表格从 jsp 页面导出到 Excel。

Angularjs 不能在 IE 中工作吗?并且还收到此错误SCRIPT5009: 'Node' is undefined

在 Chrome 和 Firefox 中,只有当我保存它而不是 open with 选项时才会打开 Excel。保存的 Excel 不正确。试图解决这个问题数周。

甚至尝试使用 Jquery,但没有运气。 请给我一个解决方案。真的很感激。

我的jsp:

<html>
<head>
<script src="/Pulse/static/scripts/jquery-1.11.2.min.js"></script><script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js"></script>
<script src="https://rawgithub.com/eligrey/FileSaver.js/master/FileSaver.js" type="text/javascript"></script>
</head>
<body>
<div id="myCtrl" ng-app="myApp" ng-controller="myCtrl">
<table>
    <tr><td>
    <p ng-click="exportData()" id="myImg">Click here</p>
    </td></tr>
</table>
    <div id="exportable">
    <table width="100%" id="myTable">
        <thead>
            <tr>
                <th>Num</th>
                <th>Sq</th>
            </tr>
        </thead>
        <tbody>
         <tr>
                <td>2</td>
                <td>4</td>
            </tr>
        </tbody>
    </table>
    </div>
</div>  
</body>
</html>

我的 angularjs:

<script>
var myApp = angular.module('myApp', []);
myApp.controller('myCtrl',function($scope, $http) {
    $scope.exportData = function() {
        var blob = new Blob(
                [ document.getElementById('myTable').innerHTML ],
                {
                    type : "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
                });
        window.saveAs(blob, "MyRep.xls");
    }
});
</script>

我明白了,当在 firefox 中点击 open with excel 时:

打开保存的excel:

谁能指出我做错了什么?

【问题讨论】:

  • @Piyush Khatri 你能告诉我,甚至可以使用 angularjs 导出到 IE 中的 excel 吗?

标签: javascript html angularjs excel jsp


【解决方案1】:

你可以这样做:

var myApp = angular.module('myApp', ['ui.bootstrap', 'ngAnimate', 'ngTouch', 'mgcrea.ngStrap']);myApp.controller('myCtrl',function($scope, $http,$timeout) {

$scope.exportData = function() {
    $timeout(function () {
        var blob = new Blob([ document.getElementById('myTable').innerHTML ],
        {
            type : "application/vnd.ms-excel"
        });
    saveAs(blob, "MyRep.xls");}, 2000);
}

});

我猜在 Firefox 中运行良好

我在我使用的页面中有其他脚本

<link href="~/Content/Controls/AngularStrap/libs.min.css" rel="stylesheet" /><script src="~/Content/Controls/AngularStrap/angular-strap.min.js"></script><script src="~/Content/Controls/AngularStrap/angular-strap.tpl.min.js"></script><script src="~/Content/Controls/FileSaver/FileSaver.js"></script>

【讨论】:

    猜你喜欢
    • 2015-05-22
    • 1970-01-01
    • 2019-07-01
    • 2014-05-07
    • 1970-01-01
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    • 2013-03-12
    相关资源
    最近更新 更多