I have 4 columns of data in the HTML table.
Index Age Gender Height
1 10 M 4
2 23 F 5.6
I am trying to export the Age and Gender or Gender and Height columns to excel using javascript.
Here is the .csv converter but I am trying to convert to excel. It is displaying whole columns data in a single column with comma separating in each column data
Download list
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>
Here is the excel convert but it isn't working either
<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>
What I want in excel as
Age Gender
10 M
23 F
or
Gender Height
M 4
F 5.6