工作需要.提供下我的代码.直接上代码了,挺简单的.不需要怎么解释了
protected void btnExport_Click( object sender, EventArgs e) { List < Bonus.Model.LogMemberBonusOut > ExportExcel = new Bonus.BLL.LogMemberBonusOut().GetList( 1 , 10000 , Session[ " strWhere " ].ToString()); DataSet ds = ChangeParantIDToParentExcel(ExportExcel); string ExcelName = " PlayerFundOutListing_ " + DateTime.Now.ToString( " yyyyMMdd " ) + " .xls " ; CreateExcelFromTable(ds.Tables[ 0 ], ExcelName); } #region 生成Excel的代码 private void CreateExcelFromTable(DataTable table, string FileName) { HttpResponse response = Page.Response; response.ContentEncoding = System.Text.Encoding.GetEncoding( " GBK " ); response.AddHeader( " Content-Disposition " , " attachment;filename= " + FileName); int index = 0 ; string headers = "" ; for (index = 0 ; index < table.Columns.Count; index ++ ) { headers += table.Columns[index].ColumnName + " \t " ; } headers += " \n " ; response.Write(headers); response.Flush(); foreach (DataRow row in table.Rows) { string rowContent = "" ; foreach (DataColumn column in table.Columns) { rowContent += row[column.ColumnName].ToString() + " \t " ; } rowContent += " \n " ; response.Write(rowContent); response.Flush(); } response.End(); } private DataSet ChangeParantIDToParentExcel(List < Bonus.Model.LogMemberBonusOut > ListData) { DataSet ds = new DataSet(); DataTable tmpDt = new DataTable(); tmpDt.Columns.Add( " TranID " ); tmpDt.Columns.Add( " Member Code " ); tmpDt.Columns.Add( " Fund Out Amt " ); tmpDt.Columns.Add( " Bonus " ); tmpDt.Columns.Add( " Product " ); tmpDt.Columns.Add( " Fund Out Date " ); tmpDt.Columns.Add( " Status " ); tmpDt.Columns.Add( " Verified By " ); tmpDt.Columns.Add( " Verified Date " ); DataRow dr; foreach (var item in ListData) { dr = tmpDt.NewRow(); dr[ " TranID " ] = item.OrderID; dr[ " Member Code " ] = item.MemberCode; dr[ " Fund Out Amt " ] = item.OutMoney; dr[ " Bonus " ] = item.Bonus; dr[ " Product " ] = GetPname(Convert.ToInt16(item.ProductIDInfo)); dr[ " Fund Out Date " ] = item.OutDate.ToString( " yyyy-MM-dd hh:mm:ss " ); dr[ " Status " ] = item.StatusID; dr[ " Verified By " ] = item.CreateBy; dr[ " Verified Date " ] = item.CreateDate.ToString( " yyyy-MM-dd hh:mm:ss " ).Replace( " 0001-01-01 12:00:00 " , "" ); tmpDt.Rows.Add(dr); } tmpDt.AcceptChanges(); ds.Tables.Add(tmpDt); return ds; }