Tengo 4 columnas de datos en la tabla HTML.
Index Age Gender Height 1 10 M 4 2 23 F 5.6 Estoy tratando de exportar las columnas Edad y Género o Género y Altura para sobresalir usando javascript. Aquí está el convertidor .csv pero estoy tratando de convertirlo a Excel. Muestra datos de columnas completas en una sola columna con comas que separan los datos de cada columna
Descargar lista
function exportData(){ /* Get the HTML data using Element by Id */ var table = document.getElementById("tblStocks"); /* Declaring array variable */ var rows =[]; //iterate through rows of table for(var i=0,row; row = table.rows[i];i++){ //rows would be accessed using the "row" variable assigned in the for loop //Get each cell value/column from the row column1 = row.cells[0].innerText; column2 = row.cells[1].innerText; column3 = row.cells[2].innerText; column4 = row.cells[3].innerText; /* add a new records in the array */ rows.push( [ column1, column2, column3, column4 ] ); } csvContent = "data:application/vnd.ms-excel;charset=utf-8,"; rows.forEach(function(rowArray){ row = rowArray.join(","); csvContent += row + "\r\n"; }); /* create a hidden <a> DOM node and set its download attribute */ var encodedUri = encodeURI(csvContent); var link = document.createElement("a"); link.setAttribute("href", encodedUri); link.setAttribute("download", "Report_Result.xls"); document.body.appendChild(link); /* download the data file named "Stock_Price_Report.csv" */ link.click(); } </script>Aquí está la conversión de Excel pero tampoco funciona
<script> function ExportToExcel(type, fn, dl) { var elt = document.getElementById('tblStocks'); var wb = XLSX.utils.table_to_book(elt, { sheet: "report" }); return dl ? XLSX.write(wb, { bookType: type, bookSST: true, type: 'base64' }) : XLSX.writeFile(wb, fn || ('Report_result.' + (type || 'xlsx'))); } </script>lo que quiero en excel como
Age Gender 10 M 23 F or Gender Height M 4 F 5.6