记一下代码片段
简单实现
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);
}
后记
相关文章