1

ScreenShot

Hi,

Currently iam implementing functionality of downloading data using LWC by using following code:

    let doc = '<table>';
    // Add styles for the table
    doc += '<style>';
    doc += 'table, th, td {';
    doc += '    border: 1px solid black;';
    doc += '    border-collapse: collapse;';
    doc += '}';          
    doc += '</style>';
    // Add all the Table Headers
    this.type1ReportColumnHeaders.forEach(element => {            
        doc += '<th>'+ element +'</th>'           
    });
    doc += '<tr>';
     doc += '</tr>';
      this.wsWSMMDdetails.forEach(record => {
        doc += '<td  align: center>'+record.normalCDD+'</td>';
        doc += '</tr>';
    });  
    var element = 'data:application/vnd.ms-excel,' + encodeURIComponent(doc);
    let downloadElement = document.createElement('a');
    downloadElement.href = element;
    downloadElement.target = '_self';
    // use .csv as extension on below line if you want to export data as csv
    downloadElement.download = this.weatherStationName+'.xls';
    document.body.appendChild(downloadElement);
    downloadElement.click();

by using above code iam able to download the file but if open the downloaded file iam getting format issue alert as shown in the attachment.Plese suggest me what could be done or any workaround for this issue.

Thank you.

1

2 Answers 2

0

Use .xls instead of .xlsx, or use something like js-xlsx to create a proper XLSX file.

4
  • Hey i want export html string data in excel format in lWC, Please let me know if there is a way... Example HTML String is follows let doc = '<table>'; // Add styles for the table doc += '<style>'; doc += 'table, th, td {'; doc += ' border: 1px solid black;'; doc += ' border-collapse: collapse;'; doc += '}'; doc += '</style>'; // Add all the Table Headers doc += '<tr>'; doc += '<th align="center" colspan="9">' + 'CINCINNATI' +' Weather Station' +'</th>'
    – Murali AS
    Nov 30, 2023 at 17:51
  • @MuraliAS For the HTML format, use .xls instead of .xlsx.
    – sfdcfox
    Nov 30, 2023 at 17:56
  • if i use .xls, when opening the downloaded file shows format issue alert, is there possibility to convert html string to excel by using SheetJS (javascript library)?.
    – Murali AS
    Nov 30, 2023 at 18:07
  • Can Someone help me in this. salesforce.stackexchange.com/questions/413881/…
    – Murali AS
    Nov 30, 2023 at 18:11
0

If you use XL-Connector – it will pull data into Excel with zero issues, manually or automatically, and there's no need to write or maintain any code.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .