xlsx插件导出数据到Excel里百分比变成数字的问题。
需求是将页面数据导入到Excel表格中,数据中的百分比数据在Excel表格里展示的确实小数。
方法如下:
exportExcel() {
//以下是设置导出的Excel的名称,这是表名+当前日期。
const time = new Date()
const year = time.getFullYear()
const month = time.getMonth() + 1
const day = time.getDate()
const name = '名称' + year + '.' + month + '.' + day
// .table要导出的是哪一个表格
var wb = XLSX.utils.table_to_book(document.querySelector('.table'), { raw: true })
/* get binary string as output */
var wbout = XLSX.write(wb, {
bookType: 'xlsx',
bookSST: true,
type: 'array'
})
try {
// name+'.xlsx'表示导出的excel表格名字
FileSaver.saveAs(
new Blob([wbout], { type: 'application/octet-stream' }),
name + '.xlsx'
)
} catch (e) {
if (typeof console !== 'undefined') console.log(e, wbout)
}
return wbout
},
这是完整的正确的代码。开始这样设置var wb = XLSX.utils.table_to_book(document.querySelector('.table'))
导出的字符串自动转换成了小数。 然后通过设置raw:true,就直接导出了字符串并没有做转换。
xlsx插件导出数据到Excel里百分比变成数字的问题。
需求是将页面数据导入到Excel表格中,数据中的百分比数据在Excel表格里展示的确实小数。
方法如下:
exportExcel() {
//以下是设置导出的Excel的名称,这是表名+当前日期。
const time = new Date()
const year = time.getFullYear()
const month = time.getMonth() + 1
const day = time.getDate()
const name = '名称' + year + '.' + month + '.' + day
// .table要导出的是哪一个表格
var wb = XLSX.utils.table_to_book(document.querySelector('.table'), { raw: true })
/* get binary string as output */
var wbout = XLSX.write(wb, {
bookType: 'xlsx',
bookSST: true,
type: 'array'
})
try {
// name+'.xlsx'表示导出的excel表格名字
FileSaver.saveAs(
new Blob([wbout], { type: 'application/octet-stream' }),
name + '.xlsx'
)
} catch (e) {
if (typeof console !== 'undefined') console.log(e, wbout)
}
return wbout
},
这是完整的正确的代码。开始这样设置var wb = XLSX.utils.table_to_book(document.querySelector('.table'))
导出的字符串自动转换成了小数。 然后通过设置raw:true,就直接导出了字符串并没有做转换。