C# 获取xlsx表名

来源:互联网 发布:淘宝学院官网 编辑:程序博客网 时间:2024/06/03 00:23
   protected void Button1_Click(object sender, EventArgs e)
    {
        string strConnection1 = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=";

        string strPath = Server.MapPath("file") + "\\" + FileUpload1.FileName;
        FileUpload1.SaveAs(Server.MapPath("file") + "\\" + FileUpload1.FileName);
        OleDbConnection con = new OleDbConnection(strConnection1+strPath);
        Response.Write(strConnection1);
        con.Open();
        try
        {
            DataTable schema = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
            //下面取得第一个表名   
            string strTableName = schema.Rows[0]["TABLE_NAME"].ToString();

            Response.Write(strTableName);

            OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [" + strTableName + "]", con);
            DataSet ds = new DataSet();
            da.Fill(ds);

            da.Dispose();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
        }
    }
原创粉丝点击