node 서비스 excel 내보내기

1924 단어
html 프런트엔드에서 export function ExportExcel 호출 (data, fileName)
try {
    showLoading(' ...');
    fetch('/down_excel', {
      method: 'POST',
      body: JSON.stringify({
        datas: dataSource,
        fileName:fileName,
      }),
      headers: {
        "Content-type": "application/json; charset=UTF-8"
      }
    })
    .then(response => response.blob())
    .then(blob => {
      let url = window.URL.createObjectURL(blob);
      let a = document.createElement('a');
      a.href = url;
      a.download = fileName;
      document.body.appendChild(a); 
      a.click();    
      a.remove();  
  
    }).catch(error => {
      console.log(error);
    })

  } catch (error) {
    console.log(error);
  }finally{
    hideLoading();
  }

}
node 서비스에서 const XLSX = require('xlsx')를 호출합니다.var stream = require('stream');
app.use(bodyParser.urlencoded({extended: false, limit:'100mb'}));app.use(bodyParser.json({ limit:'100mb'}));app.post('/down_excel', (req, res) => { console.log('server down_excel'); let datas2 = req.body.datas; let fileName = req.body.fileName; console.log('fileName',fileName); function send_aoa_to_client(req, res, data, bookType) {
console.log('data',data.length);
var ws = XLSX.utils.json_to_sheet(data);
var wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "Sheet");

/* generate buffer */
var buffer = XLSX.write(wb, {type:'buffer', bookType:bookType || "xlsx"});
res.set({
  'Content-Type': 'application/octet-stream',
  'Content-Disposition': `filename=22`,
  'Content-Length': buffer.length
});
var readStream = new stream.PassThrough();
readStream.end(buffer);
readStream.pipe(res);

} send_aoa_to_client(req,res,datas2);});

좋은 웹페이지 즐겨찾기