Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 views
in Technique[技术] by (71.8m points)

javascript - HTML table to excel conversion error on open the file as "extention .xls" do not match - how to fix

I am converting html table into excel file. on open it throws and error as follows:

Error

how to prevent it. what is the correct way to conversion? here is my js code :

 let downloadLink;
const dataType = "data:application/vnd.ms-excel";
const tableSelect = `<table>
     <thead>
      <tr>
         ${this.setTitle(data)}
      </tr>
      </thead>
      <tbody>
             ${this.setBody(data)}
      </tbody>
  </table>`;

filename = filename ? filename + ".xls" : "excel_data.xls";
downloadLink = document.createElement("a");
document.body.appendChild(downloadLink);
var tableHTML = tableSelect.replace(/ /g, "%20");
if (navigator.msSaveOrOpenBlob) {
  const blob = new Blob(["ufeff", tableHTML], {
    type: dataType,
  });
  navigator.msSaveOrOpenBlob(blob, filename);
} else {
  downloadLink.href = "data:" + dataType + ", " + tableHTML;
  downloadLink.download = filename;
  downloadLink.click();
}

thanks in advance.

question from:https://stackoverflow.com/questions/66061704/html-table-to-excel-conversion-error-on-open-the-file-as-extention-xls-do-not

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...