protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
if (FileUpload1.HasFile)
{
if (!System.IO.Directory.Exists(Server.MapPath("uploads/"))) //判断是否存在文件夹
{
Directory.CreateDirectory(Server.MapPath("uploads/"));
}
string FileName = FileUpload1.FileName;
string FilePath = Server.MapPath("uploads/");
FileUpload1.SaveAs( FilePath+FileName);
string Connetstring = "Provider=Microsoft.Jet.OLEDB.4.0;"+"Data Source="+FilePath+FileName+";"+"Extended Properties=Excel 8.0";
OleDbConnection Excelcnn = new OleDbConnection(Connetstring);
Excelcnn.Open();
DataTable dt = Excelcnn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });//读取excel里面的sheets的名字
DataSet ExcelDS = new DataSet();
foreach (DataRow dr in dt.Rows)
{
string tbName= "["+dr["TABLE_NAME"].ToString()+"]";
string st="Select * from "+tbName+"";
ExcelDA = new OleDbDataAdapter(st, Excelcnn);
ExcelDA.Fill(ExcelDS);
}
Label1.Text = ExcelDS.Tables[0].Rows[6][0].ToString();
Excelcnn.Close();
}
else { Response.Write("<script>window.alert('您没有指定要上传的文件!')</script>"); }
}
}