NPOI-WebForm_Excel導入與導出


本文面對的是第一次 接觸NPOI的童鞋

不必為了一些瑣碎的事情搞的心情煩躁

廢話不多說先上 Demo 的全家福

接下來直接上代碼

 1  public partial class _Default : System.Web.UI.Page
 2     {
 3         protected void Page_Load(object sender, EventArgs e)
 4         {
 5             if (IsPostBack)
 6             {
 7                 //導出Excel
 8                 ExportByWeb("ExportDemo.xlsx");
 9             }
10             RepeaterTable.DataSource = GetDtTable();
11             RepeaterTable.DataBind();
12         }
13 
14         public static MemoryStream ExcelStream()
15         {
16             DataTable dtTable = GetDtTable();
17             return ExcelHelper.ExportDT(dtTable, "HeaderText");
18 
19         }
20 
21         private static DataTable GetDtTable()
22         {
23             string path = HttpContext.Current.Request.MapPath("~/App_Data/excel2007.xlsx");
24             //調用ZK的ExcelHelper
25             DataTable dtTable = ExcelHelper.ImportExceltoDt(path);
26             return dtTable;
27         }
28 
29         public static void ExportByWeb(string strFileName)
30         {
31             HttpContext curContext = HttpContext.Current;
32 
33             // 設置編碼和附件格式
34             curContext.Response.ContentType = "application/vnd.ms-excel";
35             curContext.Response.ContentEncoding = Encoding.UTF8;
36             curContext.Response.Charset = "";
37             curContext.Response.AppendHeader("Content-Disposition",
38                 "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));
39 
40             curContext.Response.BinaryWrite(ExcelStream().GetBuffer());
41             curContext.Response.End();
42         }
43     }

代碼很簡單,很容易看懂O(∩_∩)O~

前台綁定數據的代碼

 1 <h2>
 2         歡迎使用 NPOI
 3     </h2>
 4     <asp:Repeater ID="RepeaterTable" runat="server">
 5         <HeaderTemplate>
 6             <table border="1" cellpadding="10" cellspacing="0">
 7                 <tr>
 8                     <td>
 9                         ID
10                     </td>
11                     <td>
12                         品牌
13                     </td>
14                     <td>
15                         型號
16                     </td>
17                 </tr>
18         </HeaderTemplate>
19         <ItemTemplate>
20             <tr>
21                 <td>
22                     <%# DataBinder.Eval(Container.DataItem,"ID") %>
23                 </td>
24                 <td>
25                     <%# DataBinder.Eval(Container.DataItem,"品牌") %>
26                 </td>
27                 <td>
28                     <%# DataBinder.Eval(Container.DataItem,"型號") %>
29                 </td>
30             </tr>
31         </ItemTemplate>
32         <FooterTemplate>
33             </table>
34         </FooterTemplate>
35     </asp:Repeater>
36     <p>
37         <form action="/" method="post">
38         <input type="submit" name="name" value="導出" />
39         </form>
40     </p>

執行結果:

代碼的下載地址

http://files.cnblogs.com/zhaozhengyan/ExcelDemoFor2.0.1.rar


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM