咔片PPT · AI自动生成演示文稿,模板丰富、排版精美 讯飞智文 · 一键生成PPT和Word,高效应对学习与办公

记录分享日常接单的体会, 关注我,了解程序员私活接单



最近在接单平台,看到这样一个任务:

给定两个输入excel文件,合并关联输出新的excel

输入1:

输入2:

输出:


身为Java后端开发,首先考虑导入导出excel数据到数据库,再通过join关联来解决问题

但是,用户预算有限,另外java本身,桌面应用或者web应用,安装成本太高,因此对于交付不友好,放弃java实现方案

受限于语言熟悉度,决定选择javascript作为解决方案

难点:读取、写入 excel文件

利器:https://github.com/SheetJS/sheetjs

最终页面:


sheetjs部分功能点:

1、读取文件

function readFile(id, file) {

const reader = new FileReader();

reader.onload = function(e) {

let data = e.target.result;

data = new Uint8Array(data);

processWb(XLSX.read(data, {type: 'array'}), (sheetName, dataList) => {

console.log(sheetName, dataList)

$(`.${id}-info .rows`).html(dataList.length + "条")

});

};

reader.readAsArrayBuffer(file);

}

function processWb(wb, callback) {

console.log('---读取excel----')


dbwb = wb;

console.log(wb)


wb.SheetNames.forEach(function(sheetName) {

var now = new Date().getTime()

const data = XLSX.utils.sheet_to_json(wb.Sheets[sheetName],{editable:true});

console.log('--耗时--', (new Date().getTime() - now))

callback(sheetName, data);

});

}

2、导出文件

const exportXlsx = async function() {

const HTMLOUT = document.getElementById('htmlout');

const wb = XLSX.utils.table_to_book(HTMLOUT);

const o = await electron.dialog.showSaveDialog({

title: 'Save file as',

filters: [{

name: "Spreadsheets",

extensions: EXTENSIONS

}]

});

console.log(o.filePath);

XLSX.writeFile(wb, o.filePath);

electron.dialog.showMessageBox({ message: "Exported data to " + o.filePath, buttons: ["OK"] });

};