简体   繁体   English

如何使用其他数据导出HTML表

[英]How to Export HTML table with additional data

I have a HTML table inside div and have some more data inside span element 我在div有一个HTML表,并在span元素中有更多数据

what i am trying to do is export all whole div container to excel,but it is only exporting table 我想要做的是将所有整个div容器导出到excel,但它只是导出表

i am using table2excel plugin to export the table 我正在使用table2excel插件导出表

My code 我的代码

 $("#export-btn").click(function() { $("#printFull").table2excel({ filename: "FileName" }); }); var tableValue = [{ "Code": "1040", "Item Name": "VEG CHESSE SANDWICH PACKED ", "UOM": "NOS", "TO Qty": "2.0000", "AcceptedQty": "1.0000" }, { "Code": "1115", "Item Name": "CHICKEN MAYO S/W (CUT INTO 2)", "UOM": "NOS", "TO Qty": "4.0000", "AcceptedQty": "4.0000" }, { "Code": "1119", "Item Name": "VEGETABLE BURGER ", "UOM": "NOS", "TO Qty": "2.0000", "AcceptedQty": "2.0000" }, { "Code": "1120", "Item Name": "CHICKEN BURGER", "UOM": "NOS", "TO Qty": "2.0000", "AcceptedQty": "2.0000" }, { "Code": "1053", "Item Name": "PUNJABI SAMOSA", "UOM": "NOS", "TO Qty": "8.0000", "AcceptedQty": "8.0000" }, { "Code": "1513", "Item Name": "CHOCOLATE CUP CAKE", "UOM": "NOS", "TO Qty": "3.0000", "AcceptedQty": "3.0000" }, { "Code": "1514", "Item Name": "RED VELVETTE CUP CAKE", "UOM": "NOS", "TO Qty": "3.0000", "AcceptedQty": "3.0000" } ] function addTableAcceptance(tableValue) { var col = Object.keys(tableValue[0]); var countNum = col.filter(i => !isNaN(i)).length; var num = col.splice(0, countNum); col = col.concat(num); var table = document.createElement("table"); var tr = table.insertRow(-1); for (var i = 0; i < col.length; i++) { var th = document.createElement("th"); th.innerHTML = col[i]; tr.classList.add("text-center"); tr.appendChild(th); } for (var i = 0; i < tableValue.length; i++) { tr = table.insertRow(-1); for (var j = 0; j < col.length; j++) { var tabCell = tr.insertCell(-1); var tabledata = tableValue[i][col[j]]; if (tabledata && !isNaN(tabledata)) { tabledata = parseInt(tabledata) } tabCell.innerHTML = tabledata; if (col[j] === 'TO Qty' || col[j] === 'AcceptedQty') { tabCell.classList.add("text-right"); tabCell.classList.add("test"); // this the class given by me to these two columns } } } var divContainer = document.getElementById("tableID"); divContainer.innerHTML = ""; divContainer.appendChild(table); table.classList.add("table"); table.classList.add("table-striped"); table.classList.add("table-bordered"); table.classList.add("table-hover"); } addTableAcceptance(tableValue) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css"> <script src="https://cdn.rawgit.com/rainabba/jquery-table2excel/1.1.0/dist/jquery.table2excel.min.js"></script> <div class="table-responsive"> <div id="printFull"> <span id="printOutlet">Vivek</span> <span id="printOutletCode">kumar</span> <span id="toNumber">hwellow</span> <span id="toDate">test</span> <span id="printDateTime">test1</span> <table id=tableID></table> </div> </div> <button id="export-btn" class="btn btn-default commonButton"> Export </button> 

As you all can see i have a div element id as printFull inside which i have some span elements and the my table i want to export the whole div printFull as it is 你们都可以看到我有一个div元素id作为printFull里面我有一些span元素和我的表我想导出整个div printFull因为它是

but it is only exporting table 但它只是导出表

here i am using table2excel plugin if there is any other way to resolve this then i am open to that also 在这里我使用table2excel插件,如果有任何其他方法来解决这个,那么我也是开放的

I have not personally used the table2excel plugin, but if it is exactly what it's name implies, then it probably only supports table elements, in which case it may be better for you to change your div into a table, but that also depends on whether the plugin can interpret nested table data. 我没有亲自使用table2excel插件,但如果它正是它的名字所暗示的,那么它可能只支持table元素,在这种情况下你可能更好地将你的div更改为表,但这也取决于是否该插件可以解释嵌套的表数据。

Another approach would be to serialize your HTML into another data format (I would recommend JSON) that can be imported into Excel through another means. 另一种方法是将HTML序列化为另一种可以通过其他方式导入Excel的数据格式(我建议使用JSON)。 I know of numerous ways to achive this using C#, but unfortunately I cannot be of much more help using Javascript or JQuery. 我知道有很多方法可以使用C#实现这一点,但不幸的是,使用Javascript或JQuery我无法提供更多帮助。

Hope this helps. 希望这可以帮助。

Try this 尝试这个

 <table id="printFull"> <tr> <td> <span id="printOutlet">Vivek</span> <span id="printOutletCode">kumar</span> <span id="toNumber">hwellow</span> <span id="toDate">test</span> <span id="printDateTime">test1</span> </td> </tr> <tr> <td> <table id=tableID></table> </td> </tr> </table> 

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

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