码迷,mamicode.com
首页 > Windows程序 > 详细

c#导出Excel

时间:2015-07-22 10:36:40      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
 1  protected void btnExecl_Click(object sender, EventArgs e)
 2     {
 3         DataSet myds = new DataSet();
 4         string ConnectionString = WebConfigurationManager.ConnectionStrings["TMSDatabase"].ConnectionString;
 5         string actionid = GetParameter("id");
 6         using (SqlConnection conn = new SqlConnection(ConnectionString))
 7         {
 8             conn.Open();
 9             string sqlstr = " SELECT b.UserName 人员姓名,s.[Score_Teammate] 互评得分合计,s.[Score_Leader] 处长打分,s.[Score_Summary] 最终得分 FROM [PA_Score] s inner join BD_BaseUserInfo b on s.Assess_UserID = b.UserID "
10                       + "where  s.[Assess_Action_ID]=‘" + actionid + "‘ and s.IsHistory=‘true‘ order by s.[Score_Summary] desc";
11             SqlDataAdapter myda = new SqlDataAdapter(sqlstr, conn);
12             myda.Fill(myds, "ExportExcel");
13         }
14         if (myds.Tables[0].Rows.Count > 0)
15         {
16             CreateExcel(myds);
17         }
18         else
19         {
20             Response.Write("<script type=‘text/javascript‘>alert(‘没有要倒出的数据!‘);</script>");
21         }
22     }
View Code
技术分享
 1   public void CreateExcel(DataSet dsProductListSearch)
 2     {
 3         try
 4         {
 5             //导出
 6             XlsDocument xls = new XlsDocument();
 7             //xls.FileName = "workstat.xls";
 8             xls.FileName = DateTime.Now.ToString("yyyyMMddHHmmss", System.Globalization.DateTimeFormatInfo.InvariantInfo);
 9 
10             Worksheet sheet = xls.Workbook.Worksheets.Add("Sheet1");
11             //填充表头   
12             foreach (DataColumn col in dsProductListSearch.Tables[0].Columns)
13             {
14                 string[] columnNames = col.ColumnName.Split(#);
15 
16                 Cell cell = sheet.Cells.Add(1, col.Ordinal + 1, col.ColumnName);
17                 cell.Font.FontFamily = FontFamilies.Roman; //字体   
18                 cell.Font.Bold = true;  //字体为粗体 
19             }
20             //填充内容   
21             string strvare = "";
22             for (int i = 0; i < dsProductListSearch.Tables[0].Rows.Count; i++)
23             {
24                 for (int j = 0; j < dsProductListSearch.Tables[0].Columns.Count; j++)
25                 {
26                     if (j == 15)
27                     {
28                         if (dsProductListSearch.Tables[0].Rows[i][j].ToString() == "-1")
29                         {
30                             strvare = "超时";
31                         }
32                         if (dsProductListSearch.Tables[0].Rows[i][j].ToString() == "1")
33                         {
34                             strvare = "";
35                         }
36                     }
37                     else
38                     {
39                         strvare = dsProductListSearch.Tables[0].Rows[i][j].ToString();
40                     }
41                     sheet.Cells.Add(i + 2, j + 1, strvare);
42                 }
43             }
44 
45             xls.Send();
46         }
47         catch
48         {
49             Response.Write("<script language=javascript>‘rztj->serarch.aspx.cs->Button_ToExcel_Click导出excel错误‘</script>");
50         }
51 
52     }
View Code

需要用到的dll下载地址

http://files.cnblogs.com/files/ithuo/%E5%AF%BC%E5%87%BAExcel%E7%94%A8%E5%88%B0%E7%9A%84dll.rar

c#导出Excel

标签:

原文地址:http://www.cnblogs.com/ithuo/p/4666559.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!