最近开始学习.net,感觉还不错,写了一个简单的web网页显示数据导出!如有不好的地方,还请指出。
废话就不多说了,请往下看。
前台按钮:
<asp:LinkButton ID="btnxls" runat="server" onclick="btnxls_Click"> <span class="iconbg tableExport"></span>导出数据</asp:LinkButton>
web后台代码:
1 /// <summary> 2 /// 导出数据 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 protected void btnxls_Click(object sender, EventArgs e) 7 { 8 IList<RJ.Model.perms_data_license> license = Business.UserInfo.GetDataLicenseXls(); 9 System.Text.StringBuilder sb = new System.Text.StringBuilder(@" 10 11 <html> 12 <body> 13 <style> 14 td{border: 1px solid #000 ;} 15 </style> 16 <table><tr><th>键</th><th>值</th><th>权限名称</th><th>权限描述</th></tr>"); 17 for (int i = 0; i < license.Count; i++) 18 { 19 sb.Append("<tr>"); 20 sb.Append(" <th>" + license[i].data_license_key + "</th>"); 21 sb.Append(" <th>" + license[i].data_license_value + "</th>"); 22 sb.Append(" <th>" + license[i].data_license_name + "</th>"); 23 sb.Append(" <th>" + license[i].data_license_desc + "</th>"); 24 sb.Append("</tr>"); 25 } 26 sb.Append("</table></body></html>"); 27 Response.Clear(); 28 Response.Write(sb.ToString()); 29 Response.Buffer = true; 30 Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString 31 ("yyyyMMdd") + ".xls"); 32 Response.ContentEncoding = System.Text.Encoding.Default; 33 Response.ContentType = "application/vnd.ms-excel"; 34 Response.End(); 35 }
之后在 GetDataLicenseXls()方法里面写sql语句,搞定!