jquery data to csv

I have this code to open data in Excel, in csv format, seems to work fine in Firefox, does not work in IE 9 or 10.  Is there anything that can be done  ?

        var uri = 'data:application/vnd.ms-excel,' + html;
        var downloadLink = document.createElement("a");
        downloadLink.href = uri;
        downloadLink.download = "Export.csv";
        document.body.appendChild(downloadLink);
        downloadLink.click();
        document.body.removeChild(downloadLink);

Open in new window

DemosthenesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rainer JeschorCommented:
Hi,
sorry but I don't have working code at hand, but the main functionality could be found at the Angular UI GitHub repository:
https://github.com/angular-ui/ui-grid/issues/2312

Please scroll down to the last 3-4 comments.
HTH
Rainer
0
Rainer JeschorCommented:
Hi,

the following code would need a little bit of cleanup but should work in general:

function CreateExcelLikeAngular(fileName, csvContent) {
          var D = document;
          var a = D.createElement('a');
          var strMimeType = 'application/octet-stream;charset=utf-8';
          var rawFile;

          if (!fileName) {
            var currentDate = new Date();
            fileName = "CSV Export - " + currentDate.getFullYear() + (currentDate.getMonth() + 1) +
                          currentDate.getDate() + currentDate.getHours() +
                          currentDate.getMinutes() + currentDate.getSeconds() + ".csv";
          }

          if (isIEBelow10()) {
            var frame = D.createElement('iframe');
            document.body.appendChild(frame);

            frame.contentWindow.document.open("text/html", "replace");
            frame.contentWindow.document.write('sep=,\r\n' + csvContent);
            frame.contentWindow.document.close();
            frame.contentWindow.focus();
            frame.contentWindow.document.execCommand('SaveAs', true, fileName);

            document.body.removeChild(frame);
            return true;
          }

          // IE10+
          if (navigator.msSaveBlob) {
            return navigator.msSaveBlob(new Blob(["\ufeff", csvContent], {
              type: strMimeType
            }), fileName);
          }

          //html5 A[download]
          if ('download' in a) {
            var blob = new Blob([csvContent], {
              type: strMimeType
            });
            rawFile = URL.createObjectURL(blob);
            a.setAttribute('download', fileName);
          } else {
            rawFile = 'data:' + strMimeType + ',' + encodeURIComponent(csvContent);
            a.setAttribute('target', '_blank');
            a.setAttribute('download', fileName);
          }


          a.href = rawFile;
          a.setAttribute('style', 'display:none;');
          D.body.appendChild(a);
          setTimeout(function() {
            if (a.click) {
              a.click();
              // Workaround for Safari 5
            } else if (document.createEvent) {
              var eventObj = document.createEvent('MouseEvents');
              eventObj.initEvent('click', true, true);
              a.dispatchEvent(eventObj);
            }
            D.body.removeChild(a);

          }, 100);
        }
function isIEBelow10() {
	var myNav = navigator.userAgent.toLowerCase();
    return (myNav.indexOf('msie') != -1) ? parseInt(myNav.split('msie')[1]) < 10 : false;
}

Open in new window


HTH
Rainer
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DemosthenesAuthor Commented:
I copy pasted your code as is, and it works.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.