manbetx报道:

  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;//伸入execl操干命名当空

  using System.Data.SqlClient;

  ///

  

  ///

  查询EXCEL电儿子表格添加以到DATASET

  ///

  

  ///

  

  name="filenameurl">效力动器道路

  ///

  

  name="table">表名

  public

  DataSet ExecleDs(string filenameurl, string table)

  {

  string strConn="Provider=Microsoft.Jet.OleDb.4.0;" + "data

  source=" + filenameurl + ";Extended Properties='Excel 8.0; HDR=YES;

  IMEX=1'";

  OleDbConnection conn=new OleDbConnection(strConn);

  conn.Open();

  DataSet ds=new DataSet();

  OleDbDataAdapter odda=new OleDbDataAdapter("select * from

  [Sheet4$]", conn);

  odda.Fill(ds, table);

  return ds;

  }

  //单击终止管excel文件并将数据管到数据库

  protected

  void Button1_Click(object sender, EventArgs e)

  {

  if (FileUpload1.HasFile==false)//HasFile用到来反节FileUpload能否拥有指定文件

  {

  Response.Write("

  ");

  return;//当无文件时,前往

  }

  string

  IsXls=System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();//System.IO.Path.GetExtension得到文件的扩展名

  if (IsXls !=".xls")

  {

  Response.Write("");

  return;//当选择的不是Excel文件时,前往

  }

  SqlConnection cn=new SqlConnection(strConn);

  cn.Open();

  string filename=DateTime.Now.ToString("yyyymmddhhMMss") +

  FileUpload1.FileName;

  //获取Execle文件名 DateTime日期函数

  string savePath=Server.MapPath(("~\?es") +

  filename);//Server.MapPath 得到杜撰效力动器对立道路

  FileUpload1.SaveAs(savePath);

  //SaveAs 将上传的文件情节管在效力动器上

  DataSet ds=ExecleDs(savePath,

  filename);

  //调用己定义方法

  DataRow[] dr=ds.Tables[0].Select();