<template> <button @click="exportExcel">导出Excel</button> </template> <script> import XLSX from 'xlsx'; export default { methods: { exportExcel() { // 创建Workbook和工作表 const workbook = XLSX.utils.book_new(); const worksheet = XLSX.utils.aoa_to_sheet([ ['姓名', '年龄', '邮箱'], ['Alice', 28, '[email protected]'], ['Bob', 22, '[email protected]'] ]); // 设置姓名列的文本格式 const nameCellRef = XLSX.utils.encode_cell({ c: 0, r: 0 }); // 第一行第一列 worksheet[nameCellRef].z = 'm'; // 设置单元格格式为文本
worksheet[nameCellRef].z = '@'; // 设置单元格格式为文本,数字也是按照字符串处理
// 将工作表添加到Workbook XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1'); // 生成Excel的配置 const wbout = XLSX.write(workbook, { bookType: 'xlsx', type: 'binary' }); // 创建二进制对象并创建url const blob = new Blob([s2ab(wbout)], { type: 'application/octet-stream' }); const url = URL.createObjectURL(blob); // 创建a标签模拟点击进行下载 const a = document.createElement('a'); if (typeof a.download === 'undefined') { window.location = url; } else { a.href = url; a.download = 'formatted_excel.xlsx'; document.body.appendChild(a); a.click(); document.body.removeChild(a); } // 清除对象URL setTimeout(function() { URL.revokeObjectURL(url); }, 100); } } }; // 将字符串转换为ArrayBuffer function s2ab(s) { const buf = new ArrayBuffer(s.length); const view = new Uint8Array(buf); for (let i = 0; i !== s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF; return buf; } </script>
是在具体的列为文本格式
//设置第colum列为文本格式,输入的内容按照字符串显示
function sheet_from_array_of_arrays_column(data, column) { var ws = {}; var range = {s: {c: 100000, r: 100000}, e: {c: 0, r: 0}}; for (var R = 0; R != data.length; ++R) { for (var C = 0; C != data[R].length; ++C) { if (range.s.r > R) range.s.r = R; if (range.s.c > C) range.s.c = C; if (range.e.r < R) range.e.r = R; if (range.e.c < C) range.e.c = C; var cell = {v: data[R][C]}; if (C==column) { cell.z = '@'; } if (C==column && cell.v == null){ cell.v =''; } var cell_ref = XLSX.utils.encode_cell({c: C, r: R}); if (typeof cell.v === 'number') cell.t = 'n'; else if (typeof cell.v === 'boolean') cell.t = 'b'; else if (cell.v instanceof Date) { cell.t = 'n'; cell.z = XLSX.SSF._table[14]; cell.v = datenum(cell.v); } else cell.t = 's'; ws[cell_ref] = cell; } } if (range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range); return ws; }
生成表格
export function export_json_to_excel_column(th, jsonData, defaultTitle,column) { /* original data */ var data = jsonData; /*方法可向数组的开头添加一个或更多元素,并返回新的长度。*/ data.unshift(th); var ws_name = "SheetJS"; var wb = new Workbook(), ws = sheet_from_array_of_arrays_column(data,column); // const nameCellRef = XLSX.utils.encode_cell({ c: 1, r: 0 }); // 第一行第一列 // console.log(nameCellRef); // ws[nameCellRef].z = '@'; // 设置单元格格式为文本 /* add worksheet to workbook */ wb.SheetNames.push(ws_name); wb.Sheets[ws_name] = ws; var wbout = XLSX.write(wb, {bookType: 'xlsx', bookSST: false, type: 'binary'}); var title = defaultTitle || '列表' saveAs(new Blob([s2ab(wbout)], {type: "application/octet-stream"}), title + ".xlsx") }
标签:XLSX,vue,const,表格,导出,cell,range,ws,var From: https://www.cnblogs.com/RedOrange/p/18094768