本文介绍了SheetJS如何格式化列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Vue项目,该项目使用 SheetJS 写入excel文件.
I have a Vue project that writes an excel file using SheetJS.
如何设置生成的excel文件中列的格式?
How do I set the format of the columns in my generated excel file?
我需要将SalesOrderDate,CustomerRequestedDeliveryDate,ConfirmedDate和_ProductionDate设置为日期格式.
I need to set the SalesOrderDate, CustomerRequestedDeliveryDate, ConfirmedDate, and _ProductionDate to date format.
generateExcel() {
axios.get('generateExcel?format=json', {
params: {
division: this.auth.user.current_division,
area: this.form.area,
month: this.form.month
}
})
.then(response => {
let structArray = []
for (let [index, value] of response.data.entries()) {
structArray.push({
SalesOrderNumber: value.so_id,
SalesOrderDate: (value.so_date.trim().length ? moment(value.so_date, 'MMMM, DD YYYY HH:mm:ss', true).format('MM/DD/YYYY'): ''),
ShipmentStatus: value.shipment_status,
Remarks: value.remarks,
ID: value.id,
ModelName: value.model_name,
ModelNumber: value.model_id,
Qty: value.qty,
CustomerRequestedDeliveryDate: (value.requested_delivery_date.trim().length ? moment(value.requested_delivery_date, 'MMMM, DD YYYY HH:mm:ss', true).format('MM/DD/YYYY'): ''),
ConfirmedDate: (value.confirmed_date.trim().length ? moment(value.confirmed_date, 'MMMM, DD YYYY HH:mm:ss', true).format('MM/DD/YYYY'): ''),
'ProductionDateBy': value.production_date_by,
'_ProductionDate': (value.production_date.trim().length ? moment(value.production_date, 'MMMM, DD YYYY HH:mm:ss', true).format('MM/DD/YYYY'): ''),
'_ProductionRemarks': value.production_remarks,
})
}
this.sheet.jsondata = structArray
let ws = XLSX.utils.json_to_sheet(this.sheet.jsondata)
ws['!autofilter'] = { ref: `A1:L${response.data.length+1}` }
ws.columns = [
]
let wb = XLSX.utils.book_new()
wb.Props = {
Title: "Production Schedule Template",
Author: "Admin"
}
XLSX.utils.book_append_sheet(wb, ws, "Schedule")
let wbout = XLSX.write(wb, {type:"array", bookType:"xlsx"})
saveAs(
new Blob([wbout],
{type:"application/octet-stream"}
), "production_schedule.xlsx")
})
.catch(error => {
this.$store.commit('SET_ALERT',{type:'error', message:[error]})
console.log(error)
})
},
推荐答案
不幸的是,在SheetJS中没有轻松格式化列的功能.根据此问题,这是一种解决方法:
Unfortunately, there is no feature to format a column easily in SheetJS. As per this issue, here's a way to do this:
var colNum = XLSX.utils.decode_col("B"); //decode_col converts Excel col name to an integer for col #
var fmt = '$0.00'; // or '"$"#,##0.00_);[Red]\\("$"#,##0.00\\)' or any Excel number format
/* get worksheet range */
var range = XLSX.utils.decode_range(ws['!ref']);
for(var i = range.s.r + 1; i <= range.e.r; ++i) {
/* find the data cell (range.s.r + 1 skips the header row of the worksheet) */
var ref = XLSX.utils.encode_cell({r:i, c:colNum});
/* if the particular row did not contain data for the column, the cell will not be generated */
if(!ws[ref]) continue;
/* `.t == "n"` for number cells */
if(ws[ref].t != 'n') continue;
/* assign the `.z` number format */
ws[ref].z = fmt;
}
这篇关于SheetJS如何格式化列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!