0

I want to export html elements or form to excel(.xlsm) template in php or javascript just as in this image file. I searched for possible solutions but all I get was to export table data to excel, I can use that too but I need the input fields as they are just as in this image.

Till now I have user basic code to export a table into excel but when I change the extenstion of the file then excel says it is a corrupted file, the code I used is like:

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+'.xlsm':'excel_data.xlsm';
    
    // 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 code returns the excel file with or without the custom name.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • This might be useful: https://stackoverflow.com/questions/22223255/how-to-insert-a-html-into-excel – Andyba Aug 20 '20 at 14:04
  • Hey thanks for the help but my project is based on php and javascript......can you have reference in this? – suraj kumar Aug 20 '20 at 15:56

1 Answers1

0

What you are creating is TSV (in this case SSV) - Tab Separated Values, it's not xlsm. Just save it as .tsv and excel will open that.

Konrad
  • 21,590
  • 4
  • 28
  • 64