有一个div标签
<div>
</div>
加载时后台动态的在div里面生成了一个 table 如何把这个table导出成excel表格
用NPOI,去github找下
google C# 导出Excel
注意excel不要写错了。
用npoi吧~原生.net支持
http://npoi.codeplex.com/
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.IO; 6 using System.Data; 7 using Bussiness; 8 using Models; 9 using System.Text; 10 namespace _5vClient 11 { 12 /// <summary> 13 /// DownLoad 的摘要说明 14 /// </summary> 15 public class DownLoad : IHttpHandler 16 { 17 /// <summary> 18 /// 导出会员信息 19 /// </summary> 20 /// <param name="context"></param> 21 public void ProcessRequest(HttpContext context) 22 { 23 string info = context.Request.QueryString["info"]; 24 MemberMgr bll = new MemberMgr(); 25 context.Response.ContentType = "application/octet-stream";
//导出excel后缀名就xls,导出word的话就改后缀名 26 context.Response.AddHeader("content-disposition", "attachment;filename=要导出的表名.xls"); 27 28 List<Excel_MemberInfo> memlist = bll.ExportMemberInfo(info); 29 StringBuilder sb = new StringBuilder(); 30 context.Response.Write("<table border='1'cellspacing='0' cellpadding='0'>"); 31 context.Response.Write("<tr>"); 32 List<string> list = new List<string> { "序号", "会员卡号", "会员名称", "密码", "手机", "电话", "性别", "身份证", "会员生日", "地址", "邮编", "邮箱", "照片", "有效期", "备注", "所属店铺", "推荐人卡号", "提成人", "余额", "积分", "等级", "会员卡状态", "注册日期", "锁定会员等级", "是否可用", "操作人", "操作时间", "操作IP地址" }; 33 ////第一行 34 foreach (var item in list) 35 { 36 context.Response.Write("<td style='font-size:14px;text-align:center;'>" + item + "</td>"); 37 } 38 context.Response.Write("</tr>"); 39 40 41 //获取的参数列表绑定 42 foreach (var item in memlist) 43 { 44 context.Response.Write("<tr>"); 45 context.Response.Write("<td>" + item.MemberID + "</td>"); 46 context.Response.Write("<td>" + item.MemberNO + "</td>"); 47 context.Response.Write("<td>" + item.MemberName + "</td>"); 48 context.Response.Write("<td>" + item.Password + "</td>"); 49 context.Response.Write("<td>" + item.MemberPhone + "</td>"); 50 context.Response.Write("<td>" + item.MemberTel + "</td>"); 51 context.Response.Write("<td>" + item.MemberSex + "</td>"); 52 context.Response.Write("<td>" + item.MemberNumber + "</td>"); 53 context.Response.Write("<td>" + item.MemberBirthday + "</td>"); 54 context.Response.Write("<td>" + item.Province + "" + item.City + "" + item.District + "" + item.MemberAddress + "</td>"); 55 context.Response.Write("<td>" + item.MemberZip + "</td>"); 56 context.Response.Write("<td>" + item.MemberEmail + "</td>"); 57 context.Response.Write("<td>" + item.MemberPicture + "</td>"); 58 context.Response.Write("<td>" + item.MemberPeriod + "</td>"); 59 context.Response.Write("<td>" + item.MemberRemarks + "</td>"); 60 context.Response.Write("<td>" + item.CompanyName + "</td>"); 61 context.Response.Write("<td>" + item.MemberReferrerNo + "</td>"); 62 context.Response.Write("<td>" + item.MemberPushMoneyPeople + "</td>"); 63 context.Response.Write("<td>" + item.Balance + "</td>"); 64 context.Response.Write("<td>" + item.Integral + "</td>"); 65 context.Response.Write("<td>" + item.GradeLevelName + "</td>"); 66 context.Response.Write("<td>" + item.Status + "</td>"); 67 context.Response.Write("<td>" + item.MemberDate + "</td>"); 68 context.Response.Write("<td>" + item.Lock + "</td>"); 69 context.Response.Write("<td>" + item.Hide + "</td>"); 70 context.Response.Write("<td>" + item.UserName + "</td>"); 71 context.Response.Write("<td>" + item.OperationDate + "</td>"); 72 context.Response.Write("<td>" + item.OperationIP + "</td>"); 73 context.Response.Write("</tr>"); 74 } 75 context.Response.Write("</table>"); 76 // string str= context.Request.Params["info"]; 77 context.Response.End(); 78 } 79 80 public bool IsReusable 81 { 82 get 83 { 84 return false; 85 } 86 } 87 } 88 }
就是卓样
同意4楼的观点,查询出你想要的数据,如:List<Excel_MemberInfo> memlist = bll.ExportMemberInfo(info);
然后创建表:StringBuilder sb = new StringBuilder(); 30 context.Response.Write("<table border='1'cellspacing='0' cellpadding='0'>"); 31 context.Response.Write("<tr>"); 32 List<string> list = new List<string> { "序号", "会员卡号", "会员名称", "密码", "手机", "电话", "性别", "身份证", "会员生日", "地址", "邮编", "邮箱", "照片", "有效期", "备注", "所属店铺", "推荐人卡号", "提成人", "余额", "积分", "等级", "会员卡状态", "注册日期", "锁定会员等级", "是否可用", "操作人", "操作时间", "操作IP地址" }; 33 ////第一行 34 foreach (var item in list) 35 { 36 context.Response.Write("<td style='font-size:14px;text-align:center;'>" + item + "</td>"); 37 } 38 context.Response.Write("</tr>");,展示出你想要展示的节点;
下一步是将取出来的值循环绑定到对应的节点下;
最后一步下载功能
OLEDB