private DataSet CreateDataSource()
    {
        string strCon;
        strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/Book1.xls") + "; Extended Properties=Excel 8.0;";
        OleDbConnection olecon = new OleDbConnection(strCon);
        OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strCon);
        DataSet myds = new DataSet();
        myda.Fill(myds);
        return myds;
    }

    protected void Button1_ServerClick(object sender, EventArgs e)//导入数据按钮
    {
        GridView1.DataSource = CreateDataSource();
        GridView1.DataBind();
    }

需要注意的是: excel 必须是自己把数据录进去的,不能对导出的excel进行导入,excel不能有合并行。

1.Server.MapPath("~/Book1.xls")   注意xls的文件所在。   sheet1 是xls里面的表名,打开xls 左下角.

相关文章:

  • 2021-11-25
  • 2021-06-01
  • 2021-11-16
  • 2022-12-23
  • 2022-01-14
  • 2021-06-28
  • 2021-09-05
  • 2022-12-23
猜你喜欢
  • 2021-06-11
  • 2021-11-11
  • 2021-12-03
  • 2021-06-23
  • 2021-10-13
  • 2022-02-10
相关资源
相似解决方案