2015-02-07 12 views
6

tôi đã sử dụng mã jquery để xuất dữ liệu bảng vào tệp excel, nhận mã từ hereCung cấp tên tệp tùy chỉnh không hoạt động trong table2excel jquery?

nhưng cung cấp tên tệp theo yêu cầu không hoạt động là lấy tên tệp ngẫu nhiên.
cách cung cấp tên tệp tùy chỉnh từ mã.

<script> 
$(function() { 
     $("button").click(function(){ 
     $("#example").table2excel({ 
       exclude: ".noExl", 
     name: "Employee" 
     }); 
     }); 
}); 
</script> 

Trả lời

0

Biến trong plugin này đề cập đến tên của trang tính chứ không phải tên tệp Excel.

Nếu bạn muốn thay đổi tên tệp, bạn sẽ phải hack mã plugin một chút hoặc chỉ sử dụng một plugin hoặc đoạn mã khác phù hợp với nhu cầu của bạn, chẳng hạn như this one (nơi bạn có thể đặt tên tệp trong biến số postfix).

+0

Bạn sẽ phải thêm một vài dòng html vào đầu div chứa bảng của bạn. Xem [this] (http://forums.codecharge.com/posts.php?post_id=107155) để biết thêm thông tin. – Linostar

0

Bạn có thể hack table2excel jquery cho biết tên tùy chỉnh để tải về như sau:

Từ js của bạn:

<script> 
 
$(function() { 
 
     $("button").click(function(){ 
 
     $("#example").table2excel({ 
 
       exclude: ".noExl", 
 
     name: "Employee.txt" //This name will be passed for download 
 
     }); 
 
     }); 
 
}); 
 
</script>

Sau đó thay đổi getFileName gọi (e.settings) trong table2excel .js để đặt tên như sau:

if (typeof msie !== "undefined" && msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))  // If Internet Explorer 
 
      { 
 
       if (typeof Blob !== "undefined") { 
 
        //use blobs if we can 
 
        fullTemplate = [fullTemplate]; 
 
        //convert to array 
 
        var blob1 = new Blob(fullTemplate, { type: "text/html" }); 
 
        window.navigator.msSaveBlob(blob1, name); // Changed Here 
 
       } else { 
 
        //otherwise use the iframe and save 
 
        //requires a blank iframe on page called txtArea1 
 
        txtArea1.document.open("text/html", "replace"); 
 
        txtArea1.document.write(e.format(fullTemplate, e.ctx)); 
 
        txtArea1.document.close(); 
 
        txtArea1.focus(); 
 
        sa = txtArea1.document.execCommand("SaveAs", true, name); // Changed Here 
 
       } 
 

 
      } else { 
 
       link = e.uri + e.base64(e.format(fullTemplate, e.ctx)); 
 
       a = document.createElement("a"); 
 
       a.download = name; // Changed Here 
 
       a.href = link; 
 

 
       document.body.appendChild(a); 
 

 
       a.click(); 
 

 
       document.body.removeChild(a); 
 
      }

0

1.Open jquery.table2excel.js

2. Tìm function getFileName(settings)

3.Change nó để

function getFileName(settings) { 
     return (settings.filename ? settings.filename : settings.name) + 
       (settings.fileext ? settings.fileext : ".xls"); 
} 

settings.name có thể thay đổi từ kịch bản tùy chỉnh của bạn js khi u gọi jquery2excel Trong ví dụ của tôi, có vẻ như

$(".generateXLS").click(function(){ 
    var idOfTable = $(this).attr("data-rel"); 
    var tableName = $(this).attr("data-table-name"); 
    $("#tableN"+idOfTable).table2excel({ 
     name: tableName 
    }); 
}); 
Các vấn đề liên quan