protected void dispdata_Click(object sender, EventArgs e)
{
string file_path = FileUpload1.PostedFile.FileName.ToString().Trim();
string[] sheetNameString;
string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + file_path.ToString() + ";" + "Extended Properties=Excel 8.0";
OleDbConnection objcon = new OleDbConnection(ConnectionString);
objcon.Open();
DataTable dt = null;
try
{
objConn = new OleDbConnection(ConnectionString);
objConn.Open();
dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt != null)
{
sheetNameString = new string[dt.Rows.Count];
int i = 0;
foreach (DataRow row in dt.Rows)//遍历取出各个工作表的SHEET名称
{
sheetNameString[i] = row["TABLE_NAME"].ToString();
i++;
}
}
}
catch(Exception er)
{
Response.Write(er.ToString());
}
string str1="select * from ["+sheetNameString[0].ToString()+"]";//显示第一个工作表的内容,要显示多个工作表,请循环显示则行
OleDbDataAdapter olda = new OleDbDataAdapter(str1, objcon);
DataSet ds = new DataSet();
olda.Fill(ds,"studenttab");
GridView1.DataSource = ds.Tables["studenttab"].DefaultView;
GridView1.DataBind();
objcon.Close();
}