2 javawhy javawhy 于 2015.07.28 09:57 提问

不能用execcommand函数导出除了txt,htm,html,以外的格式。 50C

我的客户端是WIN7,IE8
在用document.execCommand("saveas",true,"name.xls");
导出后的界面,支持的文件格式只有txt,htm,html。
我依照IE7的修改方法:
[HKEY_CLASSES_ROOT.xls] “PerceivedType”=”document”
[HKEY_CLASSES_ROOT.xlt] “PerceivedType”=”document”

还是没用,请大神们帮忙解决。

4个回答

showbo
showbo   Ds   Rxr 2015.07.28 10:23

http://stackoverflow.com/questions/2515791/execcommandsaveas-null-file-csv-is-not-working-in-ie8

最好是上传内容到服务器后输出content-disposition响应头来实现其他文件类型的保存

asp导出excel文件

fandyvon
fandyvon
2 年多之前 回复
javawhy
javawhy   2015.07.28 10:45

走服务端的已经实现了,现在请教的问题是前台js导出问题。
另,服务端下载的弊端是数据量大的话,还得再传输回后台,传回前台。

贴一段代码供大家测试

IE8
<!DOCTYPE html>




EB页面导出为EXCEL文档的方法
<br> function saveCode() {<br> var winname = window.open(&#39;&#39;, &#39;_blank&#39;, &#39;top=10000&#39;);<br> var strHTML = document.all.tableExcel.innerHTML;<br> strHTML = &#39;<html><head>&#39;+<br> &#39;<style type="text/css"></style>&#39;+<br> &#39;</head><body>&#39;+strHTML+&#39;</body></html>&#39;;<br> winname.document.open(&#39;text/html&#39;, &#39;replace&#39;);<br> winname.document.writeln(strHTML);<br> winname.document.execCommand(&#39;saveas&#39;, &#39;&#39;, &#39;excel.xls&#39;);<br> winname.close();<br> }<br>

编号 姓名 年龄 性别
3 张三1 11
2 张三2 11
1 张三3 11
4 张三4 11



save4me
save4me   Ds   Rxr 2015.07.28 11:43

参考 IE9 - no way to save the results of CSV export #2312

        /**
         * @ngdoc function
         * @name isIEBelow10
         * @methodOf  ui.grid.exporter.service:uiGridExporterService
         * @description Checks whether current browser is IE and of version below 10
        */
        isIEBelow10: function () {
            var myNav = navigator.userAgent.toLowerCase();
            return (myNav.indexOf('msie') != -1) ? parseInt(myNav.split('msie')[1]) < 10 : false;
        },

        /**
         * @ngdoc function
         * @name downloadFile
         * @methodOf  ui.grid.exporter.service:uiGridExporterService
         * @description Triggers download of a csv file.  Logic provided
         * by @cssensei (from his colleagues at https://github.com/ifeelgoods) in issue #2391
         * @param {string} fileName the filename we'd like our file to be
         * given
         * @param {string} csvContent the csv content that we'd like to 
         * download as a file
         */
        downloadFile: function (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 (this.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);
        }
javawhy
javawhy   2015.07.29 13:24

感谢save4me

execcommand在IE8下导出是,格式还是只支持txt,htm,html三种。

Csdn user default icon
上传中...
上传图片
插入图片