OleDbConnection odc= new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\\test\\test.xls; Extended Properties=Excel 8.0");
odc.Open();
try
{
DataTable dt = odc.GetOleDbSchemaTable( OleDbSchemaGuid.Tables,new object[] {null, null, null, "TABLE"});
odc.Close();
if ( dt.Rows.Count > 0 ) {
sTBList = new string[dt.Rows.Count];
int i = 0;
foreach ( DataRow dr in dt.Rows ) {
sTBList[i] = dr["TABLE_NAME"].ToString();
i += 1;
}
}
}
catch(Exception ex)
{
Response.Write(ex.StackTrace);
}
OleDbCommand odcmd= new OleDbCommand("SELECT * FROM [" + sTBList[0] +"]",odc); //取得第1個工作表
OleDbDataAdapter odda= new OleDbDataAdapter(odcmd) ;
DataSet ds= new DataSet();
odda.Fill(ds) ;
DataGrid1.DataSource = ds.Tables[0].DefaultView;
DataGrid1.DataBind() ;
odda.Dispose();
odc.Close();
沒有留言:
張貼留言