近日在项目中需要将一个带有超链接的页面导出成Excel,但是导出的文件中要去除这些超链接。
页面主体部分是一个<table>标签,是后台生成的。
我的解决方法如下:
1.取到table 的InnerHtml;
2.将取到的InnerHtml中的超链接全部替换掉;
3.将新的字符串拼接成一个可导出的HTML页,并导出。
第一步就不废话了。
第二步,取到InnerHtml的内容,将其中的超链接替换成空字符。因为要保留带超链接的文本本身,所以这里分两步
1.利用正则表达式替换掉超链接文本前面的超链接部分
2.利用String.Replace替换掉</a>
View Code
1 string result = Regex.Replace(resultHtml, "<a[^>]*>", ""); 2 result = result.Replace("</a>", "");
第三步,拼接HTML页,导出。
原HTML页如下:
View Code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>无标题文档</title> </head> <body> <table id="StatisticTbl" align='center' border='1' style="border-collapse: collapse; line-height:25px;" bordercolor="#111111" > <tr align='center'> <th colspan="9">2012年5月29日到2012年6月28日统计表</th> </tr> <tr> <th align="center"><strong>column1</strong></th> <th align="center"><strong>column2</strong></th> <th align="center"><strong>column3</strong></th> <th align="center"><strong>column4</strong></th> <th align="center"><strong>column5</strong></th> <th align="center"><strong>column6</strong></th> <th align="center"><strong>column7</strong></th> <th align="center"><strong>column8</strong></th> <th align="center"><strong>column9</strong></th> </tr> <tr> <td>row1</td> <td><a href="../StatisticReport/Detail.aspx?typeId=1&statisticType=1" target="_blank">0</a></td> <td><a href="../StatisticReport/Detail.aspx?typeId=1&statisticType=1" target="_blank">4</a></td> <td><a href="../StatisticReport/Detail.aspx?typeId=2&statisticType=1" target="_blank">3</a></td> <td><a href="../StatisticReport/Detail.aspx?typeId=3&statisticType=1" target="_blank">2</a></td> <td><a href="../StatisticReport/Detail.aspx?typeId=4&statisticType=1" target="_blank">4786.24</a></td> <td><a href="../StatisticReport/Detail.aspx?typeId=5&statisticType=1" target="_blank">1</a></td> <td><a href="../StatisticReport/Detail.aspx?typeId=6&statisticType=1" target="_blank">0</a></td> <td><a href="../StatisticReport/Detail.aspx?typeId=7&statisticType=1" target="_blank">4</a></td> </tr> <tr> <td>row1</td> <td><a href="../StatisticReport/Detail.aspx?typeId=1&statisticType=1" target="_blank">1</a></td> <td><a href="../StatisticReport/Detail.aspx?typeId=1&statisticType=1" target="_blank">3</a></td> <td><a href="../StatisticReport/Detail.aspx?typeId=2&statisticType=1" target="_blank">4</a></td> <td><a href="../StatisticReport/Detail.aspx?typeId=3&statisticType=1" target="_blank">2</a></td> <td><a href="../StatisticReport/Detail.aspx?typeId=4&statisticType=1" target="_blank">2873.23</a></td> <td><a href="../StatisticReport/Detail.aspx?typeId=5&statisticType=1" target="_blank">2</a></td> <td><a href="../StatisticReport/Detail.aspx?typeId=6&statisticType=1" target="_blank">0</a></td> <td><a href="../StatisticReport/Detail.aspx?typeId=7&statisticType=1" target="_blank">5</a></td> </tr> </table> </body> </html>
拼接可导出的HTML页,并导出(本段代码转自http://kb.cnblogs.com/a/1339957/)
View Code
1 /// <summary> 2 /// 导出HTML table 内容到Excel 3 /// </summary> 4 /// <param name="str">HTML table标签内容</param> 5 /// <param name="fileName">导出文件名</param> 6 /// <param name="pages"></param> 7 public static void StreamExport(string str, string fileName, System.Web.UI.Page pages) 8 { 9 if (string.IsNullOrEmpty(fileName)) 10 { 11 return; 12 } 13 StringBuilder content = new StringBuilder(); 14 content.Append("<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'>"); 15 content.Append("<head><title></title><meta http-equiv='Content-Type' content=\"text/html; charset=UTF-8\">"); 16 //注意:[if gte mso 9]到[endif]之间的代码,用于显示Excel的网格线,若不想显示Excel的网格线,可以去掉此代码 17 content.Append("<!--[if gte mso 9]>"); 18 content.Append("<xml>"); 19 content.Append(" <x:ExcelWorkbook>"); 20 content.Append(" <x:ExcelWorksheets>"); 21 content.Append(" <x:ExcelWorksheet>"); 22 content.Append(" <x:Name>Sheet1</x:Name>"); 23 content.Append(" <x:WorksheetOptions>"); 24 content.Append(" <x:Print>"); 25 content.Append(" <x:ValidPrinterInfo />"); 26 content.Append(" </x:Print>"); 27 content.Append(" </x:WorksheetOptions>"); 28 content.Append(" </x:ExcelWorksheet>"); 29 content.Append(" </x:ExcelWorksheets>"); 30 content.Append("</x:ExcelWorkbook>"); 31 content.Append("</xml>"); 32 content.Append("<![endif]-->"); 33 34 content.Append("</head><body>"); 35 36 content.Append(str); 37 38 content.Append("</body></html>"); 39 40 pages.Response.Clear(); 41 pages.Response.Buffer = true; 42 pages.Response.ContentType = "application/ms-excel"; //"application/ms-excel"; 43 pages.Response.Charset = "UTF-8"; 44 pages.Response.ContentEncoding = System.Text.Encoding.UTF8; 45 fileName = System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8); 46 pages.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName + ".xls"); 47 pages.Response.Write(content.ToString()); 48 //pages.Response.End(); //注意,若使用此代码结束响应可能会出现“由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值。”的异常。 49 HttpContext.Current.ApplicationInstance.CompleteRequest(); 50 }