I found this function for exporting an HTML table for excel download from this article. I'm using cheerio.js to scrape web data and put it into a MongoDB collection, then displaying that in a table. It seems like the function works all the way up until it reaches a "#" in one of the cells and then stops the export.
Since I didn't write the function myself, I'm not sure what could be causing this but I have a feeling if I add or change some regex it could help? I've never used regex myself so I'm really not sure.
I can post my code for web scraping/the html table if anyone thinks that will be helpful.
HTML to table export:
function exportTableToExcel(tableID, filename = ''){
var downloadLink;
var dataType = 'application/vnd.ms-excel';
var tableSelect = document.getElementById(tableID);
var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');
// Specify file name
filename = filename?filename+'.xls':'excel_data.xls';
// Create download link element
downloadLink = document.createElement("a");
document.body.appendChild(downloadLink);
if(navigator.msSaveOrOpenBlob){
var blob = new Blob(['\ufeff', tableHTML], {
type: dataType
});
navigator.msSaveOrOpenBlob( blob, filename);
}else{
// Create a link to the file
downloadLink.href = 'data:' + dataType + ', ' + tableHTML;
// Setting the file name
downloadLink.download = filename;
//triggering the function
downloadLink.click();
}
}
This is how my output appears in the html table (shows up fine):
But most of it gets cut off when exported to excel: