Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

541
Visualizações
A GET API (google sheets API) returns an excel sheet in windows 1252 encoding format. How can I download the sheet as excel using nodeJS?

This is the response API gives

This is the code snippet.

axios.get(api , { headers: {"Authorization" : `Bearer ${token}` , "Content-Type":"application/json" , "Accept" : "*/*", "Accept-Encoding" : "gzip, deflate, br"} })
  .then(res => {
  try {     
  let fileString = iconv.decode(res.data, 'win1252');
  fs.writeFile('NodeReport.xlsx' , fileString , (error) => {
    console.log(error);
  });

  }
  catch (err){
    console.log(err);
  }
  }

);

So if I hit the API in browser it downloads the excel file , and If I save response from Postman as file it also downloads excel file . But I want to hit the API from my nodeJS code and then save the file as Excel file . I have tried to use fs.writeFile but it doesn't open the saved file , it says the format is wrong or content is corrupted. Can anyone tell how can I save this file from API response ?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

In your script, as a modification, how about the following modification?

Modified script:

axios
  .get(api, {
    headers: { Authorization: `Bearer ${token}` },
    responseType: "arraybuffer",
  })
  .then((res) => {
    try {
      fs.writeFile("NodeReport.xlsx", res.data, (error) => {
        console.log(error);
      });
    } catch (err) {
      console.log(err);
    }
  });
  • In this modification, the data is retrieved as the array buffer and saved the data as an XLSX file.

  • If the exported file is broken, please modify fs.writeFile("NodeReport.xlsx", res.data, (error) => { to fs.writeFile("NodeReport.xlsx", new Buffer.from(res.data), (error) => { and test it again.

Reference:

  • axios
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda