htmledit_views">
众所周知,html" title=前端>前端生成表格通常是用xlsx、html" title=excel>excel.js等js库,但这些库想要生成时增加html" title=excel>excel样式会很麻烦。
有这么一个js库把html表格连样式带数据一并导出为html" title=excel>excel表格: html-table-to-html" title=excel>excel
npm install html-table-to-html" title=excel>excel
使用
html表格:
html"> <el-button @click="goExport">导出</el-button><table id="myTable" ref="myTable"><tr><td style="width: 30px;"></td><td style="background-color: #000;color:yellow">123</td><td style="border:1px solid #000;color:red;font-weight: 700;font-size: 20px;font-family: Arial;">123abc</td></tr></table>
js:
const goExport = () => {const tableDOM = document.querySelector('#myTable');htmlTableToExcel(tableDOM, '文件名.xlsx', 'Sheet1');
}
可以看到表格中单元格背景、字体颜色、边框、字体都导入进了html" title=excel>excel表格,同理就算是图片也可以,如果用xlsx单独给每个单元格都添加样式的话可是相当麻烦