private void button2_Click(object sender, System.EventArgs e) { string str_School_ID, SchoolPassword; int i; useExcel = true; excelApp = new Excel.ApplicationClass(); Excel.Workbook excelBook = excelApp.Workbooks.Add(1); Excel.Worksheet excelSheet = (Excel.Worksheet)excelBook.Worksheets[1]; if (textBox_openFile.Text == "") { MessageBox.Show("必须输入争取文件名!", "警告!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } string Excelfileloc = textBox_openFile.Text; string Excelfilename = conn.GetLastStr(Excelfileloc, "\\"); string Excelfiletype = conn.GetLastStr(Excelfilename, ".").Trim(); if (Excelfiletype != "xls") { MessageBox.Show("必须输入Excel文件!", "警告!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } //创建一个数据链接 string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + Excelfileloc + ";Extended Properties=Excel 8.0"; OleDbConnection myConn = new OleDbConnection(strCon); string strCom = " SELECT * FROM [Sheet1$] "; myConn.Open(); //打开数据链接,得到一个数据集 OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn); //创建一个 DataSet对象 myDataSet = new DataSet(); //f得到自己的DataSet对象 myCommand.Fill(myDataSet, "[Sheet1$]"); //关闭此数据链接 myConn.Close(); //取出各行数据,第二列学校名,第一列School_ID—>算出来有效的密码,存入myDataSet for (i = 0; i < myDataSet.Tables[0].Rows.Count; i++) { str_School_ID = myDataSet.Tables[0].Rows[i][0].ToString(); SchoolPassword = conn.ValidUploadPassword(str_School_ID); myDataSet.Tables[0].Rows[i][2] = SchoolPassword; } //逐行填入 for (i = 0; i < myDataSet.Tables[0].Rows.Count; i++) { excelSheet.Cells[i, 1] = myDataSet.Tables[0].Rows[i][0].ToString(); excelSheet.Cells[i, 2] = myDataSet.Tables[0].Rows[i][1].ToString(); excelSheet.Cells[i, 3] = myDataSet.Tables[0].Rows[i][2].ToString(); } excelApp.Visible = true; }