4

I am exporting html table to excel using javascript code on browser. All done but when i try to open the file in microsoft excel it gives prompt like :

"Excel cannot open the file 'filename.xlsx' because the file format for the file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.Do you want to open file now?"

If i press yes it works fine and all data gets displayed properly in excel.

i want to remove this prompt.

My JavaScript code is

function fnExcelReport()
{
var tab_text="<table border='2px'><tr bgcolor='#87AFC6'>";
var textRange; var j=0;
tab = document.getElementById('headerTable'); // id of table

for(j = 0 ; j < tab.rows.length ; j++) 
{     
    tab_text=tab_text+tab.rows[j].innerHTML+"</tr>";
    //tab_text=tab_text+"</tr>";
}

tab_text=tab_text+"</table>";
tab_text= tab_text.replace(/<A[^>]*>|<\/A>/g, "");//remove if u want links in your table
tab_text= tab_text.replace(/<img[^>]*>/gi,""); // remove if u want images in your table
tab_text= tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); // reomves input params

var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE "); 

if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))      // If Internet Explorer
{
    txtArea1.document.open("txt/html","replace");
    txtArea1.document.write(tab_text);
    txtArea1.document.close();
    txtArea1.focus(); 
    sa=txtArea1.document.execCommand("SaveAs",true,"data.xls");
}  
 else{}             
    var a = document.createElement('a');
    a.href = 'data:application/vnd.ms-excel,' +  encodeURIComponent(tab_text);
    a.download = 'rxe_data' + '.xls';
    a.click();
}
Keval
  • 1,857
  • 16
  • 26
  • You get the prompt because you are opening an HTML file with an .xls extension and not a native .xls file. In your case, Excel can understand the contents and therefore you can see the file. If you tried to rename a .pdf file with a .xls extension you will get the same message but you will not have the same result on opening the file. – wf4 Jul 30 '15 at 08:04
  • thanx @wf4 for this info and i i got your point, but is there any way so i dont get this prompt ? – Keval Jul 30 '15 at 08:36
  • If its for a web download then that will depend on what server languages you are using e.g. PHP, C# etc. [Google Search](https://www.google.co.uk/#q=create+xls+file) – wf4 Jul 30 '15 at 08:43
  • @wf4 i am using java at server side, but it will be great if i can do it on client side as all stuff is done just issue is of this prompt. – Keval Jul 30 '15 at 08:51
  • You can try SheetJS. You can refer to my answer [here](https://stackoverflow.com/a/52712803/3967044) – Kevin Ng Oct 09 '18 at 03:16

2 Answers2

2

In the end it seems that it isn't possible to bypass this alert. If I convert HTML code to excel data that does open in excel, so I implemented server side code to generate pure excel and return that to the client instead of HTML data.

SuperBiasedMan
  • 9,814
  • 10
  • 45
  • 73
Keval
  • 1,857
  • 16
  • 26
1

From what I understand, you are not actually building an .xls file. You are just creating a HTML file with a .xls extension. That's not the same. Excel seems to be able to read your HTML anyway, but it warns you, because the file format and extension don't match.

If you want to build true xls files take a look at various libraries that do that, for example: https://github.com/SheetJS/js-xlsx

ralh
  • 2,514
  • 1
  • 13
  • 19
  • thanks @mromnia, can you give me link for some tutorial or information so i can look at sheetjs for creating excel file from html data or json data ... i have searched on google but i got the stuff for reverse order like import data from excel, but i need to create excel from html or json data. – Keval Jul 30 '15 at 08:54
  • It's really well explained on the github readme, I think. But it is also pretty complicated. Perhaps try making a .csv file? That is much simpler, and Excel will also accept it. Or simply make a .html file, though on most PCs it won't open in Excel by default. – ralh Jul 30 '15 at 09:02