asp.net以流导出Excel
废话不多说,直接上代码
这是点击导出的事件函数,因为我是从前端获取的Table的json数据,所以需要转换一下,大家直接用查询出来的DataTable即可
1 protected void bt_export_ServerClick(object sender, EventArgs e) 2 { 3 DataTable dt = updateInfo(datajson.Value);//将json数据转为DataTable 4 string content = getExcelContent(dt); 5 string css = ".setwidth{width:200px;}";//表格的样式,可自定义 6 string filename = DateTime.Now.ToString("yyyyMMddHHmmssfffff") + ".xls"; 7 8 ExportToExcel(filename, content, css);//调用函数 9 }
然后是转换为DataTable
1 public DataTable getDataTable() 2 { 3 DataTable dt = new DataTable(); 4 dt.Columns.Add("订单号", typeof(System.String)); 5 dt.Columns.Add("提现时间", typeof(System.String)); 6 dt.Columns.Add("提现人姓名", typeof(System.String)); 7 dt.Columns.Add("提现人手机号码", typeof(System.String)); 8 dt.Columns.Add("提现卡号", typeof(System.String)); 9 dt.Columns.Add("提现银行", typeof(System.String)); 10 dt.Columns.Add("账户余额", typeof(System.String)); 11 dt.Columns.Add("提现金额", typeof(System.String)); 12 dt.Columns.Add("手续费", typeof(System.String)); 13 dt.Columns.Add("当前状态", typeof(System.String)); 14 return dt; 15 } 16 17 /// <summary> 18 /// json转换为DataTable 19 /// </summary> 20 /// <param name="json">需要转化的json格式字符串</param> 21 /// <returns></returns> 22 public DataTable updateInfo(string json) 23 { 24 DataTable dt = getDataTable(); 25 System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer(); 26 if (!string.IsNullOrEmpty(json)) 27 { 28 object[] obj = (object[])jss.DeserializeObject(json); 29 Dictionary<string, object> dic; 30 DataRow dr; 31 foreach (object _obj in obj) 32 { 33 dr = dt.NewRow(); 34 dt.Rows.Add(dr); 35 dic = (Dictionary<string, object>)_obj; 36 dr["订单号"] = dic["orderid"]; 37 dr["提现时间"] = dic["createtime"]; 38 dr["提现人姓名"] = dic["cname"]; 39 dr["提现人手机号码"] = dic["cphone"]; 40 dr["提现卡号"] = dic["cno"]; 41 dr["提现银行"] = dic["cbkname"]; 42 dr["账户余额"] = dic["userye"]; 43 dr["提现金额"] = dic["ofee"]; 44 dr["手续费"] = dic["sxfee"]; 45 string status = ""; 46 switch (dic["ostatus"].ToString()) 47 { 48 case "0": 49 status = "审核拒绝"; 50 break; 51 case "1": 52 status = "放款成功"; 53 break; 54 case "2": 55 status = "待审核"; 56 break; 57 case "3": 58 status = "审核通过"; 59 break; 60 case "4": 61 status = "放款失败"; 62 break; 63 } 64 dr["当前状态"] = status; 65 } 66 } 67 return dt; 68 }
最后是实现流导出Excel
1 private string getExcelContent(DataTable dt) 2 { 3 StringBuilder sb = new StringBuilder(); 4 5 sb.Append("<table borderColor='black' border='1' >"); 6 sb.Append("<thead><tr>"); 7 /*for (int j = 0; j < dt.Columns.Count; j++) 8 { 9 sb.Append("<th class='setwidth' bgColor='#ccfefe'>" + dt.Columns[j].ColumnName + "</th>"); 10 }*/ 11 sb.Append("<th style='width:150px;' bgColor='#ccfefe'>订单号</th>"); 12 sb.Append("<th style='width:110px;' bgColor='#ccfefe'>提现时间</th>"); 13 sb.Append("<th style='width:80px;' bgColor='#ccfefe'>提现人姓名</th>"); 14 sb.Append("<th style='width:110px;' bgColor='#ccfefe'>提现人手机号</th>"); 15 sb.Append("<th style='width:140px;' bgColor='#ccfefe'>提现卡号</th>"); 16 sb.Append("<th style='width:110px;' bgColor='#ccfefe'>提现银行</th>"); 17 sb.Append("<th style='width:80px;' bgColor='#ccfefe'>账户余额</th>"); 18 sb.Append("<th style='width:80px;' bgColor='#ccfefe'>提现金额</th>"); 19 sb.Append("<th style='width:80px;' bgColor='#ccfefe'>手续费</th>"); 20 sb.Append("<th style='width:70px;' bgColor='#ccfefe'>当前状态</th>"); 21 sb.Append("</tr></thead>"); 22 sb.Append("<tbody>"); 23 DataRow[] myRow = dt.Select(); 24 int i = 0; 25 int cl = dt.Columns.Count; 26 foreach (DataRow row in myRow) 27 { 28 sb.Append("<tr>"); 29 for (i = 0; i < cl; i++) 30 { 31 if (i == 0 || i == 4) 32 {//在Excel中以文本的格式显示 33 sb.Append("<td style=\"vnd.ms-excel.numberformat:@\">" + row[i].ToString() + "</td>"); 34 } 35 else 36 { 37 sb.Append("<td>" + row[i].ToString() + "</td>"); 38 } 39 } 40 sb.Append("</tr>"); 41 } 42 sb.Append("</tbody></table>"); 43 return sb.ToString(); 44 } 45 46 /// <summary> 47 /// 以流的形式,可以设置很丰富复杂的样式 48 /// </summary> 49 /// <param name="content">Excel中内容(Table格式)</param> 50 /// <param name="filename">文件名</param> 51 /// <param name="cssText">样式内容</param> 52 public static void ExportToExcel(string filename, string content, string cssText) 53 { 54 var res = HttpContext.Current.Response; 55 content = String.Format("<style type='text/css'>{0}</style>{1}", cssText, content); 56 57 res.Clear(); 58 res.Buffer = true; 59 res.Charset = "UTF-8"; 60 res.AddHeader("Content-Disposition", "attachment; filename=" + filename); 61 res.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); 62 res.ContentType = "application/ms-excel;charset=UTF-8"; 63 res.Write(content); 64 res.Flush(); 65 res.End(); 66 }
在这里有个重点需要注意下,就是Excel的单元格格式
在这个时候,需要将数字格式转换为文本格式,也就是上面的代码块里的 style=\"vnd.ms-excel.numberformat:@\"

OK,至此,以流导出Excel便完成了,大家如果有什么意见,可以在下面留言,博主看到会回复大家的

浙公网安备 33010602011771号