asp.net 记录一下教常用的代码吧 记忆力太差了
2013-06-06 09:53
459 查看
A标签防止中文乱码
获取后台的值...经常忘啊
调用后台方法
从字符串中获取图片路径
向DataTable不够8行的话,凑足8行数据
DataTable 导出Excel 自定义文件名
<a href="KSXX_List.aspx?type=<%=Server.UrlEncode("政务管理") %>&id=T1" target="_blank" class="ablud">政务管理</a>
获取后台的值...经常忘啊
<a href='<%=imgLink[0] %>' .....
调用后台方法
<img alt="" src='<%#getImageUrl(Eval("L_CONTENT").ToString()) %>' width="160" height="120" border="0" />
从字符串中获取图片路径
/// <summary> /// 从字符串中获取需要的图片地址 /// </summary> /// <param name="source">字符串</param> /// <returns>需要的图片地址</returns> public string getImageUrl(string source) { ArrayList list = new ArrayList(); string result = ""; string pattern = "<img[^<>]*?\\ssrc=['\"]?(.*?)['\"].*?>"; Regex reg = new Regex(pattern); MatchCollection mc = reg.Matches(source); foreach (Match m in mc) { list.Add(m.Groups[1].Value); } if (list.Count > 0) { string s = list[0].ToString().TrimStart('/'); result = s.Substring(s.IndexOf('/') + 1); } else { result = "images/noPic.jpg"; } return result; }
向DataTable不够8行的话,凑足8行数据
DataSet ds = dm.getsql(sql); //数据量少 加空行 if (ds.Tables[0].Rows.Count < 8) { int dtRows = ds.Tables[0].Rows.Count;//现在的行数 int forno = 8 - dtRows; for (int i = 0; i < forno; i++) { DataRow dr = ds.Tables[0].NewRow(); ds.Tables[0].Rows.Add(dr); } }
DataTable 导出Excel 自定义文件名
protected void btnExportExcel_Click(object sender, EventArgs e) { DataTable dt = VSDataSet.Tables[0]; dt.Columns.Remove("WKKID"); DataTable4Excel(dt, "Excel"); } /// <summary> /// Datatable to Excel带自定文件名的导出 /// </summary> /// <param name="dt"></param> /// <param name="FileName"></param> private void DataTable4Excel(System.Data.DataTable dt, string FileName) { System.IO.StringWriter stringWriter = new System.IO.StringWriter(); HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter); DataGrid excel = new DataGrid(); System.Web.UI.WebControls.TableItemStyle headerStyle = new TableItemStyle(); headerStyle.BackColor = System.Drawing.Color.LightGray; headerStyle.Font.Bold = true; headerStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center; excel.HeaderStyle.MergeWith(headerStyle); excel.GridLines = GridLines.Both; excel.HeaderStyle.Font.Bold = true; excel.DataSource = dt.DefaultView; //输出DataTable的内容 excel.DataBind(); excel.RenderControl(htmlWriter); HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename='" + FileName + ".xls"); HttpContext.Current.Response.Charset = "UTF-8"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); HttpContext.Current.Response.ContentType = ".xls"; HttpContext.Current.Response.Write(stringWriter.ToString()); HttpContext.Current.Response.End(); }
相关文章推荐
- 记录一下ASP.NET上传代码
- ASP.NET 常用的33种代码(转,收藏一下,以备后查)
- ASP.NET 常用的33种代码(转,收藏一下,以备后查)
- ASP.NET 常用的33种代码(转,收藏一下,以备后查)
- ASP.NET 常用的33种代码(转,收藏一下,以备后查)
- ASP.NET 常用的33种代码(转,收藏一下,以备后查)
- ASP.NET程序中常用的三十三种代码(第三页)
- ASP.NET程序中常用编程代码(一)
- ASP.NET常用代码
- Asp.net常用代码
- ASP.NET程序中常用代码汇总(五)
- ASP.NET_程序中常用的三十三种代码_
- ASP.NET中常用功能代码总结(6)——XML文件操作篇
- ASP.NET程序中常用代码汇总(四)
- ASP.NET程序中常用代码汇总------4
- ASP.NET程序中常用的三十三种代码(3)
- Asp.net常用的51个代码(非常实用) [转]
- ASP.NET程序中常用的三十三种代码
- ASP.NET程序中常用代码汇总(五)
- ASP.NET 程序中常用的三十三种代码