/// 操作EXCEL 到数据库
///
private void OpterData()
{
if(txtExcelPath.Text.Trim()=="") return;
string strSql=string.Empty;
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\";"+"data source=" + txtExcelPath.Text;
strSql = "Select * From [成绩$]";//数学//语文//英语
OleDbConnection con1 = null;
try
{
con1 = new OleDbConnection(connStr);
OleDbDataAdapter pter1 = new OleDbDataAdapter(strSql,con1);
con1.Open();
DataTable dtb1 = new DataTable();
DataSet ds = new DataSet();
pter1.Fill(ds);
dtb1=ds.Tables[0];
SaveToSql cls1 = new SaveToSql();
cls1.Server = txtServer.Text;
cls1.DataBase = txtDataBase.Text;
cls1.Uid = txtUid.Text;
cls1.Pwd = txtPwd.Text;
int i=0;
progressBar1.Value =0;
progressBar1.Maximum = dtb1.Rows.Count;
foreach(DataRow drw in dtb1.Rows)
{
cls1.strNewCodeId = drw[0].ToString();
cls1.strNewCode = drw[1].ToString();
cls1.strNewDes = drw[2].ToString();
cls1.strOldCodeId = drw[3].ToString();
cls1.strOldCode = drw[4].ToString();
cls1.strOldDes = drw[5].ToString();
cls1.TableNames = "GPMS_Code_Psn"; //目的表名
cls1.Save();
i++;
progressBar1.Value=i;
}
MessageBox.Show("导入成功!");
}
catch(Exception Ex)
{
MessageBox.Show(Ex.Message);
}
finally
{
con1.Dispose();
}
}
网络转载,版权归原作者所有