Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

array to excel javascript

//my solution was export as csv and opn file directly in excel
//here is how i didit its in spanish sorry nut you can always google translate

https://asfo.medium.com/exportando-un-json-a-csv-con-javascript-410aee9381d8


function convertToCSV(objArray) {
    var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
    var str = '';

    for (var i = 0; i < array.length; i++) {
        var line = '';
        for (var index in array[i]) {
            if (line != '') line += ','

			if (array[i][index] != null){
				line += array[i][index];
			} else {
				line += ''
			}
        }

        str += line + '
';
    }

    return str;
}

function exportCSVFile(headers, items, fileName) {
 if (headers) {
  items.unshift(headers);
 }

const jsonObject = JSON.stringify(items);

const csv = convertToCSV(jsonObject);

const exportName = fileName + ".csv" || "export.csv";

const blob = new Blob([csv], { type: "text/csv;charset=utf-8;" });
 if (navigator.msSaveBlob) {
  navigator.msSaveBlob(blob, exportName);
 } else {
  const link = document.createElement("a");
  if (link.download !== undefined) {
   const url = URL.createObjectURL(blob);
   link.setAttribute("href", url);
   link.setAttribute("download", exportName);
   link.style.visibility = "hidden";
   document.body.appendChild(link);
   link.click();
   document.body.removeChild(link);
  }
 }
}


const headers = {
 id: 'Identificador',
 nombre: 'Nombre'
};

const data = [
 { id: 1, nombre: 'John Doe' },
 { id: 2, nombre: 'Juan' },
 { id: 3, nombre: 'Samanta' }
];

exportCSVFile(headers, data, 'nombres');

Comment

PREVIOUS NEXT
Code Example
Javascript :: regex to ignore white spaces js 
Javascript :: como ler um arquivo json com javascript 
Javascript :: jquery find previous element with class 
Javascript :: nodejs mysql insert object query 
Javascript :: reverse every word 
Javascript :: js onload 
Javascript :: contains whitespace js function 
Javascript :: js weakset 
Javascript :: iframe chrome devtool 
Javascript :: mongodb update many 
Javascript :: count a character in a string, js 
Javascript :: axios x-api-key for all 
Javascript :: express-ejs-layouts install 
Javascript :: nodejs aws s3 bucket delete item 
Javascript :: JS ignoring accents 
Javascript :: render tab screen when goBack function is called of other screen 
Javascript :: select2 find option by value 
Javascript :: slickcdn 
Javascript :: $(this).text() in jquery return white space 
Javascript :: While loop factorial function in javascript 
Javascript :: loop through object in array javascript 
Javascript :: how to find length of array in javascript without using length method 
Javascript :: zoho smtp mail nodejs 
Javascript :: bootstrap modal clear all fields 
Javascript :: moment format date 
Javascript :: javascript map max value 
Javascript :: onclick inline function react 
Javascript :: get hover element js 
Javascript :: cypress get element val and return it 
Javascript :: convert string in hh:mm am/pm to date js 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =