C # Export Excel

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.