html 以 .xls 扩展名在 javascript 或 php 中导出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16425270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 04:36:25  来源:igfitidea点击:

html to excel export with .xls extension in javascript or php

phpjavascript

提问by Matrix123

I need export html table to excel with .xls extension in javascriptor php

我需要在javascriptphp 中使用 .xls 扩展名导出 html 表

I am using below code but it does not export to file with .xls extension If possible in php code embedded in javascript code then its fine.

我正在使用下面的代码,但它不会导出到带有 .xls 扩展名的文件 如果可能的话,在嵌入在 javascript 代码中的 php 代码中,那很好。

Linkto fiddle.

链接到小提琴。

var tableToExcel = (function() {
 var uri = 'data:application/vnd.ms-excel;base64,', 
 template = '<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">
 <head>
 <!--[if gte mso 9]>
 <xml>
 <x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>     
 <x:Name>
  {worksheet}
 </x:Name>
 <x:WorksheetOptions>
  <x:DisplayGridlines/>
 </x:WorksheetOptions>
 </x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook>
 </xml>
 <![endif]-->
 </head>
 <body>
 <table>{table}</table>
 </body>
 </html>'
 ,base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))); }
 ,format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p];}) }
 return function(table, name) {
  if (!table.nodeType) table = document.getElementById(table)
   var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
   window.location.href = uri + base64(format(template, ctx)) 
 }
});

回答by Elavarasan M Lee

In you Current File:

在您当前文件中:

<?php
  //Your Table code.
  <a href="http://your_site_url.com/export_excel.php" target="_blank">
   <input id="export-btn" type="button" value="Export as Excel" onclick="export()"/>
  </a>
?>

Inside export_excel.php

里面export_excel.php

<?php
  $filename = 'Youe_Filename_without_extension';
  header('Content-type: application/vnd.xls');
  header('Content-Disposition: attachment; filename="'.$filename.'.xls"');
  //Your Table code.
?>

Example Url: Demo

示例网址:演示



Update:更新:


如果你想把它放在同一个文件中 [to avoid duplication of the same code避免重复相同的代码],然后下面的代码会有所帮助:

<?php
$same_page = $_POST['same-page'];
if(!empty($same_page) && $same_page == 1) {
  $filename = 'Sample Table';
  header('Content-type: application/vnd.xls');
  header('Content-Disposition: attachment; filename="'.$filename.'.xls"');
}?>
  //Your Table code.
<?php if(empty($same_page)): ?>
  //write whatever you want to hide in excel like export button,heading etc.
 <form method="POST" action="" target="_balnk">
   <input type="hidden" name="same-page" value="1"/>
   <input id="export-btn" type="submit" value="Export as Excel on Form Submit"/>
 </form>
<?php endif; ?>

回答by dandavis

using a download lib from http://danml.com/js/download.js, it's easy.

使用来自http://danml.com/js/download.js的下载库,这很容易。

function download(strData, strFileName, strMimeType) {
    var D = document,
        A = arguments,
        a = D.createElement("a"),
        d = A[0],
        n = A[1],
        t = A[2] || "text/plain";

    //build download link:
    a.href = "data:" + strMimeType + "," + escape(strData);


    if (window.MSBlobBuilder) {
        var bb = new MSBlobBuilder();
        bb.append(strData);
        return navigator.msSaveBlob(bb, strFileName);
    } /* end if(window.MSBlobBuilder) */



    if ('download' in a) {
        a.setAttribute("download", n);
        a.innerHTML = "downloading...";
        D.body.appendChild(a);
        setTimeout(function() {
            var e = D.createEvent("MouseEvents");
            e.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
            a.dispatchEvent(e);
            D.body.removeChild(a);
        }, 66);
        return true;
    } /* end if('download' in a) */
    ; //end if a[download]?

    //do iframe dataURL download:
    var f = D.createElement("iframe");
    D.body.appendChild(f);
    f.src = "data:" + (A[2] ? A[2] : "application/octet-stream") + (window.btoa ? ";base64" : "") + "," + (window.btoa ? window.btoa : escape)(strData);
    setTimeout(function() {
        D.body.removeChild(f);
    }, 333);
    return true;
} /* end download library function () */

// code that implements OP's functionality:

// 实现 OP 功能的代码:

 function tableToExcel (table) {
   if (!table.nodeType) table = document.getElementById(table);
   download(table.outerHTML, "table.xls", "application/vnd.ms-excel");
 }

works in IE10, FF, and Chrome, maybe others as well.

适用于 IE10、FF 和 Chrome,也许其他也适用。