console
$(function(){
$(".in").html("<input onkeydown='CalSN()' class = 'newin' />");
$("a").click(function(){exportPathMethod(exportDataSource)});
})
function CalSN() {
if ($(".StartSN").val().length() >= 6) {
if ($(".Amount").val() > 0 && $(".Amount").val() < 99999) {
var count = int.parseInt($(".Amount").val());
var StartSN = $(".StartSN").val();
var StartTailNum = 100000 + int.parseInt(StartSN.substring(StartSN.length - 5, 5));
var EndSN = StartSN.substring(0, StartSN.length - 5) + (StartTailNum + count - 1).toString().substring(1, 5);
$(".EndSN").val(EndSN);
}
}
}
var exportDataSource = [{
name: '路人甲',
phone: '123456789',
email: '000@123456.com'
}, {
name: '炮灰乙',
phone: '123456789',
email: '000@123456.com'
}, {
name: '土匪丙',
phone: '123456789',
email: '000@123456.com'
}, {
name: '流氓丁',
phone: '123456789',
email: '000@123456.com'
}, ];
function export1() {
JSONToOrderExcelConvertor(exportDataSource);
}
function JSONToOrderExcelConvertor(JSONData) {
var str = 'NO,姓名,电话,邮箱\n';
for(let i=0;i<JSONData.length;i++){
var result ='';
if (JSONData[i].orderStatusc=='0'){
result="是";
} else {
result="否";
}
str += (i+1).toString()+','+JSONData[i].name+'\t'+','+JSONData[i].phone+'\t'+','+JSONData[i].email+'\t'+',\n'
}
alert(str);
var blob = new Blob([str], {type: "text/plain;charset=utf-8"});
blob = new Blob([String.fromCharCode(0xFEFF), blob], {type: blob.type});
object_url = window.URL.createObjectURL(blob);
var link = document.createElement("a");
link.href = object_url;
link.download = "导出订单.xls";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
function JSONToExcelConvertor(JSONData, FileName) {
var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;
var excel = '<table>';
var row = "<tr>";
var keys = Object.keys(JSONData[0]);
keys.forEach(function (item) {
row += "<td>" + item + '</td>';
});
excel += row + "</tr>";
for (var i = 0; i < arrData.length; i++) {
var row = "<tr>";
for (var index in arrData[i]) {
console.log(arrData[i][index]);
row += '<td>' + arrData[i][index] + '</td>';
}
excel += row + "</tr>";
}
excel += "</table>";
var excelFile = "<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'>";
excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">';
excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel';
excelFile += '; charset=UTF-8">';
excelFile += "<head>";
excelFile += "<!--[if gte mso 9]>";
excelFile += "<xml>";
excelFile += "<x:ExcelWorkbook>";
excelFile += "<x:ExcelWorksheets>";
excelFile += "<x:ExcelWorksheet>";
excelFile += "<x:Name>";
excelFile += "{worksheet}";
excelFile += "</x:Name>";
excelFile += "<x:WorksheetOptions>";
excelFile += "<x:DisplayGridlines/>";
excelFile += "</x:WorksheetOptions>";
excelFile += "</x:ExcelWorksheet>";
excelFile += "</x:ExcelWorksheets>";
excelFile += "</x:ExcelWorkbook>";
excelFile += "</xml>";
excelFile += "<![endif]-->";
excelFile += "</head>";
excelFile += "<body>";
excelFile += excel;
excelFile += "</body>";
excelFile += "</html>";
var uri = 'data:application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent(excelFile);
var link = document.createElement("a");
link.href = uri;
link.style = "visibility:hidden";
link.download = FileName + ".xls";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
function exportPathMethod(data) {
var jsonData = [];
for(var i=0; i<data.length ; i++){
jsonData.push({
index :i+1,
name: data[i].name,
phone: data[i].phone,
email :data[i].email
});
}
let str = `index,name,phone,email\n`;
for(let i = 0 ; i < jsonData.length ; i++ ){
for(let item in jsonData[i]){
str+=`${jsonData[i][item] + '\t'},`;
}
str+='\n';
}
let uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(str);
var link = document.createElement("a");
link.href = uri;
link.download = "json数据表.xls";
document.body.appendChild(link);
link.click();
}
<a style="float: left;">导出下载</a>
<div class="in">
<input/>
</div>