读取Excel中的数据

  1. public DataSet GetData()  
  2.    {  
  3.        string strConn;  
  4.        strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +  
  5.        @"Data Source=E:\\公司的资料\复件 (6) WJ业务库管明细表.xls;" +  
  6.        "Extended Properties=Excel 8.0;";  
  7.        OleDbConnection conn = new OleDbConnection(strConn);  
  8.        OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [MAC$]", strConn);  
  9.        DataSet myDataSet = new DataSet();  
  10.        myCommand.Fill(myDataSet);  
  11.        return myDataSet;  
  12.    } 

插入到数据库中

  1. public int ImportData(DataSet ds)  
  2. {  
  3.     int counts = 0;  
  4.     if (ds.Tables.Count != 0 && ds.Tables[0].Rows.Count != 0)  
  5.     {  
  6.         SqlConnection con = OpenCon();  
  7.         con.Open();  
  8.         SqlCommand myCommand = new SqlCommand();//0序号       2购入时间   4保修期    5购供单位 7备注 8sstate  
  9.         myCommand.Connection = con;  
  10.         for (int i = 75; i < ds.Tables[0].Rows.Count; i++)  
  11.         {  
  12.             //质保年数  
  13.             string strovertime = ds.Tables[0].Rows[i][4].ToString();  
  14.             //购买日期  
  15.             string dtbuytime = ds.Tables[0].Rows[i][2].ToString();  
  16.             //质保期  
  17.             DateTime overtime = new DateTime();  
  18.             //购买时间  
  19.             DateTime buytime = new DateTime();  
  20.             if (dtbuytime.Length != 0)  
  21.             {  
  22.                 try 
  23.                 {  
  24.                     buytime = Convert.ToDateTime(dtbuytime);  
  25.                 }  
  26.                 catch (Exception ex)  
  27.                 { }  
  28.             }  
  29.             if (strovertime.Length != 0 && dtbuytime.Length != 0)  
  30.             {  
  31.                 if (strovertime != "质保期")  
  32.                 {  
  33.                     if (strovertime == "18个月")  
  34.                     {  
  35.                         overtime = Convert.ToDateTime(dtbuytime).AddMonths(18);  
  36.                     }  
  37.                 }  
  38.             }  
  39.             //1序列号  
  40.             string snum = ds.Tables[0].Rows[i][1].ToString();  
  41.             //3单价  
  42.             string sprice = ds.Tables[0].Rows[i][3].ToString();  
  43.             //采购公司  
  44.             string company = ds.Tables[0].Rows[i][5].ToString();  
  45.             //备注  
  46.             string describe = ds.Tables[0].Rows[i][7].ToString();  
  47.             //sstate  
  48.             string sstate = ds.Tables[0].Rows[i][8].ToString();  
  49.             //6使用站点  
  50.             string current = ds.Tables[0].Rows[i][6].ToString();  
  51.  
  52.             if (overtime.ToString() != "0001-1-1 0:00:00" && buytime.ToString() != "0001-1-1 0:00:00")  
  53.                 myCommand.CommandText = "insert into storemanagerTable(snum,sOvertime,stime,sprice,svendor,[current],sdescribe,sstate) values ('" + snum + "','" + overtime + "','" + buytime + "','" + sprice + "','" + company + "','" + current + "','" + describe + "','" + sstate + "')";  
  54.             else 
  55.             {  
  56.                 if (overtime.ToString() == "0001-1-1 0:00:00" && buytime.ToString() == "0001-1-1 0:00:00")  
  57.                     myCommand.CommandText = "insert into storemanagerTable(snum,sprice,svendor,[current],sdescribe,sstate) values ('" + snum + "','" + sprice + "','" + company + "','" + current + "','" + describe + "','" + sstate + "')";  
  58.                 else if (overtime.ToString() == "0001-1-1 0:00:00")  
  59.                     myCommand.CommandText = "insert into storemanagerTable(snum,stime,sprice,svendor,[current],sdescribe,sstate) values ('" + snum + "','" + buytime + "','" + sprice + "','" + company + "','" + current + "','" + describe + "','" + sstate + "')";  
  60.                 else if (buytime.ToString() == "0001-1-1 0:00:00")  
  61.                     myCommand.CommandText = "insert into storemanagerTable(snum,sOvertime,sprice,svendor,[current],sdescribe,sstate) values ('" + snum + "','" + overtime + "','" + sprice + "','" + company + "','" + current + "','" + describe + "','" + sstate + "')";  
  62.             }  
  63.             counts += myCommand.ExecuteNonQuery();//由于增加了记录,所以受影响的行数  
  64.         }  
  65.         OpenCon().Close();  
  66.     }  
  67.     return counts;  

这是调用

  1. DataSet ds = GetData();  
  2.                 if (ds.Tables[0].Rows.Count != 0)  
  3.                 {  
  4.                     if (ImportData(ds) != 0)  
  5.                     {  
  6.                         Response.Write("OK");  
  7.                     }  
  8.                     else 
  9.                     {  
  10.                         Response.Write("error");  
  11.                     }  
  12.                 }  

有什么不明白或错误的地方,请您发消息,共同进步