admin管理员组文章数量:1425142
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();
}
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();
}
Share
Improve this question
asked Jul 30, 2015 at 7:56
KevalKeval
1,85917 silver badges27 bronze badges
5
- 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 Commented Jul 30, 2015 at 8:04
- thanx @wf4 for this info and i i got your point, but is there any way so i dont get this prompt ? – Keval Commented Jul 30, 2015 at 8: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 – wf4 Commented Jul 30, 2015 at 8: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 Commented Jul 30, 2015 at 8:51
- You can try SheetJS. You can refer to my answer here – Kevin Ng Commented Oct 9, 2018 at 3:16
2 Answers
Reset to default 2In 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.
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./SheetJS/js-xlsx
版权声明:本文标题:javascript - Export html to Excel : when try to open file it gives prompt like file is corrupted - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745348441a2654611.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论