求求各位高手,帮我这个菜鸟解决一下这个问题。我对编程基本上一窍不通,所以请尽量详细,最好是有详细代码。

解决方案 »

  1.   

    大体思路是:将EXCEL的数据提出放在数据集中,在过循环将主表数据插入,在通过循环将从表插入。
    代码如下:private void button1_Click(object sender, System.EventArgs e)
    {
    //选择文件
      ofdSelectExcel.Filter = "Excel Files(*.xls)|*.xls";
      ofdSelectExcel.RestoreDirectory = true;
      if( ofdSelectExcel.ShowDialog() == DialogResult.OK )
      {
     if ( ofdSelectExcel.FileName.Trim().Length == 0)
     {
       MessageBox.Show(this,"Please select a excel file first!");
       return;
     }
     else
     {
      ImportExcelToSqlServer(ofdSelectExcel.FileName.Trim());
     }
            
     }
    }********************************************************
    提取数据
    public  void ImportExcelToSqlServer(string fileName)
    {
     if (fileName == null)
     {
      throw new ArgumentNullException("filename string is null!");
     } if (fileName.Length == 0)
     {
      throw new ArgumentException("filename string is empty!");
     } string oleDBConnString = String.Empty;
     oleDBConnString = "Provider=Microsoft.Jet.OLEDB.4.0;";
     oleDBConnString += "Data Source=";
     oleDBConnString += fileName;
     oleDBConnString += ";Extended Properties=Excel 8.0;";
     OleDbConnection oleDBConn = null;
     OleDbDataAdapter oleAdMaster = null;
     DataTable m_tableName=new DataTable();;
     DataSet ds=new DataSet();
     try
     {
        oleDBConn = new OleDbConnection(oleDBConnString);
        oleDBConn.Open();
        m_tableName=oleDBConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null);
       
        if (m_tableName != null && m_tableName.Rows.Count > 0)
        {  m_tableName.TableName =m_tableName.Rows[0]["TABLE_NAME"].ToString();    }
      string sqlMaster;
      sqlMaster=" SELECT *  FROM ["+m_tableName+"]";
      oleAdMaster=new OleDbDataAdapter(sqlMaster,oleDBConn);
      oleAdMaster.Fill(ds,"m_tableName");
        
      MailRebateManager manger=new MailRebateManager();
      bool isSucess=manger.AddExceLGmailRebate(ds.Tables["m_tableName"],ApplicationVariable.HomeCompanyID);
      if(isSucess)
      {
       MessageBox.Show("Manipulate Succs!");
      }
      else
      {
        MessageBox.Show("Manipulate Failed");
      }
     }
     catch(Exception ex)
     {
        MessageBox.Show(ex.Message);
        SimpleLogger.Log(ex);
      try
      {  }
      catch (OleDbException e)
      {
       SimpleLogger.Log(e);
             MessageBox.Show("An exception of type " + e.GetType() +");
      }
     }    
    }
      

  2.   

    private void button1_Click(object sender, System.EventArgs e)
    {
    //选择文件
      ofdSelectExcel.Filter = "Excel Files(*.xls)|*.xls";
      ofdSelectExcel.RestoreDirectory = true;
      if( ofdSelectExcel.ShowDialog() == DialogResult.OK )
      {
     if ( ofdSelectExcel.FileName.Trim().Length == 0)
     {
       MessageBox.Show(this,"Please select a excel file first!");
       return;
     }
     else
     {
      ImportExcelToSqlServer(ofdSelectExcel.FileName.Trim());
     }
            
     }
    }********************************************************
    提取数据
    public  void ImportExcelToSqlServer(string fileName)
    {
     if (fileName == null)
     {
      throw new ArgumentNullException("filename string is null!");
     } if (fileName.Length == 0)
     {
      throw new ArgumentException("filename string is empty!");
     } string oleDBConnString = String.Empty;
     oleDBConnString = "Provider=Microsoft.Jet.OLEDB.4.0;";
     oleDBConnString += "Data Source=";
     oleDBConnString += fileName;
     oleDBConnString += ";Extended Properties=Excel 8.0;";
     OleDbConnection oleDBConn = null;
     OleDbDataAdapter oleAdMaster = null;
     DataTable m_tableName=new DataTable();;
     DataSet ds=new DataSet();
     try
     {
        oleDBConn = new OleDbConnection(oleDBConnString);
        oleDBConn.Open();
        m_tableName=oleDBConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null);
       
        if (m_tableName != null && m_tableName.Rows.Count > 0)
        {  m_tableName.TableName =m_tableName.Rows[0]["TABLE_NAME"].ToString();    }
      string sqlMaster;
      sqlMaster=" SELECT *  FROM ["+m_tableName+"]";
      oleAdMaster=new OleDbDataAdapter(sqlMaster,oleDBConn);
      oleAdMaster.Fill(ds,"m_tableName");
        
      MailRebateManager manger=new MailRebateManager();
      bool isSucess=manger.AddExceLGmailRebate(ds.Tables["m_tableName"],ApplicationVariable.HomeCompanyID);
      if(isSucess)
      {
       MessageBox.Show("Manipulate Succs!");
      }
      else
      {
        MessageBox.Show("Manipulate Failed");
      }
     }
     catch(Exception ex)
     {
        MessageBox.Show(ex.Message);
        SimpleLogger.Log(ex);
      try
      {  }
      catch (OleDbException e)
      {
       SimpleLogger.Log(e);
             MessageBox.Show("An exception of type " + e.GetType() +");
      }
     }    
    }
      

  3.   

    小刀可乐:你的解答很详细啊,能留下你的QQ\MSN或者是邮箱吗,请教一下.