GridView读取Excel数据功能
具体代码:
 public partial class ExcelToGridView : System.Web.UI.Page
    {
        string strCon = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + @"e:\excel.xls" + "; Extended Properties='Excel 8.0;'";
        protected void Page_Load(object sender, EventArgs e)
        {        }
        public DataSet CreateDataSource()
        {
            DataSet myds = new DataSet();
            try
            {
                OleDbConnection olecon = new OleDbConnection(strCon);
                olecon.Open();
                OleDbDataAdapter myda = new OleDbDataAdapter("select * from [excel$]", olecon);
                myda.Fill(myds);
                olecon.Close();
            }
            catch (Exception er)
            {
                string ex = er.ToString();
            }
            return myds;
        }        /// <summary>
        /// 从Excel中倒出到GridView中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (CreateDataSource().Tables[0].Rows.Count > 0)
            {
                this.GridView1.DataSource = CreateDataSource().Tables[0].DefaultView;
                this.GridView1.DataBind();
            }
        }    }现在报错,请各位大侠帮忙看看

解决方案 »

  1.   

    无法找到表 0。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.IndexOutOfRangeException: 无法找到表 0。源错误: 
    行 51:         protected void Button1_Click(object sender, EventArgs e)
    行 52:         {
    行 53:             if (CreateDataSource().Tables[0].Rows.Count > 0)
    行 54:             {
    行 55:                 this.GridView1.DataSource = CreateDataSource().Tables[0].DefaultView;
     
    是不是版本的问题叻?
      

  2.   

    给你看个我写的excel上传protected void Button12_Click(object sender, EventArgs e)
        {
            string strConn = "Data Source=.;DataBase=gongnuan;Uid=sa;Pwd=sa";
            if (FileUpload2.HasFile == false)//HasFile用来检查FileUpload是否有指定文件
            {
                Response.Write("<script>alert('请您选择Excel文件')</script> ");
                return;//当无文件时,返回
            }
            string IsXls = System.IO.Path.GetExtension(FileUpload2.FileName).ToString().ToLower();//System.IO.Path.GetExtension获得文件的扩展名
            if (IsXls != ".xls")
            {
                Response.Write("<script>alert('只可以选择Excel文件')</script>");
                return;//当选择的不是Excel文件时,返回
            }
            SqlConnection cn = new SqlConnection(strConn);
            cn.Open();
            string filename = FileUpload2.FileName;              //获取Execle文件名  DateTime日期函数
            string savePath = Server.MapPath(("~\\upfiles\\") + filename);//Server.MapPath 获得虚拟服务器相对路径
            FileUpload2.SaveAs(savePath);                        //SaveAs 将上传的文件内容保存在服务器上
            DataSet ds = ExecleDs(savePath, filename);           //调用自定义方法
            DataRow[] dr = ds.Tables[0].Select();            //定义一个DataRow数组
            int rowsnum = ds.Tables[0].Rows.Count;
            if (rowsnum == 0)
            {
                Response.Write("<script>alert('Excel表为空表,无数据!')</script>");   //当Excel表为空时,对用户进行提示
            }
            else
            {
                for (int i = 0; i < dr.Length; i++)
                {
                    string container_code = dr[i]["货柜号"].ToString();//日期 excel列名【名称不能变,否则就会出错】
                    string delivery_date = dr[i]["出货日期"].ToString();//编号 列名 以下类似
                    string order_code = dr[i]["订单号"].ToString();
                    //string long_short = dr[i]["尺寸"].ToString();
                    //string bl = dr[i]["bl"].ToString();
                    string container_name = dr[i]["集装箱号"].ToString();
                    //string qian_code = dr[i]["铅封号"].ToString();
                    //string zhuang_date = dr[i]["装箱时间"].ToString();                string insertstr = "insert into container_code_name (container_code,delivery_date,order_code,container_name) values ('" + container_code + "',convert(datetime,'" + delivery_date + "'),'" + order_code + "','" + container_name + "')";                SqlCommand cmd = new SqlCommand(insertstr, cn);
                    try
                    {
                        cmd.ExecuteNonQuery();
                    }
                    catch (MembershipCreateUserException ex)       //捕捉异常
                    {
                        Response.Write("<script>alert('导入内容:" + ex.Message + "')</script>");
                    }
                }
                Response.Write("<script>alert('Excle表导入成功!');location='default.aspx'</script>");
            }
            cn.Close();
        }
      

  3.   

    DataSet ds=CreateDataSource();
    if (ds.Tables[0].Rows.Count > 0)
      {
      this.GridView1.DataSource = ds;
      this.GridView1.DataBind();
      }
    string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
     using(OleDbConnection OleConn = new OleDbConnection(strConn))
    {
      OleConn.Open();
      String sql = "SELECT * FROM [Sheet1$]";
      OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
      DataSet ds= new DataSet();
      OleDaExcel.Fill(ds);
      OleConn.Close();
    }
      

  4.   

    这个方法我试过了,还是不行,报错如下:
    外部表不是预期的格式。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.OleDb.OleDbException: 外部表不是预期的格式。源错误: 
    行 31:             using (OleDbConnection OleConn = new OleDbConnection(strConn))
    行 32:             {
    行 33:                 OleConn.Open();
    行 34:                 String sql = "SELECT * FROM [Sheet1$]";
    行 35:                 OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);

    麻烦再帮忙看看,谢了~
      

  5.   

    嘿嘿,问题找出来了,是excel的格式不吻合,另存为Excel 97-2003就可以了,谢谢各位!
      

  6.   

    学习中。。
    string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
    我到哪个asp中遇到这个问题。你可以是是改改哪个Properties='Excel 8.0 那个8.0的值。。改为excel对应的版本就可以了。。
      

  7.   

    找不到可安装的 ISAM在网上找了下,大多说是连接字符串的问题,可我按大家说的改了N个版本还是有上面的问题。怎么回事?
      

  8.   

       我用的是07,把excel打来另存时保存类型改为97-2003就ok了,你们试试看~
      

  9.   

    找不到可安装的 ISAM。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.OleDb.OleDbException: 找不到可安装的 ISAM。源错误: 
    行 384:            using (OleDbConnection OleConn = new OleDbConnection(strConn))
    行 385:            {
    行 386:                OleConn.Open();
    行 387:                String sql = "SELECT * FROM [Sheet1$]";
    行 388:                OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
     什么原因?
      

  10.   

    搞定,顺便给大家说下原因。我系统安装的是公司买的office 2007(也许是精简版),需要注册一下ISAM才行。开始->运行:Regsvr32 c:\WINDOWS\system32\msexcl40.dll解决了问题!另外:通过找个原因也明白了连接字符串中两个参数的意义
    HDR=NO 表示无字段 
    HDR=Yes 表示有字段,一般默认excel表中第1行的列标题为字段名
    IMEX   表示是否强制转换为文本
      

  11.   

    读取的sql语句这样写:"select * from [sheet$]",其中的sheet 是excel中工作薄的名字
      

  12.   

    找不到可安装的 ISAM。  是因为你先装的vs后装的office,你再注册一下office就行了
      

  13.   

    问题已解决啦 
    Excel 8.0 
      

  14.   


    之前公司用的盗版Office2003,后来换成正版2007。
    发现问题是好事哈~ 嘿嘿