前端excel写入信息并下载

// 需要npm install xlsx,安装xlsx依赖


const data = {
  'management': {
    'zh-cn': '管理',
    'en-us': '',
  },
};


function dataToFormat(data) {
  let newdata = [];
  for (let i in data) {
    newdata.push({
      '序号': i,,
      '内容': data[i]['zh-cn'],
    });
  }
  return newdata;
}


function exportToExcel(oldData, fileName) {
  const data = dataToFormat(oldData);
  const XLSX = require('xlsx'); 		// 依赖xlsx
  const newFileName = `${fileName}\.xlsx`;
  const headers = Object.keys(data[0]);
  const worksheet = XLSX.utils.json_to_sheet(data, { header: headers });
  const workbook = XLSX.utils.book_new();
  XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1");
  XLSX.writeFile(workbook, newFileName);
}

exportToExcel(data, 'translate_submit');

在这里插入图片描述

前端excel写入信息并下载_第1张图片

你可能感兴趣的:(前端,javascript)