解析TABLE導入到EXCEL_.Net教程

      編輯Tag賺U幣
      教程Tag:暫無Tag,歡迎添加,賺取U幣!

      推薦:解析ASP.NET頁面數據導出到Excel或Word
      privatevoidbtnMIME_Click(objectsender,System.EventArgse) { BindData(); Response.ContentType=application/vnd.ms-Excel; Response.AddHeader(Content-Disposition,inline;filename= +HttpUtility.UrlEncode(下載文件.xls,Encoding.UTF8)); //如

      前臺代碼:ExportExcel1.aspx 
         <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExportExcel1.aspx.cs" Inherits="ExportExcel1" %>  
        
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
        
      <html xmlns="http://www.w3.org/1999/xhtml" >  
      <head runat="server">  
          <title>導出數據到EXCEL</title>  
      </head>  
      <body>  
           <h3>Table Example, constructed programmatically</h3>  
           <form id="Form1" runat=server>  
               <asp:Table id="Table1"    
                GridLines="Both"    
                HorizontalAlign="Center"    
                Font-Name="Verdana"    
                Font-Size="8pt"    
                CellPadding=15    
                CellSpacing=0    
                Runat="server"/>  
               <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="導出數據" />  
           </form>  
      </body>  
      </html>  

      后臺代碼:ExportExcel1.aspx .cs
      using System;   
      using System.Data;   
      using System.Configuration;   
      using System.Collections;   
      using System.Web;   
      using System.Web.Security;   
      using System.Web.UI;   
      using System.Web.UI.WebControls;   
      using System.Web.UI.WebControls.WebParts;   
      using System.Web.UI.HtmlControls;   
      using System.IO;   
      public partial class ExportExcel1 : System.Web.UI.Page   
      {   
          protected void Page_Load(object sender, EventArgs e)   
          {   
               // Generate rows and cells.              
                   TableRow r = new TableRow();   
                   TableCell c1 = new TableCell();   
                   c1.ColumnSpan = 2;   
                   c1.Text = "test";   
                   c1.HorizontalAlign = HorizontalAlign.Center;   
                   r.Cells.Add(c1);   
                   Table1.Rows.Add(r);   
                   int numrows = 3;   
                   int numcells = 2;   
                   for (int j=0; j<numrows; j++)   
                   {             
                       TableRow r1 = new TableRow();   
                       for (int i=0; i<numcells; i++)    
                       {   
                          TableCell c = new TableCell();   
                          c.Controls.Add(new LiteralControl("row " + j.ToString() + ", cell " + i.ToString()));   
                          r1.Cells.Add(c);   
                       }   
                       Table1.Rows.Add(r1);   
                   }   
          }   
          protected void Button1_Click(object sender, EventArgs e)   
          {   
                 
              DateTime dt = System.DateTime.Now;//取出當前系統日期時間   
              string dtt = dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString();//取出系統日期   
              string filestr = "C:\\excel"; //filestr是文件的路徑   
              StringWriter stringWriter = new StringWriter();   
              HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);   
              Table1.RenderControl(htmlWriter);   
              string file = filestr + "\\" + dtt + ".xls";   
              if (!Directory.Exists(filestr))   
              {   
                  Directory.CreateDirectory(filestr);   
              }   
              System.IO.StreamWriter sw = new StreamWriter(file);   
              sw.Write(stringWriter.ToString());   
              sw.Close();   
              
          }   
      }  

      分享:解析簡單實用的DataGrid自定義分頁源程序
      首先新建一個名為article.aspx的文件,將以下內容拷貝到article.aspx.cs中: usingSystem; usingSystem.Data; usingSystem.Configuration; usingSystem.Collections; usingSystem.Web; usingSystem.Web.Security; usingSystem.Web.UI; usingSystem.Web.UI.WebCo

      來源:模板無憂//所屬分類:.Net教程/更新時間:2010-02-01
      相關.Net教程