导出页面数据Excel
2010-07-30 09:29
260 查看
/// <summary>
/// ExcelHtml脚本替换
/// </summary>
/// <param name="Htmlstring"></param>
/// <returns></returns>
public static string NoHTML(string Htmlstring)
{
//删除脚本
Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
//删除HTML
Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"([/r/n])[/s]+", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "/"", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "/xa1", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "/xa2", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "/xa3", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "/xa9", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"(/d+);", "", RegexOptions.IgnoreCase);
Htmlstring.Replace("<", "");
Htmlstring.Replace(">", "");
Htmlstring.Replace("/r/n", "");
Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
return Htmlstring;
}
//点击事件
private void GetExcel()
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);
//tabhtml是服务器名称一个层
tabhtml.RenderControl(htw);
string strLoadInfo = sb.ToString();
strLoadInfo = strLoadInfo.Replace(" ", "").Replace("/n", "").Replace("/r", "").Replace("/r/n", "");
strLoadInfo = strLoadInfo.Replace("</td></tr>", "/n");
strLoadInfo = strLoadInfo.Replace("</td>", ",");
strLoadInfo = NoHTML(strLoadInfo);
string temp = string.Format("attachment;filename={0}", "ExportData.csv");
Response.ClearHeaders();
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.AppendHeader("Content-disposition", temp);
Response.Write(strLoadInfo);
Response.End();
}
/// ExcelHtml脚本替换
/// </summary>
/// <param name="Htmlstring"></param>
/// <returns></returns>
public static string NoHTML(string Htmlstring)
{
//删除脚本
Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
//删除HTML
Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"([/r/n])[/s]+", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "/"", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "/xa1", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "/xa2", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "/xa3", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "/xa9", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"(/d+);", "", RegexOptions.IgnoreCase);
Htmlstring.Replace("<", "");
Htmlstring.Replace(">", "");
Htmlstring.Replace("/r/n", "");
Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
return Htmlstring;
}
//点击事件
private void GetExcel()
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);
//tabhtml是服务器名称一个层
tabhtml.RenderControl(htw);
string strLoadInfo = sb.ToString();
strLoadInfo = strLoadInfo.Replace(" ", "").Replace("/n", "").Replace("/r", "").Replace("/r/n", "");
strLoadInfo = strLoadInfo.Replace("</td></tr>", "/n");
strLoadInfo = strLoadInfo.Replace("</td>", ",");
strLoadInfo = NoHTML(strLoadInfo);
string temp = string.Format("attachment;filename={0}", "ExportData.csv");
Response.ClearHeaders();
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.AppendHeader("Content-disposition", temp);
Response.Write(strLoadInfo);
Response.End();
}
相关文章推荐
- 在页面中的数据导出excel操作
- 将ASP.NET页面内的数据导出到Excel或Word中
- 在aspx页面中将DataTable中的数据通过GridView导出到Excel中
- Vue2.0实现将页面中表格数据导出excel的实例
- jsp页面表格数据导出到excel
- jsp页面导出excel,部分数据乱码
- Web页面的数据导出excel时的格式问题
- Web页面的数据导出excel时的格式问题(长数字显示为科学计数法格式等)
- aspx页面中,DataTable中的数据导出到Excel文件中
- [转载]jsp页面显示数据导出到excel表中
- 将ASP.NET页面内的数据导出到Excel或Word中
- 将页面的列表数据导出到excel文件中
- 如何避免JSP/PHP等页面数据导出EXCEL时出现科学计算法
- asp 页面数据导出为word或者excel
- Web页面的数据导出excel时的格式问题
- 导出页面的数据至EXCEL
- javascript调用activeXObject 导出页面表格数据到excel
- jsp页面数据导出到excel和word
- jsp页面查询的数据导出到excel
- 页面数据导出到Excel或者Word方法