本例介紹的用C#(asp.net)實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出Excel詳細(xì)代碼的方法,,會(huì)輸出標(biāo)準(zhǔn)的Excel格式文件,,非常穩(wěn)定,不會(huì)死鎖Excel進(jìn)程,,支持中文文件名,,支持表頭導(dǎo)出,,支持大多數(shù)數(shù)據(jù)庫(kù)導(dǎo)入。 C#(asp.net)數(shù)據(jù)導(dǎo)出Excel實(shí)現(xiàn)算法:利用Excel組件將DataGrid控件內(nèi)容生成Excel臨時(shí)文件,,并存放在服務(wù)器上,,然后用Response方法將生成的Excel文件下載到客戶端然后再將生成的臨時(shí)文件刪除。 25億CMS系統(tǒng)v2.9實(shí)現(xiàn)此功能,,大家可以25億CMS系統(tǒng)后臺(tái)查看并研究,。 C#(asp.net)數(shù)據(jù)導(dǎo)出Excel具體步驟: 1,在項(xiàng)目中引用Excel組件(Interop.Excel.dll),如需此組件,,可以到技術(shù)社區(qū)(http://bbs.)索取,,25億官方技術(shù)部會(huì)提供。 2,aspx頁(yè)面代碼: <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True" CellPadding="8" Width="690px" onpageindexchanging="gridview1_PageIndexChanging" PageSize="15" > <FooterStyle BackColor="White" ForeColor="#000066" /> <RowStyle ForeColor="#000066" /> <Columns> <asp:TemplateField HeaderText="用戶ID" > <ItemTemplate> <asp:Label runat="server" ID="name" Text='<%#DataBinder.Eval(Container.DataItem,"UserInfoId")%>'> </asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="用戶名" > <ItemTemplate> <asp:Label runat="server" ID="na" Text='<%#DataBinder.Eval(Container.DataItem,"UserName")%>'> </asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="注冊(cè)時(shí)間" > <ItemTemplate> <asp:Label runat="server" ID="na" Text='<%#DataBinder.Eval(Container.DataItem,"AddTime")%>'> </asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="登錄次數(shù)" > <ItemTemplate> <asp:Label runat="server" ID="na" Text='<%#DataBinder.Eval(Container.DataItem,"LoginNum")%>'> </asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="最后登錄時(shí)間" > <ItemTemplate> <asp:Label runat="server" ID="na" Text='<%#DataBinder.Eval(Container.DataItem,"EndTime")%>'> </asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> <PagerSettings FirstPageText="首頁(yè)" LastPageText="尾頁(yè)" NextPageText="下一頁(yè)" PreviousPageText="上一頁(yè)" Mode="NextPreviousFirstLast" /> </asp:GridView> 3,cs代碼: public void DataSourse() { //GridView數(shù)據(jù)綁定 GridView1.DataSource = bllintable.GetModds(9, Remark, this.pbComment.CurrentPage - 1, this.pbComment.PageSize, Language); GridView1.DataBind(); } public override void VerifyRenderingInServerForm(Control control) { } private void Export(string FileType, string FileName) { Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.UTF7; Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString()); Response.ContentType = FileType; this.EnableViewState = false; StringWriter tw = new StringWriter(); HtmlTextWriter hell = new HtmlTextWriter(tw); GridView1.AllowPaging = false; DataSourse(); GridView1.RenderControl(hell); Response.Write(tw.ToString()); Response.End(); GridView1.AllowPaging = true; DataSourse(); } 直接把上述代碼復(fù)制到頁(yè)面中,,修改GridView綁定參數(shù)既可以運(yùn)行,如有疑問(wèn),,可以官方技術(shù)社區(qū)(http://bbs.)提問(wèn)。
|