需求:把页面中表格table的html代码导出成excel
实现:把table的html代码放入HiddenField中,然后过滤标签并导出
protected void linkExcel_Click(object sender, EventArgs e) { Response.ClearContent(); Response.AddHeader("content-disposition", "attachment; filename=www.fanmulu.cn.csv"); Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); Response.ContentType = "application/excel"; string tab = val_tab.Value.Trim(); tab = tab.Replace("<tbody>", "").Replace("</tbody>", ""); tab = Regex.Replace(tab, "<tr[^>]+>", "<tr>", RegexOptions.IgnoreCase|RegexOptions.Singleline); tab = Regex.Replace(tab, "<td[^>]+>", "<td>", RegexOptions.IgnoreCase|RegexOptions.Singleline); tab = Regex.Replace(tab, "<th[^>]+>", "<th>", RegexOptions.IgnoreCase|RegexOptions.Singleline); tab = tab.Replace("<tr>", "").Replace("</tr>", "\r\n"); tab = tab.Replace("<th>", "\"").Replace("</th>", "\","); tab = tab.Replace("<td>", "\"").Replace("</td>", "\","); tab = tab.Replace("<br>", "/"); tab = tab.Replace(" ", ""); tab = tab.Replace(" ", ""); Response.Write(tab); Response.End(); }