记一下代码片段
简单实现
const fileName = 'output.xlsx';
const ws = XLSX.utils.json_to_sheet(this.arr);
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, 'output');
XLSX.writeFile(wb, fileName);
<script
type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.13.1/xlsx.full.min.js"
></script>
<script>
function ExportData()
{
filename='reports.xlsx';
data=[{Market: "IN", New Arrivals: "6", Upcoming Appointments: "2", Pending - 1st Attempt: "4"},
{Market: "KS/MO", New Arrivals: "4", Upcoming Appointments: "4", Pending - 1st Attempt: "2"},
{Market: "KS/MO", New Arrivals: "4", Upcoming Appointments: "4", Pending - 1st Attempt: "2"},
{Market: "KS/MO", New Arrivals: "4", Upcoming Appointments: "4", Pending - 1st Attempt: "2"}]
var ws = XLSX.utils.json_to_sheet(data);
var wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "People");
XLSX.writeFile(wb,filename);
}
</script>
比较好的实现
import { read, utils, writeFile } from 'xlsx';
const handleExport = () => {
const data = [
['orderId', 'orderName', 'orderPrice', 'orderCount', 'orderTotalPrice', 'orderStatus'],
];
orders.forEach((order) => {
data.push([
order.orderId,
order.orderName,
order.orderPrice,
order.orderCount,
order.orderTotalPrice,
order.orderStatus,
]);
]);
});
const fileName = 'output.xlsx';
const ws = utils.json_to_sheet(data);
const wb = utils.book_new();
utils.book_append_sheet(wb, ws, 'output');
writeFile(wb, fileName);
}
后记
相关文章
上一篇
Vue3 中使用 keep-alive 组件报错:TypeError: parentComponent.ctx.deactivate is not a function 的解决方法
Vue3 使用 keep-alive 组件时,切换页面报错 TypeError: parentComponent.ctx.deactivate is not a function。文章提供了解决方案:在 keep-alive 和 component 上设置 key 属性进行排序,并附带相关参考文章链接。
下一篇
Vant 样式覆盖问题及解决方法
解决 Vant 组件按需导入导致自定义样式覆盖无效的问题。文章详细介绍了通过全局导入 Vant 样式来解决此问题的方法,并分析了按需导入与全局导入在样式优先级上的差异。