+ Start a Discussion
manpreetsaini26manpreetsaini26 

Excel exported from vf page containing html table giving some warning

I am exporting a vf page containing html table to excel (.xls) format. But when I open the excel file it gives this warning: "The file format and extension of filename.xls don't match. The file could be corrupted or unsafe. Unless you trust its source, don't open it. Do you want to open it anyway? Yes or No". Now if I press Yes the file opens and has the data as expected. But I don't want the warning to be shown. Here's the code that I have used to export the excel file.

VF Page:

<apex:page standardController="Case" extensions="MyController" action="{!exportExcel}" readOnly="true" contentType="application/vnd.ms-excel#cases.xls" sidebar="false" cache="true">
	<head>
		<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
	</head>
	<table border="1">
		{!header}
		{!excelString}
	</table>
</apex:page>
Apex class code:
public void exportExcel(){
          header ='<tr><td>'+Label1+'</td><td>'+Label2+'</td><td>'+Label3+''</td></tr>';
          excelString = '';
          for(wrapper w : getExportResultsWrapper()){
              excelString += '<tr><td>'+w.CaseNumber+'</td><td>'+w.Type+'</td><td>'+w.ReceivedDate+'</td></tr>';
        }
    }
Why is the warning coming up? What is the best way to avoid the warning? Please help...
pconpcon
If you remove lines 2-4 of your Visualforce page, do you get the same error?
manpreetsaini26manpreetsaini26
Yes, it still gives the same error. And that line is needed because some of the letters to be exported to excel sheet ar french letters. If removed some jibberish gets exported to excel instead but still the error persists. Even if you remove head and code behind and just simply pass a table using <html><body> tags, the error still comes up.