2010-12-11 8 views
0

私は2枚のExcelシートを持っています。私の要件は、私は別のExcelシートからすべてのデータをコピーする必要があります。以下は同じコードスニペットです。下のコードでは、すべてのデータをExcelの1つのシートから別のシートにコピーして貼り付けていますが、A1の範囲からデータを貼り付けることはありません。選択したデータをOL​​EDBを使用して別のExcelシートに貼り付けるにはどうすればよいですか?

最初の行から別のExcelシートに貼り付けたいデータを選択します。また、ボタンをクリックするたびに新しいデータを削除して挿入したいのではないかと思います。助けてください。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.Common; 
using Excel = Microsoft.Office.Interop.Excel; 
using System.Reflection; 

namespace TestExcelSheet 
{ 
    public partial class Form1 : Form 
    { 
     string path = @"C:\Users\Bhawesh\Documents\Visual Studio 2010\Projects\TestExcelSheet\DataValidationTest.xlsx"; 
     string pathdestination = @"C:\Users\Bhawesh\Documents\Visual Studio 2010\Projects\TestExcelSheet\DataValidationTest - updated.xlsx"; 


     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       //Source Path 
       string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;"; 
       DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb"); 
       DbDataAdapter adapter = factory.CreateDataAdapter(); 
       DbCommand selectCommand = factory.CreateCommand(); 
       selectCommand.CommandText = "SELECT [Job Code], [Job Name],Format([Start Date],'MM-DD-YYYY') as [Start Date], Format([End Date],'MM-DD-YYYY') as [End Date] FROM [Job$]"; 
       DbConnection connection = factory.CreateConnection(); 
       connection.ConnectionString = connectionString; 
       selectCommand.Connection = connection; 
       adapter.SelectCommand = selectCommand; 
       DataSet Job = new DataSet(); 
       adapter.Fill(Job); 
       for (int iRowCount = 0; iRowCount <= Job.Tables[0].Rows.Count; iRowCount++) 
       { 

         string JobCode = ""; 
         string JobName = ""; 
         string StartDate = ""; 
         string EndDate = ""; 
         JobCode = Job.Tables[0].Rows[iRowCount]["Job Code"].ToString().Trim(); 
         JobName = Job.Tables[0].Rows[iRowCount]["Job Name"].ToString().Trim(); 
         StartDate = Job.Tables[0].Rows[iRowCount]["Start Date"].ToString().Trim(); 
         EndDate = Job.Tables[0].Rows[iRowCount]["End Date"].ToString().Trim(); 

         //Destination Path 
         string connectionStringdest = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pathdestination + ";Extended Properties=Excel 12.0;"; 
         DbProviderFactory factorydest = DbProviderFactories.GetFactory("System.Data.OleDb"); 
         DbDataAdapter adapterdest = factorydest.CreateDataAdapter(); 
         DbCommand insertCommand = factorydest.CreateCommand(); 
         DbConnection connectiondest = factorydest.CreateConnection(); 
         connectiondest.ConnectionString = connectionStringdest; 
         insertCommand.Connection = connectiondest; 
         adapterdest.InsertCommand = insertCommand; 


         if (connectiondest.State == ConnectionState.Closed) 
         { 
          connectiondest.Open(); 
         } 
         if (Job.Tables.Count > 0 && Job.Tables[0].Rows.Count > 0) 
         { 

           insertCommand.CommandText = "Insert into [Job_updated$] values ('" + JobCode + "', '" + JobName + "', '" + StartDate + "', '" + EndDate + "') "; 
           //insertCommand.CommandText = "UPDATE [Job_updated$E1:E1] SET F1='MyNewCol';"; 
           insertCommand.ExecuteNonQuery(); 
          insertCommand.Dispose(); 
         } 
         connectiondest.Close(); 
         dataGridView1.DataSource = Job.Tables[0].DefaultView; 
        } 
       } 

      catch (Exception ex) 
      { 
       MessageBox.Show(ex.ToString()); 
      } 

     } 

    } 
} 

答えて

0

が、その最初の行がテーブルのヘッダーであると考えられるため、A1の範囲からデータを貼り付けていない(シート) それらを手動で作成するか、またはそれを行うためにgetRangeを使用しますか。

// Create an array for the headers and add it to cells A1:C1. 
object[] objHeaders = {"Order ID", "Amount", "Tax"}; 
m_objRange = m_objSheet.get_Range("A1", "C1"); 
m_objRange.Value = objHeaders; 

たびに OLEDBを挿入する前に削除コマンドを実行し、新しいデータを挿入するためには、deleteコマンドにエクセルの詳細な機能については、このリンクはHere

ファイル

Excel.Range ran = (Excel.Range)sheet.Rows[5, Type.Missing]; 
ran.Select(); 
ran.Delete(Excel.XlDirection.xlUp); 

チェックをsopportていません

関連する問題