- GridView數據導入Excel/Excel數據讀入GridView
- 效果圖:
- GridView數據導入Excel/Excel數據讀入GridView
- 效果圖:
- <IMG alt="" src="http://www.vs2005.com/DownloadPictures/200708/2007081212135891.gif">
- 解決方案:
- 頁面增加一個按鈕,單擊事件添加如下方法:
- 1protected void Button1_Click(object sender, EventArgs e)
- 2{
- 3 Export("application/ms-excel", "學生成績報表.xls");
- 4}
- 5private void Export(string FileType, string FileName)
- 6{
- 7 Response.Charset = "GB2312";
- 8 Response.ContentEncoding = System.Text.Encoding.UTF7;
- 9 Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
- 10 Response.ContentType = FileType;
- 11 this.EnableViewState = false;
- 12 StringWriter tw = new StringWriter();
- 13 HtmlTextWriter hw = new HtmlTextWriter(tw);
- 14 GridView1.RenderControl(hw);
- 15 Response.Write(tw.ToString());
- 16 Response.End();
- 17}
- 18//如果沒有下面方法會報錯類型“GridView”的控件“GridView1”必須放在具有 runat=server 的窗體標記內
- 19public override void VerifyRenderingInServerForm(Control control)
- 20{
- 21}
- 還有由於是文件操作所以要引入名稱空間IO和Text
- 後臺代碼:
- 1using System;
- 2using System.Data;
- 3using System.Configuration;
- 4using System.Web;
- 5using System.Web.Security;
- 6using System.Web.UI;
- 7using System.Web.UI.WebControls;
- 8using System.Web.UI.WebControls.WebParts;
- 9using System.Web.UI.HtmlControls;
- 10using System.Data.SqlClient;
- 11using System.Drawing;
- 12using System.IO;
- 13using System.Text;
- 14public partial class Default7 : System.Web.UI.Page
- 15{
- 16 SqlConnection sqlcon;
- 17 SqlCommand sqlcom;
- 18 string strCon = "Data Source=(local);Database=北風貿易;Uid=sa;Pwd=sa";
- 19 protected void Page_Load(object sender, EventArgs e)
- 20 {
- 21 if (!IsPostBack)
- 22 {
- 23 bind();
- 24 }
- 25 }
- 26
- 27 public void bind()
- 28 {
- 29 string sqlstr = "select top 5 * from 飛狐工作室";
- 30 sqlcon = new SqlConnection(strCon);
- 31 SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
- 32 DataSet myds = new DataSet();
- 33 sqlcon.Open();
- 34 myda.Fill(myds, "飛狐工作室");
- 35 GridView1.DataSource = myds;
- 36 GridView1.DataKeyNames = new string[] { "身份證號碼" };
- 37 GridView1.DataBind();
- 38 sqlcon.Close();
- 39 }
- 40 protected void Button1_Click(object sender, EventArgs e)
- 41 {
- 42 Export("application/ms-excel", "學生成績報表.xls");
- 43 }
- 44 private void Export(string FileType, string FileName)
- 45 {
- 46 Response.Charset = "GB2312";
- 47 Response.ContentEncoding = System.Text.Encoding.UTF7;
- 48 Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
- 49 Response.ContentType = FileType;
- 50 this.EnableViewState = false;
- 51 StringWriter tw = new StringWriter();
- 52 HtmlTextWriter hw = new HtmlTextWriter(tw);
- 53 GridView1.RenderControl(hw);
- 54 Response.Write(tw.ToString());
- 55 Response.End();
- 56 }
- 57 public override void VerifyRenderingInServerForm(Control control)
- 58 {
- 59 }
- 60}
- 前臺:
- 1<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3"
- 2 BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" >
- 3 <FooterStyle BackColor="White" ForeColor="#000066" />
- 4 <Columns>
- 5 <asp:BoundField DataField="身份證號碼" HeaderText="編號" ReadOnly="True" />
- 6 <asp:BoundField DataField="姓名" HeaderText="姓名" />
- 7 <asp:BoundField DataField="出生日期" HeaderText="郵政編碼" />
- 8 <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" />
- 9 <asp:BoundField DataField="郵政編碼" HeaderText="郵政編碼" />
- 10 <asp:BoundField DataField="起薪" HeaderText="起薪" />
- 11
- 12 </Columns>
- 13 <RowStyle ForeColor="#000066" />
- 14 <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
- 15 <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" CssClass="ms-formlabel DataGridFixedHeader"/>
- 16 <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
- 17</asp:GridView>
- 18<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="導出" />
- 讀取Excel數據的代碼:這個很簡單的
- 1private DataSet CreateDataSource()
- 2{
- 3 string strCon;
- 4 strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("excel.xls") + "; Extended Properties=Excel 8.0;";
- 5 OleDbConnection olecon = new OleDbConnection(strCon);
- 6 OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strCon);
- 7 DataSet myds = new DataSet();
- 8 myda.Fill(myds);
- 9 return myds;
- 10}
- 11protected void Button1_Click(object sender, EventArgs e)
- 12{
- 13 GridView1.DataSource = CreateDataSource();
- 14 GridView1.DataBind();
- 15}