zoukankan      html  css  js  c++  java
  • asp.net 从Excel表导入数据到数据库中

    一、导入Excel的界面

    这个界面很简单,代码就不列出来了。
    二、导入的代码
    我分了两部分,第一部分是点击查看数据的代码,这个是将数据导入到DataTable里面,但是还没有导入到数据库里。这里需要注意的是当程序在服务器运行时,要先把导入的文件上传到服务器上,否则不能导入,会出现莫名奇妙的错误,为了改这个错误当初弄了好久,希望大家不要走我的弯路啊。如果是在自己的机器上就不用上穿文件。
    第二部分是选择相应的表,然后将数据导入到表里面,这部分很简单。
    1、

    try
            
    {
                InputDataBLL input 
    = new InputDataBLL();
                
    this.Label1.Text = "";
                
    if (this.FileUpload1.HasFile)
                
    {
                   
    // string filename = this.FileUpload1.PostedFile.FileName.ToString().Trim();
                    DataTable inputdt = new DataTable();
                    
    int len = this.FileUpload1.FileName.ToString().Trim().Length;
                    string path = "~/temp/upfile/"+this.FileUpload1 .FileName .ToString ().Trim();
                    path = Server.MapPath(path);
                    
    this.FileUpload1.SaveAs(path); //上传文件
                    inputdt = input.InputExcel(path, this.FileUpload1.FileName.ToString().Trim().Substring(0, len - 4),this.TextBox1.Text.Trim ());

                    
    if (Session["inputdt"!= null)
                        Session.Remove(
    "inputdt");
                    Session.Add(
    "inputdt", inputdt);
                    
    if (inputdt.Rows.Count > 0)
                    
    {
                        
    this.GridView1.DataSource = inputdt;
                        
    this.GridView1.DataBind();
                    }

                }

                
    else
                    
    throw new Exception("请选择导入表的路径");
            }

            
    catch (Exception ex)
            
    {
                Response.Write(
    "<script language='javascript'>alert('" + ex.Message + "');</script>");
            }


    导入的函数

     /// <summary>
        
    /// 导入数据到数据集中
        
    /// </summary>
        
    /// <param name="Path"></param>
        
    /// <param name="TableName"></param>
        
    /// <param name="tablename2">如果这个有就以他为表名,没有的话就以TableName</param>
        
    /// <returns></returns>

        public DataTable InputExcel(string Path,string TableName,string tablename2)
        
    {
            
    try
            
    {
                
    string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
                OleDbConnection conn 
    = new OleDbConnection(strConn);
                conn.Open();
                
    string strExcel = "";
                OleDbDataAdapter myCommand 
    = null;
                
    if (tablename2.Length > 0 && !tablename2.Equals(string.Empty))
                    TableName 
    = tablename2;
                strExcel 
    = "select * from [" + TableName + "$]";
                myCommand 
    = new OleDbDataAdapter(strExcel, strConn);
                DataTable dt 
    = new DataTable();
                myCommand.Fill(dt);
                conn.Close();
                
    return dt;
            }

            
    catch (Exception ex)
            
    {
                
    throw new Exception(ex.Message);
            }

        }

    二、将数据导入到数据库里
    这部分其实很简单,就是插入数据。

     if (this.DropDownList1.SelectedItem.Text.ToString().Equals("Material"))//导物料
                {
                    
    new StockBaseBLL().ISUserModel("物料导入", Response, Request, Server);
                    MaterialBLL material 
    = new MaterialBLL();
                  
         foreach (DataRow row in inputdt.Rows)//inputdt为刚刚从函数中返回的数据源
                    
    {
                        
    float MaterialPrice = 0.0f;
                        
    float MaterialTaxPrice = 0.0f;
                        
    float TaxRate = 0.0f;
                        
    float Moneys = 0.0f;
                        
    int temp=0;
                        
    if (row["单价"].ToString().Trim() != "")
                            MaterialPrice 
    = float.Parse(row["单价"].ToString().Trim());
                        
    if (row["含税单价"].ToString().Trim()!="")
                            MaterialTaxPrice
    =float.Parse(row["含税单价"].ToString().Trim());
                        
    if (row["税率"].ToString().Trim()!="")
                            TaxRate
    = float.Parse(row["税率"].ToString().Trim());
                        
    if (row["金额"].ToString().Trim()!="")
                            Moneys
    =float.Parse(row["金额"].ToString().Trim());

                        
    if (material.SelectMaterialsDynamic("MaterialID='" + row["物料长代码"].ToString() + "'""").Rows.Count <= 0//不存在,其实可以不要,因为编号是主键,如果相同则插不进去
                            temp = material.InsertMaterial(row["物料长代码"].ToString(), row["物料名称"].ToString(), row["单位"].ToString().Trim(), MaterialPrice, MaterialTaxPrice, TaxRate, Moneys, 0);
                        
    else
                            NotIntoID 
    += row["物料长代码"].ToString()+",";
                        
    if (temp > 0)
                                index 
    += temp;
                         temp 
    = 0;
                    }

                }
  • 相关阅读:
    Realtime crowdsourcing
    maven 常用插件汇总
    fctix
    sencha extjs4 command tools sdk
    首次吃了一颗带奶糖味的消炎药,不知道管用不
    spring mvc3 example
    ubuntu ati driver DO NOT INSTALL recommand driver
    yet another js editor on windows support extjs
    how to use springsource tools suite maven3 on command
    ocr service
  • 原文地址:https://www.cnblogs.com/hfzsjz/p/1922901.html
Copyright © 2011-2022 走看看