文件格式或文件扩展名无效 [英] File format or File extension not valid

查看:174
本文介绍了文件格式或文件扩展名无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 rainabba/jquery-table2excel 导出html表格转为excel.点击该按钮可下载文件,但excel会报告文件或格式已损坏.请提出建议.

I am using rainabba/jquery-table2excel to export html table to excel .On clicking the button the file gets download but excels reports either the file or format is corrupted.Please suggest.

<html>
<head>
     <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
    <script type="text/javascript" src="jquery-table2excel-master/dist/jquery.table2excel.min.js"></script>

    <script type="text/javascript">
$(document).ready(function(e){
    $("#myButton").click(function (e){
        $(".mytable").table2excel({
            name:"new File",
            filename:"work",
            //fileext:".xlsx"

        });

    });

    });


</script>
</head>
<body>
    <button id="myButton">Click To Download</button>
    <table class="mytable" border="1">
        <tr>
            <th>new1</th>
            <th>new2</th>
            <th>new3</th>
            <th>new4</th>

        </tr>
        <tr>
            <td>Data 1</td>
            <td>Data 2</td>
            <td>Data 3</td>
            <td>Data 4</td>
        </tr>
    </table>
</body>
</html>

推荐答案

<button class="btn btn-default" onclick="tableToExcel(\'#divId\', \'Excel Report\')">Generate Excel</Button>
<div id="divId">
    <table class="mytable" border="1">
        <tr>
            <th>new1</th>
            <th>new2</th>
            <th>new3</th>
            <th>new4</th>

        </tr>
        <tr>
            <td>Data 1</td>
            <td>Data 2</td>
            <td>Data 3</td>
            <td>Data 4</td>
        </tr>
    </table>
</div>

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

使用此代码来生成excel文件.

use this code..for generate excel file..

这篇关于文件格式或文件扩展名无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆