TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Rui Ruivo
NA
132
37.6k
from Excel to DataGreedView
Jun 13 2013 5:16 AM
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.xlsx
Sheet: "Sheet1"
Column: A:A
I 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();
}
Reply
Answers (
4
)
What is the problem in this code?
Problem wth separation technique of 3layer architecture