日期:2014-05-17  浏览次数:20527 次

Excel导入到GridView并保存到sqlserver中
HttpPostedFile p = fuExcel.PostedFile;
  string filename = DateTime.Now.ToString("yyyMMddHHmm") + Path.GetFileName(p.FileName);
  if (!Directory.Exists(Server.MapPath(@"~/upload" + "//")))
  Directory.CreateDirectory(Server.MapPath(@"~/upload" + "//"));
  string filePath = Server.MapPath(@"~/upload" + "//" + filename);
  fuExcel.SaveAs(filePath);
  if (filename != "")
  {
  if (filename.Contains("xls"))
  {
  OleDbConnection oleCon = new OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'");
  oleCon.Open();
  string sql = "select * from [Sheet1$]";
  OleDbDataAdapter mycommand = new OleDbDataAdapter(sql, oleCon);
  DataSet ds = new DataSet();
  mycommand.Fill(ds, "[Sheet1$]");
  oleCon.Close();
  int count = ds.Tables["[Sheet1$]"].Rows.Count;
  for (int i = 0; i < count; i++)
  {
  string Log_ship_unit = ds.Tables["[Sheet1$]"].Rows[i]["出货单位"].ToString().Trim();
  DateTime Log_doc_date = Convert.ToDateTime(ds.Tables["[Sheet1$]"].Rows[i]["单据时间"].ToString().Trim());
  DateTime Log_delivery_date = Convert.ToDateTime(ds.Tables["[Sheet1$]"].Rows[i]["发货时间"].ToString().Trim());
  string Log_customers = ds.Tables["[Sheet1$]"].Rows[i]["客户"].ToString().Trim();
  string Log_user = ds.Tables["[Sheet1$]"].Rows[i]["用户(物流单显示的全称)"].ToString().Trim();
  string Log_logistics_order = ds.Tables["[Sheet1$]"].Rows[i]["物流单号"].ToString().Trim();
  string Log_outbound_number = ds.Tables["[Sheet1$]"].Rows[i]["出库单号"].ToString().Trim();
  string Log_state = ds.Tables["[Sheet1$]"].Rows[i]["状态"].ToString().Trim();
  string Log_insurance = ds.Tables["[Sheet1$]"].Rows[i]["投保"].ToString().Trim();
  string Log_transport_unit = ds.Tables["[Sheet1$]"].Rows[i]["承运单位"].ToString().Trim();
  string Log_transport_order = ds.Tables["[Sheet1$]"].Rows[i]["承运单号"].ToString().Trim();
  double Log_cost = Convert.ToDouble(ds.Tables["[Sheet1$]"].Rows[i]["成本"].ToString().Trim());
  string Log_settlement_way = ds.Tables["[Sheet1$]"].Rows[i]["结算方式"].ToString().Trim();
  string Log_ship_people = ds.Tables["[Sheet1$]"].Rows[i]["发货人"].ToString().Trim();
  DateTime Log_expected_arrive = Convert.ToDateTime(ds.Tables["[Sheet1$]"].Rows[i]["预计到达"].ToString().Trim());
  DateTime Log_served_time = Convert.ToDateTime(ds.Tables["[Sheet1$]"].Rows[i]["送达时间"].ToString().Trim());
  string Log_track_cond_one = ds.Tables["[Sheet1$]"].Rows[i]["追踪情况1"].ToString().Trim();