0

I am trying to import one column from an excel file to one column in datagridview .. now the idea of importing could be done by selecting all data into datagridview but how can i select only one column from excel file to only one column in datatgridview after comparing all data in both of them and make sure they are equal ? I mean is there any statement that can select this column from excel file into the other specific column in datagridview where all data in both of them is equal ?

Thank you all in advance

Note : I don't know if the idea is clear but you can ask me when more clarification is needed

A.Wad
  • 29
  • 3
  • 9
  • Here is a StackOverflow link you can reference many examples and additional links within this post http://stackoverflow.com/questions/207693/how-to-query-excel-file-in-c-sharp-using-a-detailed-query – MethodMan Aug 14 '12 at 23:41

1 Answers1

0

here is something that you can also try for starters mess around with it

if you need an example that uses Ranges..please let me know and I can post an example of that as well but this should be enough to get you started. Change the code Sorce= to match your file path of the Excel file

using System.Data;
using System.Data.OleDb;
...
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Book1.xls;Extended Properties=Excel 8.0");
OleDbDataAdapter da = new OleDbDataAdapter("select * from MyObject", con);
DataTable dt = new DataTable();
da.Fill(dt);

If you want a full example of how to read the excel file here is another example

Below is the whole code required for reading the Excel file.

void Read_My_Excel_File()
{

// Test.xls is in the C:\

string connectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + C:\Test.xls + ";";

connectionString += "Extended Properties=Excel 8.0;";

// always read from the sheet1.
OleDbCommand myCommand = new OleDbCommand("Select * from [Sheet1$];");
OleDbConnection myConnection = new OleDbConnection(connectionString);
myConnection.Open();
myCommand.Connection = myConnection;
OleDbDataReader myReader = myCommand.ExecuteReader();

while (myReader.Read())
{

// it can read upto 5 columns means A to E. In your case if the requirement is different then change the loop limits a/c to it.

for (int i = 0; i < 5; i++)
{
  Response.Write(myReaderIdea.ToString() + " ");
}
Response.Write("<br>");
}
myConnection.Close();
}
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • Hallo DJ KRAZE ... Thank you very much for the help now the code you worte is regarding reading the whole excel and import it what about reading the whole excel compare with data in datagridview and import the (comment) column only when the comparison result is equal – A.Wad Aug 15 '12 at 08:28
  • you could change this OleDbCommand myCommand = new OleDbCommand("Select * from [Sheet1$];"); to Select the Comment Column from the Excel Sheet – MethodMan Aug 15 '12 at 18:21