methods: {
generateJson() {
startTime = startTime ? moment(startTime, 'YYYYMMDDHHmmss').format('YYYY-MM-DD HH:mm:ss') : " ";
endTime = endTime ? moment(endTime, 'YYYYMMDDHHmmss').format('YYYY-MM-DD HH:mm:ss') : " ";
},
dataToExcel(data) {
let style = "color:Black;background-color:White;border-color:#CCCCCC;border-width:1px;border-style:None;width:100%;border-collapse:collapse;font-size:12pt;text-align:center;";
let excel = '<table cellspacing="0" rules="rows" border="1" style=' + style + '>';
let titleArr = ['节目类型', '节目名称', '开始时间', '结束时间', '直播流id'];
let dataKeys = ['type', 'name', 'startTime', 'endTime', 'liveStreamId'];
let row = '<tr style="font-size:15pt;font-weight:700;text-align:center;">';
for (let i = 0; i < titleArr.length; i++) {
row += "<td>" + titleArr[i] + "</td>";
}
excel += row + "</tr>";
for (let j = 0; j < data.length; j++) {
let row = "<tr>";
for (let i = 0; i < dataKeys.length; i++) {
if (dataKeys[i] === 'startTime' || dataKeys[i] === 'endTime') {
row += "<td> " + data[j][dataKeys[i]] + "</td>";
} else {
row += "<td>" + data[j][dataKeys[i]] + "</td>";
}
}
excel += row + "</tr>";
}
excel += "</table>";
return excel;
},
generateExcel(excel) {
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>";
return excelFile;
}
getExcel() {
let dataJson = this.generateJson();
let excelContent = this.dataToExcel(dataJson);
let today = moment().format('YYYY-MM-DD');
let excelName = today + "直播.xls";
let excel = this.generateExcel(excelContent);
saveAs(
new Blob([excel], {type: "application/vnd.ms-excel;charset=utf-8"}),
excelName
)
}
}