Good morning
I have an excel file from wich i want to extract the first or more column to a DataGreedView "as is".
File: c:\temp.xlsxSheet: "Sheet1"
Column: A:AI was able to understrand the code line by line and wrote its meaning but i dont know how to use the last piece of code and i get an error.
adapter.Fill(Sheet1, A:A);
dataGridView1.DataSource = Sheet1;(I dont know how to work with this nor understand the MSDN about it)The sourcecode i already have:
Div
// Abre janela para selecionar ficheiro a importar.
OpenFileDialog ofImport = new OpenFileDialog();
// Define propriedades para a janela de selecionar ficheiro.
ofImport.Title = "Select file";
ofImport.InitialDirectory = @"c:\";
// ofImport.FileName = "temp.xlsx";
ofImport.Filter = "Excel Sheet(*.xlsx)|*.xlsx|All Files(*.*)|*.*";
ofImport.FilterIndex = 1;
ofImport.RestoreDirectory = true;
if (ofImport.ShowDialog() == DialogResult.OK)
{
// Define Caminho completo para ficheiro em STRING.
string path = System.IO.Path.GetFullPath(ofImport.FileName);
// Define pedido a fazer em forma de STRING.
string query = "SELECT * FROM [Sheet1$]";
// Define ligação como nova ligação OleDB.
OleDbConnection conn = new OleDbConnection();
// Define linha de comunicação com todos os dados recuperados até aqui.
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ofImport.FileName + ";Extended Properties=" + "\"Excel 12.0 Xml;HDR=YES;IMEX=1\"";
// Define adaptador para pedido "query" (tipo string) ao adaptador que comunica com o ficheiro excel através da ligação CONN definida anteriormente.
OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn);
DataSet dataSet = new DataSet();
adapter.Fill(Sheet1, A:A);
dataGridView1.DataSource = Sheet1;
}
else
{
ofImport.Dispose();
}