我有4列的列,其中每列包含文本字段和按鈕,每行的末尾都包含編輯和刪除按鈕。 我想將表格導出爲Excel格式,但是當我做文本字段和列標題按鈕和編輯和刪除按鈕也被導出到我不想要的Excel文件。 請問任何人都可以告訴我我在javascript中犯了什麼錯誤。使用javascript將HTML表導入excel
這裏是我的jQuery的代碼,我是從網上(http://jsfiddle.net/insin/cmewv/)
<script type="text/javascript">
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))
}
})()
</script>
我的HTML代碼如下
<TABLE id="table_id" class="display" align="Center" border="1px" width="80%">
<thead>
<tr>
<th> <b>User_ID </th></b>
<form action="SearchId" method="post">
<input type="hidden" name="hiddenname" value="hidden_uid" >
<input type="text" name="uid" id="uid">
<input type="submit" value="Search">
</form>
<th><b>User_Name </th></b>
<form action="SearchId" method="post">
<input type="text" name="uname" id="uname">
<input type="hidden" name="hiddenname" value="hidden_uname" >
<input type="submit" value="Search">
</form>
<th><b>Password</th></b>
<form action="SearchId" method="post">
<input type="text" name="pass" id="pass">
<input type="hidden" name="hiddenname" value="hidden_pass" >
<input type="submit" value="Search">
</form>
<th><b>Designation</th></b>
<form action="SearchId" method="post">
<input type="text" name="desig" id="desig">
<input type="hidden" name="hiddenname" value="hidden_desig" >
<input type="submit" value="Search">
</form>
</thead>
<tbody >
<%Iterator itr;%>
<%List data=(List) request.getAttribute("UserData");
for(itr=data.iterator();itr.hasNext();)
{%>
<tr>
<% String s= (String) itr.next(); %>
<td><%=s %></td>
<td><%=itr.next() %></td>
<td><%=itr.next() %></td>
<td><%=itr.next() %></td>
<form id="edit" action="EditRecord" method="post" >
<td><input type="hidden" name="hidden_edit" id="edit_id" value="<%=s %>"/>
<input type="submit" id="myButton" value="Edit" name="edit" onclick="toggleVisibility('');"> </td>
</form>
<td><form id="delete" action="DeleteRecord" method="post" >
<td><input type="hidden" name="hidden_delete" id="delete_id" value="<%=s %>"/>
<input type="submit" value="delete" name="delete"> </td>
</form></td>
<%} %>
</tr>
</tbody>
</TABLE>
代碼真的很難看。你能清理它嗎?這些浮動逗號是什麼,','?那些應該是';'?您應該用分號「;」來終止javascript語句。 – akgill
@akgill感謝您的回覆。我從網上得到了這段代碼。這是鏈接「http://jsfiddle.net/insin/cmewv/」。它工作得很好,除了它還出口按鈕和tex字段..我可以消除這個..請指導我 – Dinesh
@Thileepan請給我你的HTML代碼。 –