Excel匯入到SQL Server ·····返回試算表中某個表並形成datatable

來源:互聯網
上載者:User

格式一定要統一,要不然程式就不好以什麼為標準進行判斷,
#region 返回開啟的試算表中的某個表並形成datatable
/// <summary>
/// 返回開啟的試算表中的某個表並形成datatable
/// </summary>
/// <param name="path">檔案絕對路徑</param>
/// <param name="target">要開啟的表名</param>
/// <returns>返回DATATABLE資料庫集</returns>

private void ReadExcel()
{       
 try
 {
  DataTable rs=new DataTable();
  rs=null;
  DataSet rDs=null;
  string strConn="";
  int count=0;
  string id =Request["id"];
  DateTime createtime=DateTime.Now ;
  string date=createtime.Year.ToString()+"-"+createtime.Month.ToString ()+"-"+createtime.Day.ToString ()+" "+createtime.Hour.ToString()+":"+createtime.Minute.ToString()+":"+createtime.Second.ToString();
  string sqlstr=" select * from tab_uppart_file where id ="+id+"";   
  string userid=data1.readout(sqlstr,1);
  string fileName=data1.readout(sqlstr,3);      
  string path = Server.MapPath("../upparts/"+fileName);  

  string filepath="..\\upparts\\"+fileName;
  if (File.Exists (Server.MapPath(filepath)))//判斷檔案是否存在
  {
   strConn ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+path+";Extended Properties='Excel 8.0;HDR=YES;IMEX=1';"; //IMEX=1 用來轉換文本  
   OleDbConnection conn = new OleDbConnection(strConn);     
   OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$]",strConn);
   DataSet myDataSet = new DataSet();   
   myCommand.Fill(myDataSet,"[Sheet1$]");
   foreach(DataRow row in myDataSet.Tables[0].Rows)
   {
    count++;
    string classid="";
    string name="";
    string model="";
    string price="";
    string num="";
    string mfg="";
    string package="";
    string remark="";
    classid=row["產品類型"].ToString();
    name=row["產品名稱"].ToString();
    model=row["產品型號"].ToString();
    price=row["產品價格"].ToString();
    num=row["數    量"].ToString();
    mfg=row["生產廠商"].ToString();
    package=row["封    裝"].ToString();
    remark=row["交易說明"].ToString();
    if (name!="" && model!="")
    {
     sqlstr="insert into tab_part values('"+name+"','"+model+"','"+num+"','"+package+"','"+mfg+"','"+price+"','"+remark+"','"+classid+"','"+createtime+"','"+userid+"','0')";
     data1.moddata(sqlstr);
    }
   }
   string updatesql="update tab_uppart_file set flag='1',uploadtime='"+date+"' where id='"+id+"'";
   data1.moddata(updatesql); 
   data1.Alert2 ("上傳記錄成功!","BatchAddParts.aspx",Page);
  }
  else
  {
   data1.Alert2("沒有找到相應的模板檔案","BatchAddParts.aspx",Page);  
  } 
 }
 catch(System.Data.OleDb.OleDbException  ex)
 {
  Response.Write(ex.Message );
 }

}
#endregion

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.