1 protected voidBtnexecl_click (Objectsender, EventArgs e)2 {3DataSet myds =NewDataSet ();4 stringConnectionString = webconfigurationmanager.connectionstrings["Tmsdatabase"]. ConnectionString;5 stringActionId = GetParameter ("ID");6 using(SqlConnection conn =NewSqlConnection (ConnectionString))7 {8 Conn. Open ();9 stringSqlstr ="SELECT b.username staff name, S.[score_teammate] Total Mutual assessment score, S.[score_leader] director score, S.[score_summary] final score from [Pa_score] s INNER JOIN Bd_baseuserinfo b on s.assess_userid = B.userid"Ten+"where s.[assess_action_id]= '"+ ActionId +"' and s.ishistory= ' true ' ORDER by s.[score_summary] Desc"; OneSqlDataAdapter Myda =NewSqlDataAdapter (SQLSTR, conn); AMyda. Fill (myDS,"Exportexcel"); - } - if(myDS. tables[0]. Rows.Count >0) the { - Createexcel (myds); - } - Else + { -Response.Write ("<script type= ' text/javascript ' >alert (' no data to pour out! '); </script>"); + } A}View Code
1 Public voidCreateexcel (DataSet dsproductlistsearch)2 {3 Try4 {5 //Export6Xlsdocument xls =Newxlsdocument ();7 //xls. FileName = "Workstat.xls";8Xls. FileName = DateTime.Now.ToString ("YYYYMMDDHHMMSS", System.Globalization.DateTimeFormatInfo.InvariantInfo);9 TenWorksheet sheet = xls. WORKBOOK.WORKSHEETS.ADD ("Sheet1"); One //filling the table header A foreach(DataColumn Colinchdsproductlistsearch.tables[0]. Columns) - { - string[] ColumnNames = col. Columnname.split ('#'); the -Cell cell = sheet. Cells.add (1, Col. Ordinal +1, Col. ColumnName); -Cell. font.fontfamily = Fontfamilies.roman;//Font -Cell. Font.Bold =true;//font is bold + } - //Populating content + stringStrvare =""; A for(inti =0; I < dsproductlistsearch.tables[0]. Rows.Count; i++) at { - for(intj =0; J < dsproductlistsearch.tables[0]. Columns.count; J + +) - { - if(J = = the) - { - if(dsproductlistsearch.tables[0]. ROWS[I][J]. ToString () = ="-1") in { -Strvare ="timed out"; to } + if(dsproductlistsearch.tables[0]. ROWS[I][J]. ToString () = ="1") - { theStrvare =""; * } $ }Panax Notoginseng Else - { theStrvare = dsproductlistsearch.tables[0]. ROWS[I][J]. ToString (); + } ASheet. Cells.add (i +2, J +1, strvare); the } + } - $ xls. Send (); $ } - Catch - { theResponse.Write ("<script language=javascript> ' Rztj->serarch.aspx.cs->button_toexcel_click export Excel error ' </script >"); - }Wuyi the}View Code
DLLs to use
Http://files.cnblogs.com/files/ithuo/%E5%AF%BC%E5%87%BAExcel%E7%94%A8%E5%88%B0%E7%9A%84dll.rar
C # Export Excel