首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

Excel导入到GridView并保留到sqlserver中

2012-08-01 
Excel导入到GridView并保存到sqlserver中HttpPostedFile p fuExcel.PostedFilestring filename DateT

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();
  string Log_track_cond_two = ds.Tables["[Sheet1$]"].Rows[i]["追踪情况2"].ToString().Trim();


  }
  GridView1.DataSource = ds;
  GridView1.DataBind();
  Response.Write("<script>window.alert('导入成功!');</script>");
  }
  else
  {
  Response.Write("<script>window.alert('请检查您选择的文件是否为Excel文件!');</script>");
  return;
  }
  }
  else
  {
  Response.Write("<script>window.alert('请先选择导入文件后,在执行导入!');</script>");
  return;
  }


oleCon.Open();红色字体地方提示“外部表不是预期的格式。”外面用的是office2010生成的Excel文件,格式没有改变

[解决办法]
xlsx文件使用这个写法


OleDbConnection oleCon = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath +";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";");
不是
OleDbConnection oleCon = new OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'");
[解决办法]
ACE OLEDB 12.0下载
http://www.microsoft.com/zh-cn/download/details.aspx?id=13255
[解决办法]
你的隐藏是visiable:false吗?如果是改为Style="display:none;"
[解决办法]
07的

C# code
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + Server.MapPath(aa) + ";" + "Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1;\""; 

热点排行