In my asp.Net MVC 4 project, I use the Excel export function, which exports the HTML table to an Excel file from the client side using Javascript.
Export functionality works fine in Chromeand out Firefox, but doesn't work in IE(any browser). In IE, it simply opens a new window and nothing happens.
My javascript code is below
function Export(htmltable,filename) {
var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>";
excelFile += "<head>";
excelFile += "<!--[if gte mso 9]>";
excelFile += "<xml>";
excelFile += "<x:ExcelWorkbook>";
excelFile += "<x:ExcelWorksheets>";
excelFile += "<x:ExcelWorksheet>";
excelFile += "<x:Name>";
excelFile += "{worksheet}";
excelFile += "</x:Name>";
excelFile += "<x:WorksheetOptions>";
excelFile += "<x:DisplayGridlines/>";
excelFile += "</x:WorksheetOptions>";
excelFile += "</x:ExcelWorksheet>";
excelFile += "</x:ExcelWorksheets>";
excelFile += "</x:ExcelWorkbook>";
excelFile += "</xml>";
excelFile += "<![endif]-->";
excelFile += "</head>";
excelFile += "<body>";
excelFile += htmltable.replace(/"/g, '\'');
excelFile += "</body>";
excelFile += "</html>";
var base64data = "base64," + $.base64.encode(excelFile);
window.open('data:application/vnd.ms-excel;'+ base64data);
}
I also tried to name the file, for example:
window.open('data:application/vnd.ms-excel;filename=' + filename + ';' + base64data);
but still his name is called "download.xls".
How to fix these problems.
I also found these statements,
If you are targeting Internet Explorer as a browser, you need to look for a different approach, as the current one will not work. From the MSDN library, the Data Protocol section says:
Data URIs are supported only for the following elements and/or attributes.
object (images only)
img
input type=image
link
CSS declarations that accept a URL, such as background, backgroundImage,
and so on.
Data URIs can be nested.
For security reasons, data URIs are restricted to downloaded resources. Data URIs cannot be used for navigation, for scripting, or to populate frame or iframe elements.
Any approach to overcome this?
.