使用javascript / jquery将Html表导出为ex​​cel

时间:2016-02-25 15:05:39

标签: javascript jquery html

最近我创建了一个能够将表数据导出到excel文件的网站。我正在使用插件table2excel,这在Chrome上工作正常,但不能在IE,safari,firefox或任何其他浏览器上工作。我想知道我在这里做错了什么,如果还有其他插件或方法可以做到这一点:

(注意:我正在使用称为刀片的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的:

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

我没有从firefox或safari那里得到任何错误,同时请注意,我并没有要求人们为我编写任何代码,只是推动正确的方向会很棒!

请!非常感谢每一点帮助!

2 个答案:

答案 0 :(得分:0)

不使用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);
}

只需创建一个空白的iframe:

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

调用此功能:

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

link to original post

答案 1 :(得分:0)

我最近遇到了类似的问题,并找到了(大部分)以下代码段:

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();
}

因为我不记得我在哪里找到这个并且没有给予信用到期的信用而感到羞耻......

我不使用很多插件,因此喜欢上面的代码只使用jQuery。我将上述代码用于Internet Explorer 11.对于跨浏览器支持,您需要编辑数据URI部分(快速搜索或搜索数据URI)。

我创建了一个jsfiddle来向您展示它的工作原理。