Excel导入GridView

<% @ Page Language="C#" AutoEventWireup="true" CodeFile="VisitExcel1.aspx.cs" Inherits="VisitExcel1"  %>

<! 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 >
    
< form  id ="form1"  runat ="server" >
    
< div >
        
< asp:Label  ID ="Label1"  runat ="server"  Text ="Excel表格内容:" ></ asp:Label >
        
< asp:GridView  ID ="GridView1"  runat ="server" >
        
</ asp:GridView >
    
</ div >
    
</ form >
</ body >
</ html >

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.Data.OleDb;

public  partial  class  VisitExcel1 : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
string strConn;
        strConn 
= "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=D:\\exceltest.xls;" + "Extended Properties=Excel 8.0;";
        OleDbDataAdapter myCommand 
= new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);
        DataSet myDataSet 
= new DataSet();
        myCommand.Fill(myDataSet, 
"ExcelInfo");
        GridView1.DataSource 
= myDataSet.Tables["ExcelInfo"].DefaultView;
        GridView1.DataBind();
    }

}

你可能感兴趣的:(GridView)