HTML代码:
<a id="aExportData" hidden><span>Export</span></a>
<div class="col-sm-3 col-md-2 col-lg-1">
<button class="btn btn-primary" id="btnImport" onclick="GetImportData()"><i class="fa fa-file-excel-o"></i> 导出</button>
</div>
function ImportToExcel(data) {
try {
if (data.length == 0) {
layer.msg('无可导出数据', { time: 2000, icon: 0 });
return;
}
var table = '<table id="newTable" border="1" cellspacing="0" cellpadding="0">';
table += '<thead>'
+ '<tr>'
//+ ' <th>配件申请ID</th>'
//+ ' <th>订单ID</th>'
+ ' <th>订单编号</th>'
//+ ' <th>车辆DI</th>'
+ ' <th>车牌号</th>'
+ ' <th>司机姓名</th>'
+ ' <th>配件名称</th>'
+ ' <th>单价</th>'
+ ' <th>数量</th>'
+ ' <th>总价</th>'
+ ' <th>应收金额</th>'
+ ' <th>实收金额</th>'
+ ' <th>欠款金额</th>'
+ ' <th>申请时间</th>'
+ '</tr>'
+ '</thead><tbody>';
for (var i = 0, l = data.length; i < l; i++) {
table += '<tr>'
//+ ' <td>' + data[i].FittingSumID.toString() + '</td>'
//+ ' <td>' + data[i].ApplyID.toString() + '</td>'
+ ' <td>' + data[i].ApplyNO.toString() + '</td>'
//+ ' <td>' + data[i].VehicleID + '</td>'
+ ' <td>' + data[i].RegName + '</td>'
+ ' <td>' + data[i].DriverName + '</td>'
+ ' <td>' + data[i].AccessoriesName + '</td>'
+ ' <td>' + data[i].FittingUnitPrice + '</td>'
+ ' <td>' + data[i].Quantity + '</td>'
+ ' <td>' + data[i].Price + '</td>'
+ ' <td>' + data[i].AbleAmount + '</td>'
+ ' <td>' + data[i].EdAmount + '</td>'
+ ' <td>' + data[i].OwedAmount + '</td>'
+ ' <td>' + data[i].InsertTime + '</td>'
+ '</tr>';
}
table += '</tbody></table>';
// 使用outerHTML属性获取整个table元素的HTML代码(包括<table>标签),然后包装成一个完整的HTML文档,设置charset为urf-8以防止中文乱码
var html = "<html><head><meta charset='utf-8' /></head><body>" + table + "</body></html>";
// 实例化一个Blob对象,其构造函数的第一个参数是包含文件内容的数组,第二个参数是包含文件类型属性的对象
var blob = new Blob([html], { type: "application/vnd.ms-excel" });
var a = document.getElementById("aExportData");
// 利用URL.createObjectURL()方法为a元素生成blob URL
a.href = URL.createObjectURL(blob);
// 设置文件名
a.download = "租户配件费用管理.xls";
$("#aExportData span").click();
}
catch (ex) {
catchTheException("ImportToExcel", ex);
}
};
function GetImportData() {
$('#btnImport').prop("disabled", "true")
$.ajax({
type: "Post",
url: _rootPath + "/BusApply/ApplyFitting/GetFittingCostInfoPageAjax?",
data: {
pVehicleIDs: $("#hidVehicleIDs").val(),
pBTime: $("#tBTime").val() + ' 00:00:00',
pETime: $("#tETime").val() + ' 23:59:59',
pCostFlag: $("#tInsurance").val(),
pDriverName: $("#tRenterName").val(),
page: 1,
rows: 100000
},
dataType: "json",
success: function (data) {
$('#btnImport').prop("disabled", "")
if (data.obj.rows) {
ImportToExcel(data.obj.rows);
}
},
error: function (err) {
$('#btnImport').prop("disabled", "")
layer.msg('请求数据异常' + err, { time: 2000, icon: 0 });
}
});
};