js excel导入数据

1、导入方法:

function onImportExcel(file) {

if (file.target.files[0]) {

let reader = new FileReader();

reader.onload = function (e) {

try {

let data = [];

let workbook = XLSX.read(e.target.result, { type: 'binary' });

const sheetData = workbook.Sheets.Sheet1;

data = data.concat(XLSX.utils.sheet_to_formulae(sheetData));

let formatData = data.map(item => {

return item.split("'")[1]

})

console.log(formatData)

Message["success"]('上传成功')

} catch (e) {

console.log(e)

Message["error"]('excel内容格式错误');

}

};

reader.readAsBinaryString(file.target.files[0]);

} else {

Message["error"]('未选择');

setIps(undefined)

setIpInputDisable(false)

}

}

数据最后格式化这里用的是sheet_to_formulae,可以根据需求使用不同的方法

Exporting:

  • sheet_to_json converts a workbook object to an array of JSON objects.
  • sheet_to_csv generates delimiter-separated-values output
  • sheet_to_formulae generates a list of the formulae (with value fallbacks)

 

2、导入按钮-react 

 

3、css

.fileInput{

opacity:0;

width:100%;

height:100%;

position:absolute;

top:0;

left:0;

}

你可能感兴趣的:(前端,测试开发)