`
$("#btnAdd").click(function () {
var $tr = $("<tr>");
$tr.append("<td>Ravi</td><td>50</td><td>G@gmail.com</td><td>1000000</td>");
$("#tbl tbody").append($tr);
$("#txthidden").val($("#divTable").html());
$("#txthidden").val().replace("<table>", "swe");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<div id="divtbl" runat="server">
<table id="tbl">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Mail</th>
<th>Number</th>
</tr>
</thead>
<tbody>
<tr>
<td>Raj</td>
<td>25</td>
<td>Raj@gmail.com</td>
<td>9999999</td>
</tr>
<tr>
<td>Gopu</td>
<td>29</td>
<td>Goups@gmail.com</td>
<td>9749612767</td>
</tr>
</tbody>
</table>
<input type="button" id="btnExport" runat="server" value="Export"/>
<input type="button" id="btnAdd" runat="server" value="Add Row"/>
<input type="hidden" id="txthidden" runat="server" />
`我有一个封装在div标签内的html表。我需要将数据导出到excel。为此,我发布了页面,并在其帖子上放了一些c#代码用于导出相同的内容。为了得到表格数据,我使用了代码
Response.ContentType = "application/force-download";
Response.AddHeader("content-disposition", "attachment; filename=Print.xls");
Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
Response.Write("<head>");
Response.Write("<META http-equiv=\"Content-Type\" content=\"text/html; charset=utf- 8\">");
Response.Write("<!--[if gte mso 9]><xml>");
Response.Write("<x:ExcelWorkbook>");
Response.Write("<x:ExcelWorksheets>");
Response.Write("<x:ExcelWorksheet>");
Response.Write("<x:Name>Report Data</x:Name>");
Response.Write("<x:WorksheetOptions>");
Response.Write("<x:Print>");
Response.Write("<x:ValidPrinterInfo/>");
Response.Write("</x:Print>");
Response.Write("</x:WorksheetOptions>");
Response.Write("</x:ExcelWorksheet>");
Response.Write("</x:ExcelWorksheets>");
Response.Write("</x:ExcelWorkbook>");
Response.Write("</xml>");
Response.Write("<![endif]--> ");
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
divTable.RenderControl(hw);
Response.Write(tw.ToString());
Response.Flush();
Response.End();
这很好。但是如果我使用jquery在表中添加一行,则在回发期间不会在服务器端反映出来。我尝试了另一种方法,将表的内部Html存储到隐藏文本框并发布表单。但最终显示服务器错误消息“从客户端检测到潜在危险的Request.Form值”。
有没有办法完成我打算做的事情
<div id="divtbl" runat="server">
<table id="tbl">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Mail</th>
<th>Number</th>
</tr>
</thead>
<tbody>
<tr>
<td>Raj</td>
<td>25</td>
<td>Raj@gmail.com</td>
<td>9999999</td>
</tr>
<tr>
<td>Gopu</td>
<td>29</td>
<td>Goups@gmail.com</td>
<td>9749612767</td>
</tr>
</tbody>
</table>