1
private void ExecuteSelectCommand()2

{3
DataTable dtbSearch=new DataTable();4

5
try6

{7
// Put user code to initialize the page here8
// Create connection string variable. Modify the "Data Source"9
// parameter as appropriate for your environment.10
String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +11
"Data Source=" + this.tBFileAddress.Text.Trim() + ";" +12
"Extended Properties='Excel 8.0;HDR=YES;IMEX=1;'";13

14
// Create connection object by using the preceding connection string.15
OleDbConnection objConn = new OleDbConnection(sConnectionString);16

17
// Open connection with the database.18
objConn.Open();19

20
// The code to follow uses a SQL SELECT command to display the data from the worksheet.21

22
// Create new OleDbCommand to return data from worksheet.23
OleDbCommand objCmdSelect =new OleDbCommand("SELECT * FROM ["+tBsheet.Text.Trim()+"$] where 料號<>''", objConn);24

25
// Create new OleDbDataAdapter that is used to build a DataSet26
// based on the preceding SQL SELECT statement.27
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();28

29
// Pass the Select command to the adapter.30
objAdapter1.SelectCommand = objCmdSelect;31

32
// Create new DataSet to hold information from the worksheet.33
DataSet objDataset1 = new DataSet();34

35
// Fill the DataSet with the information from the worksheet.36
objAdapter1.Fill(objDataset1,"料號基本資料");37

38
Thread.Sleep(200);39
statusBarPanel4.Text="正在讀取數據
["+objDataset1.Tables["料號基本資料"].Rows.Count+"]";40
Thread.Sleep(500);41
// Clean up objects.42
objConn.Close();43

44
dtbSearch=objDataset1.Tables["料號基本資料"];45

46
progressBar1.Value=30;47

48
}49
catch50

{51
progressBar1.Value=0;52
statusBarPanel4.Text="讀取EXCEL文檔錯誤!請返回確認!
";53
//MessageBox.Show("讀取EXCEL文檔錯誤或指定工作表不存在!請返回確認!");54
}55
finally56

{57
int i=0,ii=0,iii=0;58
try59

{60
if(dtbSearch.Rows.Count>0)61

{62
Thread.Sleep(200);63
statusBarPanel4.Text="正在導入數據,請稍候
";64

65
foreach(DataRow dr1 in dtbSearch.Rows)66

{67
if(dr1[0].ToString().Trim().Replace("'","")!="")68

{69
if(GetDBTotalNum("料號='"+dr1[0].ToString().Trim().Replace("'","")+"'")==0)70

{71
StringBuilder strSql=new StringBuilder();72
strSql.Append("insert into 料號基本資料(");73
strSql.Append("料號,品名,訂購規格,材質,數量,狀態,熱處理,廠商,備註");74
strSql.Append(")");75
strSql.Append(" values (");76
strSql.Append("'"+dr1[0].ToString().Trim().Replace("'","")+"',");77
strSql.Append("'"+dr1[2].ToString().Trim().Replace("'","")+"',");78
strSql.Append("'"+dr1[3].ToString().Trim().Replace("'","")+"',");79
strSql.Append("'"+dr1[4].ToString().Trim().Replace("'","")+"',");80
strSql.Append("'"+dr1[5].ToString().Trim().Replace("'","")+"',");81
strSql.Append("'"+dr1[6].ToString().Trim().Replace("'","")+"',");82
strSql.Append("'"+dr1[7].ToString().Trim().Replace("'","")+"',");83
strSql.Append("'"+dr1[8].ToString().Trim().Replace("'","")+"',");84
strSql.Append("'"+dr1[8].ToString().Trim().Replace("'","")+"'");85
strSql.Append(")");86

87
ExectueSQL(strSql.ToString());88

89
i++;90
}91
else92

{93
ii++;//重複數據94
}95
}96
else97

{98
iii++;//空白數據99
}100
}101

102
this.statusBarPanel3.Text="數據總量: "+GetDBTotalNum("");103
104
progressBar1.Value=100;105

106
MessageBox.Show("Excel成功導入 ["+i+"] 條數據,忽略 ["+ii+"] 條重複數據,排除 ["+iii+"] 條空白數據!");107
statusBarPanel4.Text="數據導入完成!";108
}109
else110

{111
progressBar1.Value=0;112

113
MessageBox.Show("指定工作表不存在!請返回確認!");114
}115
}116
catch117

{118
MessageBox.Show("數據轉換時出錯!請檢查數據格式是否正確!");119
}120
}121
}測試PASS!