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();
}
}
{
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();
}
}