读取Excel中的数据
- public DataSet GetData()
- {
- string strConn;
- strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
- @"Data Source=E:\\公司的资料\复件 (6) WJ业务库管明细表.xls;" +
- "Extended Properties=Excel 8.0;";
- OleDbConnection conn = new OleDbConnection(strConn);
- OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [MAC$]", strConn);
- DataSet myDataSet = new DataSet();
- myCommand.Fill(myDataSet);
- return myDataSet;
- }
插入到数据库中
- public int ImportData(DataSet ds)
- {
- int counts = 0;
- if (ds.Tables.Count != 0 && ds.Tables[0].Rows.Count != 0)
- {
- SqlConnection con = OpenCon();
- con.Open();
- SqlCommand myCommand = new SqlCommand();//0序号 2购入时间 4保修期 5购供单位 7备注 8sstate
- myCommand.Connection = con;
- for (int i = 75; i < ds.Tables[0].Rows.Count; i++)
- {
- //质保年数
- string strovertime = ds.Tables[0].Rows[i][4].ToString();
- //购买日期
- string dtbuytime = ds.Tables[0].Rows[i][2].ToString();
- //质保期
- DateTime overtime = new DateTime();
- //购买时间
- DateTime buytime = new DateTime();
- if (dtbuytime.Length != 0)
- {
- try
- {
- buytime = Convert.ToDateTime(dtbuytime);
- }
- catch (Exception ex)
- { }
- }
- if (strovertime.Length != 0 && dtbuytime.Length != 0)
- {
- if (strovertime != "质保期")
- {
- if (strovertime == "18个月")
- {
- overtime = Convert.ToDateTime(dtbuytime).AddMonths(18);
- }
- }
- }
- //1序列号
- string snum = ds.Tables[0].Rows[i][1].ToString();
- //3单价
- string sprice = ds.Tables[0].Rows[i][3].ToString();
- //采购公司
- string company = ds.Tables[0].Rows[i][5].ToString();
- //备注
- string describe = ds.Tables[0].Rows[i][7].ToString();
- //sstate
- string sstate = ds.Tables[0].Rows[i][8].ToString();
- //6使用站点
- string current = ds.Tables[0].Rows[i][6].ToString();
- if (overtime.ToString() != "0001-1-1 0:00:00" && buytime.ToString() != "0001-1-1 0:00:00")
- myCommand.CommandText = "insert into storemanagerTable(snum,sOvertime,stime,sprice,svendor,[current],sdescribe,sstate) values ('" + snum + "','" + overtime + "','" + buytime + "','" + sprice + "','" + company + "','" + current + "','" + describe + "','" + sstate + "')";
- else
- {
- if (overtime.ToString() == "0001-1-1 0:00:00" && buytime.ToString() == "0001-1-1 0:00:00")
- myCommand.CommandText = "insert into storemanagerTable(snum,sprice,svendor,[current],sdescribe,sstate) values ('" + snum + "','" + sprice + "','" + company + "','" + current + "','" + describe + "','" + sstate + "')";
- else if (overtime.ToString() == "0001-1-1 0:00:00")
- myCommand.CommandText = "insert into storemanagerTable(snum,stime,sprice,svendor,[current],sdescribe,sstate) values ('" + snum + "','" + buytime + "','" + sprice + "','" + company + "','" + current + "','" + describe + "','" + sstate + "')";
- else if (buytime.ToString() == "0001-1-1 0:00:00")
- myCommand.CommandText = "insert into storemanagerTable(snum,sOvertime,sprice,svendor,[current],sdescribe,sstate) values ('" + snum + "','" + overtime + "','" + sprice + "','" + company + "','" + current + "','" + describe + "','" + sstate + "')";
- }
- counts += myCommand.ExecuteNonQuery();//由于增加了记录,所以受影响的行数
- }
- OpenCon().Close();
- }
- return counts;
- }
这是调用
- DataSet ds = GetData();
- if (ds.Tables[0].Rows.Count != 0)
- {
- if (ImportData(ds) != 0)
- {
- Response.Write("OK");
- }
- else
- {
- Response.Write("error");
- }
- }
有什么不明白或错误的地方,请您发消息,共同进步
转载于:https://blog.51cto.com/liguo125/456106