码迷,mamicode.com
首页 > Web开发 > 详细

Ext.Net导出Excel

时间:2015-12-25 17:06:34      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:

最近项目结束了,其中用到了Ext.Net的GridPanel导出Excel,简单的过程如下:

导出按钮的事件:

技术分享
 1 protected void btnExport_DirectClick(object sender, DirectEventArgs e)
 2         {
 3             if (Session["BorrowRecord"] != null)
 4             {
 5                 DataTable tbBorrowRecord = (DataTable)Session["BorrowRecord"];
 6                 if (tbBorrowRecord.Rows.Count > 0)
 7                 {
 8                     string content = m_BorrowBLL.GetExportHtml(tbBorrowRecord); // 把DataTable表格转成HTML
 9                     string title = "工具借用记录导出" + DateTime.Now.ToString("yyyyMMddHHmmss");
10                     Session["Content"] = content;
11                     UIHelper.LogInfo("导出超修记录Exce", OperatorType.导出);
12                     X.Redirect("/Ashx/LoadExcel.ashx?title=" + title);
13                 }
14                 else
15                 {
16                     // 没有数据,提示没有数据无法导出
17                     Notification.Show(
18                         new NotificationConfig
19                         {
20                             Title = "提示",
21                             Icon = Icon.Information,
22                             Html = "没有数据,无法导出!"
23                         });
24                 }
25             }
26         }
View Code

根据DataTable拼凑出Html table结构:

技术分享
 1 /// <summary>
 2         /// 根据借用记录数据表格获取导出的HTML
 3         /// </summary>
 4         /// <param name="tbBorrowRecord"></param>
 5         /// <returns></returns>
 6         public string GetExportHtml(DataTable tbBorrowRecord)
 7         {
 8             tbBorrowRecord = tbBorrowRecord.DefaultView.ToTable(false, new string[] { "SectionName", "Category", "Model", "Code", "BorrowMan", "BorrowTime", "ReturnMan", "ReturnTime", "State" });
 9             string html = string.Empty;
10             string datarow = string.Empty;
11             foreach (DataRow drBorrow in tbBorrowRecord.Rows)
12             {
13                 string content = string.Empty; // 数据内容
14                 datarow += "<tr>";
15                 for (int i = 0; i < tbBorrowRecord.Columns.Count; i++)
16                 {
17                     if (tbBorrowRecord.Columns[i].ColumnName == "State")
18                     {
19                         switch (drBorrow[i].ToString())
20                         {
21                             case "1":
22                                 content = "未归还";
23                                 break;
24                             case "2":
25                                 content = "已归还";
26                                 break;
27                         }
28                     }
29                     else
30                     {
31                         content = drBorrow[i].ToString();
32 
33                     }
34                     datarow += "<td>" + content + "</td>";
35                 }
36                 datarow += "</tr>";
37             }
38             html = string.Format(@"<html>
39                                           <meta http-equiv=‘Content-Type‘ content=‘text/html;charset=‘utf-8‘ />
40                                           <head></head>
41                                           <body>
42                                              <table style=‘border-collapse:collapse‘ bordercolor=‘#111111‘ cellSpacing=‘0‘ cellPadding=‘2‘ border=‘1‘>
43                                                 <thead>
44                                                 <th>工区</th>
45                                                 <th>工具类型</th>
46                                                 <th>规格型号</th>
47                                                 <th>条形码</th>
48                                                 <th>借用人</th>
49                                                 <th>借用日期</th>
50                                                 <th>归还人</th>
51                                                 <th>归还日期</th>
52                                                 <th>状态</th>
53                                                 </thead>
54                                                 {0}
55                                             </table>
56                                           </body>
57                                           </html>", datarow);
58 
59             return html;
60         }
View Code

在一般处理程序中导出Excel

技术分享
 1 using System.Web;
 2 
 3 namespace HuaweiSoftware.EMMS_JCW.UI.Ashx
 4 {
 5     /// <summary>
 6     /// 导出记录到Excel
 7     /// </summary>
 8     public class LoadExcel : IHttpHandler, System.Web.SessionState.IRequiresSessionState
 9     {
10         public void ProcessRequest(HttpContext context)
11         {
12             string content = context.Session["content"].ToString();
13             string title = context.Request["title"];
14             context.Session.Remove("content");
15 
16             // 设置http流格式,写入拼接Excel内容
17             context.Response.Clear();
18             context.Response.Buffer = true;
19             context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(title) + ".xls");
20             context.Response.ContentEncoding = System.Text.Encoding.UTF8;
21             context.Response.ContentType = "application/vnd.ms-excel";
22             context.Response.Write(content);
23             context.Response.End();
24         }
25 
26         public bool IsReusable
27         {
28             get
29             {
30                 return false;
31             }
32         }
33     }
34 }
View Code

 

Ext.Net导出Excel

标签:

原文地址:http://www.cnblogs.com/hongyan5682/p/5075976.html

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