简体   繁体   English

使用javascript / jquery将HTML表格导出到excel

[英]export Html table to excel using javascript/jquery

recently i made a website that has the ability to export the data of a table to an excel file. 最近,我建立了一个网站,能够将表格数据导出到Excel文件。 I am using the plugin table2excel for this which works fine on Chrome but doesn't work on IE, safari, firefox or any other browser for that matter. 我为此使用了table2excel插件,该插件在Chrome上可以正常运行,但在IE,Safari,Firefox和其他任何浏览器上均无法使用。 was wondering if i am doing something wrong here and if there is any other plugin or way to do this: 想知道我在这里做错什么了吗,是否还有其他插件或方法可以做到这一点:

(note: i am using laravels templating engine called blade to include the table2excel plugin) html: (注意:我正在使用称为叶片的laravels模板引擎来包括table2excel插件)html:

<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta charset="UTF-8">
    <title></title>

    <!-- CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
    <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,400italic,700,700italic' rel='stylesheet' type='text/css'>
    {{ HTML::style('css/style.css') }}
    <!-- Javascript -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    {{ HTML::script('js/jquery.table2excel.js') }} 
    {{ HTML::script('js/script.js') }} 

</head>

            <table id="signedinTable" class="peopleTable table table-hover">
                <thead>
                    <tr>
                        <th>Voornaam</th>
                        <th>Achternaam</th>
                        <th>Telefoon nr.</th>
                        <th>E-mail</th>
                        <th>Bedrijf</th>
                        <th>Partner?</th>
                    </tr>
                </thead>

                <tbody>
                    @foreach($results as $result)

                    @if($result->isGoing == 1)

                        <tr class="signedinRow">
                            <td>{{ $result->firstname }}</td>
                            <td>{{ $result->lastname }}</td>
                            <td>{{ $result->phone }}</td>
                            <td>{{ $result->email }}</td>
                            <td>{{ $result->company }}</td>
                            <td class="signedinPartner">{{ $result->partner }}</td>
                        </tr>

                    @endif

                    @endforeach
                </tbody>
            </table>

        </div>
        <button id="signedinExport" class="excl btn btn-primary">Download als excel bestand</button>

jquery: jQuery的:

$("#signedinExport").click(function(){
    $("#signedinTable").table2excel({
        exclude:".noExl",
        name:"Aangemelde mensen",
        filename:"Aangemelde mensen"
    });
});

i am not getting any errors from ie, firefox or safari, also note that i'm not asking for people to write any code for me whatsoever, just a push in the right direction would be awesome! 我没有从firefox或safari中得到任何错误,也请注意,我并没有要求人们为我编写任何代码,只是朝着正确的方向发展就太好了!

please! 请! every bit of help is VERY much appreciated! 非常感谢您的帮助!

Not using the jQuery function, and a bit more code but you could give this a shot: 不使用jQuery函数,还有更多代码,但是您可以尝试一下:

function fnExcelReport()
{
    var tab_text="<table border='2px'><tr bgcolor='#87AFC6'>";
    var textRange; var j=0;
    tab = document.getElementById('headerTable'); // id of table

    for(j = 0 ; j < tab.rows.length ; j++) 
    {     
        tab_text=tab_text+tab.rows[j].innerHTML+"</tr>";
        //tab_text=tab_text+"</tr>";
    }

    tab_text=tab_text+"</table>";
    tab_text= tab_text.replace(/<A[^>]*>|<\/A>/g, "");//remove if u want links in your table
    tab_text= tab_text.replace(/<img[^>]*>/gi,""); // remove if u want images in your table
    tab_text= tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); // reomves input params

    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE "); 

    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))      // If Internet Explorer
    {
        txtArea1.document.open("txt/html","replace");
        txtArea1.document.write(tab_text);
        txtArea1.document.close();
        txtArea1.focus(); 
        sa=txtArea1.document.execCommand("SaveAs",true,"Say Thanks to Sumit.xls");
    }  
    else                 //other browser not tested on IE 11
        sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));  

    return (sa);
}

Just create a blank iframe: 只需创建一个空白的iframe:

<iframe id="txtArea1" style="display:none"></iframe>

Call this function on: 在以下位置调用此函数:

<button id="btnExport" onclick="fnExcelReport();"> EXPORT </button>

link to original post 链接到原始帖子

I recently had a similar issue and found (the majority of) the following code snippet: 我最近遇到了类似的问题,并发现了以下大部分代码(大部分):

function exportTableToCSV($table, filename, answer) {
  var $rows = $table.find('tr'),
    //Temporary delimiter characters unlikely to be typed by keyboard
    //This is to avoid accidentally splitting the actual contents
    tmpColDelim = String.fromCharCode(11), //vertical tab character
    tmpRowDelim = String.fromCharCode(0), //null character
    //actual delimiter characters for CSV format
    colDelim = '","',
    rowDelim = '"\r\n"',
    //Grab text from table into CSV formatted string
    csv = '"' + $rows.map(function(i, row) {
      var $row = $(row),
        $cols = $row.find('th,td');
      return $cols.map(function(j, col) {
        var $col = $(col),
          text = $col.text();
        return text.replace(/"/g, '""'); //escape double quotes
      }).get().join(tmpColDelim);
    }).get().join(tmpRowDelim)
    .split(tmpRowDelim).join(rowDelim)
    .split(tmpColDelim).join(colDelim) + '"';
  //Data URI
  var IEwindow = window.open();
  IEwindow.document.write(csv);
  IEwindow.document.close();
  if (IEwindow.document.execCommand('SaveAs', false, filename + ".csv")) {
    saveAsAnswer = 'saved';
  } else {
    saveAsAnswer = 'not saved';
  }
  IEwindow.close();
}

Shame on me for not remembering where I found this and not giving credit where credit is due... 对我感到羞耻,因为他不记得我在哪里找到了它,并且在应得的信贷额度上没有给予信贷...

I don't use many plugins and therefore love that the above code only uses jQuery. 我使用的插件不多,因此喜欢上面的代码仅使用jQuery。 I use the above code for Internet Explorer 11. For cross-browser support, you'll want to edit the Data URI section (do a quick SO or Google search for Data URIs). 我将上述代码用于Internet Explorer11。为实现跨浏览器的支持,您需要编辑“数据URI”部分(对数据URI进行快速SO或Google搜索)。

I've created a jsfiddle to show you how it works. 我创建了一个jsfiddle来向您展示它的工作方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM