Read data in Excel into the GridView

Source: Internet
Author: User

1. Page code:

   1:  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
   2:   
   3:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   4:   
   5:  
   6:  
   7:      <title></title>
   8:  
   9:  <body>
  10:      <form id="form1" runat="server">
  11:      <div>
  12:      </div>
  13:      <asp:Panel ID="Panel1" runat="server" BorderWidth="3px">
  14:          <asp:GridView ID="GridView1" runat="server" 
  15:      AllowPaging="True" onpageindexchanging="GridView1_PageIndexChanging" 
  16:      Width="702px">
  17:          </asp:GridView>
  18:      </asp:Panel>
  19:      <p>
20: <asp: Button ID = "Button1" runat = "server" Text = "read data" onclick = "button#click"
  21:              style="height: 26px" />
  22:      </p>
  23:      </form>
  24:  </body>
  25:  

2. Execute the code

   1:  using System;
   2:  using System.Web.UI.WebControls;
   3:  using System.Data;
   4:  using System.Data.OleDb;
   5:   
   6:  public partial class Default2 : System.Web.UI.Page
   7:  {
   8:      protected void Page_Load(object sender, EventArgs e)
   9:      {
  10:   
  11:      }
  12:      protected void Button1_Click(object sender, EventArgs e)
  13:      {
  14:          GridView1.DataSource = CreateDataSource();
  15:          GridView1.DataBind();
  16:   
  17:      }
  18:      private DataSet CreateDataSource()
  19:      {
  20:          string connString;
  21:          connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Checklist.xls") + "; Extended Properties=Excel 8.0;";
  22:          OleDbConnection olecon = new OleDbConnection(connString);
  23:          OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connString);
  24:          DataSet myds = new DataSet();
  25:          myda.Fill(myds);
  26:          return myds;
  27:      }
  28:      protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
  29:      {
  30:          GridView1.PageIndex = e.NewPageIndex;
  31:          GridView1.DataSource = CreateDataSource();
  32:          GridView1.DataBind();
  33:      }
  34:  }
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.