Angularjs 导出到 excel 在 IE 中不工作
Angularjs export to excel not working in IE
我想将我的数据导出为 excel 或 pdf,但它无法在 IE 中使用。我试图在 Chrome 中导出它并且它工作正常。
但是大多数使用我的项目的人都在使用 Internet Explorer。任何人都可以看看我的代码并建议我该怎么做吗?
这是我的 Angular 函数:
scope.exportData = function () {
var date = new Date();
var d = date.getFullYear() + '-' + date.getMonth() + 1 + '-' + date.getDate();
var blob = new Blob([document.getElementById('exportable').innerHTML], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
});
saveAs(blob, "Report_" + d + ".xls");
};
scope.exportDataItems = function () {
var date = new Date();
var d = date.getFullYear() + '-' + date.getMonth() + 1 + '-' + date.getDate();
var blob = new Blob([document.getElementById('exportablePRItems').innerHTML], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
});
saveAs(blob, "Items_"+ d +".xls");
};
}]);
我实际上在使用Blob.js。
也许这会有点帮助。 :)
scope.exportDataNew = function () {
var d = new Date();
var mont = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
if (typeof scope.filter_fromDate == 'undefined') {
scope.filter_fromDate = mont[d.getMonth()] + ' ' + d.getDate().toString() + ", " + d.getFullYear().toString();
}
if (typeof scope.filter_toDate == 'undefined') {
scope.filter_toDate = mont[d.getMonth()] + ' ' + d.getDate().toString() + ", " + d.getFullYear().toString();
}
if (typeof scope.EntityID == 'undefined') {
scope.EntityID = "";
}
if (typeof scope.DepartmentID == 'undefined') {
scope.DepartmentID = "";
}
location.href = 'ExportExcel?from=' + scope.filter_fromDate + '&to=' + scope.filter_toDate + '&EntityID=' + scope.EntityID + '&DepartmentID=' + scope.DepartmentID;
};
scope.exportData = function () {
var date = new Date();
var d = date.getFullYear() + '-' + date.getMonth() + 1 + '-' + date.getDate();
var blob = new Blob([document.getElementById('exportable').innerHTML], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
});
saveAs(blob, "Report_" + d + ".xls");
};
使用alasql。它适用于 IE9
https://github.com/agershun/alasql/wiki/Angular.js
将 xlsx 更改为 xls。
我们可以使用 JSONCSVConverter 实现导出到 excel
涉及的步骤
- JSONtoCSVConverter javascript 文件。
- JSON数据
- Jquery
这一行很重要
JSONToCSVConvertor(数据,"Blog Report",真);
第一个参数接受数据
第二个参数我们指定文件名
第三个参数我们指定是否需要标签
参考
<!DOCTYPE html>
<html>
<head>
<title>Internet explorer download</title>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
</head>
<body>
<div align="center">
<h3><u>Enter JSON data</u></h3>
<div class="mydiv">
<textarea cols="100" rows="15" class="txtarea" id="txt">[{"Blog Name":"PrathapKudupusBlog","Date":"30 Jul 2013 09:24 AM","Type":"Technical","Author"
:"Prathap Kudupu"},
{"Blog Name":"ABCBlog","Date":"30 Jul 2011 09:24 AM","Type":"Technical","Author"
:"ABC"},
{"Blog Name":"XYZBlog","Date":"30 Jul 2011 09:24 AM","Type":"Technical","Author"
:"XYZ"}]</textarea> <br>
<h3><u>Click below button to download <strong>CSV</strong> file for internet explorer and other browsers</u></h3>
<br>
<button class="download">Download CSV</button>
</div>
</body>
</html>
<script>
$(document).ready(function(){
$('button').click(function(){
var data = $('#txt').val();
if(data == '')
return;
JSONToCSVConvertor(data, "Blog report", true);
});
});
function JSONToCSVConvertor(JSONData,title, ShowLabel) {
var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;
var CSV = '';
if (ShowLabel) {
var row = "";
for (var index in arrData[0]) {
row += index + ',';
}
row = row.slice(0, -1);
CSV += row + '\r\n';
}
for (var i = 0; i < arrData.length; i++) {
var row = "";
for (var index in arrData[i]) {
var arrValue = arrData[i][index] == null ? "" : '="' + arrData[i][index] + '"';
row += arrValue + ',';
}
row.slice(0, row.length - 1);
CSV += row + '\r\n';
}
if (CSV == '') {
growl.error("Invalid data");
return;
}
var fileName = title;
if (msieversion()) {
var IEwindow = window.open();
IEwindow.document.write('sep=,\r\n' + CSV);
IEwindow.document.close();
IEwindow.document.execCommand('SaveAs', true, fileName + ".csv");
IEwindow.close();
} else {
var uri = 'data:application/csv;charset=utf-8,' + escape(CSV);
var link = document.createElement("a");
link.href = uri;
link.style = "visibility:hidden";
link.download = fileName + ".csv";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
function msieversion() {
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie != -1 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer, return version number
{
return true;
} else { // If another browser,
return false;
}
}
</script>
我想将我的数据导出为 excel 或 pdf,但它无法在 IE 中使用。我试图在 Chrome 中导出它并且它工作正常。 但是大多数使用我的项目的人都在使用 Internet Explorer。任何人都可以看看我的代码并建议我该怎么做吗?
这是我的 Angular 函数:
scope.exportData = function () {
var date = new Date();
var d = date.getFullYear() + '-' + date.getMonth() + 1 + '-' + date.getDate();
var blob = new Blob([document.getElementById('exportable').innerHTML], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
});
saveAs(blob, "Report_" + d + ".xls");
};
scope.exportDataItems = function () {
var date = new Date();
var d = date.getFullYear() + '-' + date.getMonth() + 1 + '-' + date.getDate();
var blob = new Blob([document.getElementById('exportablePRItems').innerHTML], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
});
saveAs(blob, "Items_"+ d +".xls");
};
}]);
我实际上在使用Blob.js。
也许这会有点帮助。 :)
scope.exportDataNew = function () {
var d = new Date();
var mont = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
if (typeof scope.filter_fromDate == 'undefined') {
scope.filter_fromDate = mont[d.getMonth()] + ' ' + d.getDate().toString() + ", " + d.getFullYear().toString();
}
if (typeof scope.filter_toDate == 'undefined') {
scope.filter_toDate = mont[d.getMonth()] + ' ' + d.getDate().toString() + ", " + d.getFullYear().toString();
}
if (typeof scope.EntityID == 'undefined') {
scope.EntityID = "";
}
if (typeof scope.DepartmentID == 'undefined') {
scope.DepartmentID = "";
}
location.href = 'ExportExcel?from=' + scope.filter_fromDate + '&to=' + scope.filter_toDate + '&EntityID=' + scope.EntityID + '&DepartmentID=' + scope.DepartmentID;
};
scope.exportData = function () {
var date = new Date();
var d = date.getFullYear() + '-' + date.getMonth() + 1 + '-' + date.getDate();
var blob = new Blob([document.getElementById('exportable').innerHTML], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
});
saveAs(blob, "Report_" + d + ".xls");
};
使用alasql。它适用于 IE9
https://github.com/agershun/alasql/wiki/Angular.js
将 xlsx 更改为 xls。
我们可以使用 JSONCSVConverter 实现导出到 excel 涉及的步骤
- JSONtoCSVConverter javascript 文件。
- JSON数据
- Jquery
这一行很重要 JSONToCSVConvertor(数据,"Blog Report",真);
第一个参数接受数据 第二个参数我们指定文件名 第三个参数我们指定是否需要标签
参考
<!DOCTYPE html>
<html>
<head>
<title>Internet explorer download</title>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
</head>
<body>
<div align="center">
<h3><u>Enter JSON data</u></h3>
<div class="mydiv">
<textarea cols="100" rows="15" class="txtarea" id="txt">[{"Blog Name":"PrathapKudupusBlog","Date":"30 Jul 2013 09:24 AM","Type":"Technical","Author"
:"Prathap Kudupu"},
{"Blog Name":"ABCBlog","Date":"30 Jul 2011 09:24 AM","Type":"Technical","Author"
:"ABC"},
{"Blog Name":"XYZBlog","Date":"30 Jul 2011 09:24 AM","Type":"Technical","Author"
:"XYZ"}]</textarea> <br>
<h3><u>Click below button to download <strong>CSV</strong> file for internet explorer and other browsers</u></h3>
<br>
<button class="download">Download CSV</button>
</div>
</body>
</html>
<script>
$(document).ready(function(){
$('button').click(function(){
var data = $('#txt').val();
if(data == '')
return;
JSONToCSVConvertor(data, "Blog report", true);
});
});
function JSONToCSVConvertor(JSONData,title, ShowLabel) {
var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;
var CSV = '';
if (ShowLabel) {
var row = "";
for (var index in arrData[0]) {
row += index + ',';
}
row = row.slice(0, -1);
CSV += row + '\r\n';
}
for (var i = 0; i < arrData.length; i++) {
var row = "";
for (var index in arrData[i]) {
var arrValue = arrData[i][index] == null ? "" : '="' + arrData[i][index] + '"';
row += arrValue + ',';
}
row.slice(0, row.length - 1);
CSV += row + '\r\n';
}
if (CSV == '') {
growl.error("Invalid data");
return;
}
var fileName = title;
if (msieversion()) {
var IEwindow = window.open();
IEwindow.document.write('sep=,\r\n' + CSV);
IEwindow.document.close();
IEwindow.document.execCommand('SaveAs', true, fileName + ".csv");
IEwindow.close();
} else {
var uri = 'data:application/csv;charset=utf-8,' + escape(CSV);
var link = document.createElement("a");
link.href = uri;
link.style = "visibility:hidden";
link.download = fileName + ".csv";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
function msieversion() {
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie != -1 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer, return version number
{
return true;
} else { // If another browser,
return false;
}
}
</script>