QC20N
asked on
SELECT * FROM EXCEL DOC
I have found this http://delphi.about.com/od/database/l/aa090903a.htm to see how I should connect to a excel doc. I have followed the guide, but when I try to do a SELECT as the guide says I should do. It dosen't work. My first sheet is "Sheet1" and I have the data as the guide.
The select I should use is:
SELECT * FROM [Sheet1$]
But it raise a exception in the FetchData procedure.
Excel 2007 is the format of my document.
The select I should use is:
SELECT * FROM [Sheet1$]
But it raise a exception in the FetchData procedure.
Excel 2007 is the format of my document.
procedure TFrmMain.ConnectToExcel;
var strConn : widestring;
begin
strConn:='Provider=Microsoft.ACE.OLEDB.12.0;' +
'Data Source=' + Edit2.Text + ';' +
'Extended Properties="Excel 12.0 Xml;HDR=YES";Persist Security Info=False';
AdoConnection1.Connected:=False;
AdoConnection1.ConnectionString:=strConn;
try
AdoConnection1.Open;
AdoConnection1.GetTableNames(ComboBox1.Items,True);
except
ShowMessage('Unable to connect to Excel, make sure the workbook ' + Edit2.Text + ' exist!');
raise;
end;
end;(*ConnectToExcel*)
procedure TFrmMain.FetchData;
begin
StatusBar1.SimpleText:='';
ConnectToExcel;
AdoQuery1.Close;
AdoQuery1.SQL.Text:=Edit3.Text;
try
AdoQuery1.Open;
except
ShowMessage('Unable to read data from Excel, make sure the query ' + Edit3.Text + ' is meaningful!');
raise;
end;
end;
procedure TFrmMain.Button1Click(Sender: TObject);
begin
FetchData;
end;
Exactly what exception is being raised?
ASKER
Well, it is in line 28. ADOQuery1.Open is where the exception is raised.
As the Exception handling code says, are you sure about the query text in the editbox (Edit3)?
ASKER
You see, that is my question. I have just followed the guide in the link and I can't see what the problem is. I was then hoping that someone could inform me if the SELECT command is ok.
Are you using the example from the delphi.about.com?
Because that test application works right.
However in my test, changing the provider from Jet to ACE I got an error saying "installable ISAM not found".
Is this your case?
Because that test application works right.
However in my test, changing the provider from Jet to ACE I got an error saying "installable ISAM not found".
Is this your case?
ASKER
It runs perfect until I do a ADOQuery1.Open. Do you have Excel 2007? Cause I have found out if you need to connect to excel 2007 your connectionstring should be "Provider=Microsoft.ACE.OL EDB.12.0;"
as this link http://www.connectionstrings.com/excel-2007#p84
as this link http://www.connectionstrings.com/excel-2007#p84
Yes of course.
Vista business Sp2 and Excel 2007
If I use ACE.OLEDB and the .xls file from delphi.about.com I got the ISAM error,
but using a .xlsl file (Excel 2007 version) everything is OK.
What about your xlsl file?
Could you attach your file and code so I can test it in my machine?
Vista business Sp2 and Excel 2007
If I use ACE.OLEDB and the .xls file from delphi.about.com I got the ISAM error,
but using a .xlsl file (Excel 2007 version) everything is OK.
What about your xlsl file?
Could you attach your file and code so I can test it in my machine?
ASKER
Yes, it was a typo.
Anyway I've tested your attached file also and it's working fine.
Even if the error you got could be different by finding the Sheet1, you already will got back that error.
Are you shure about the location of your file?
Anyway I've tested your attached file also and it's working fine.
Even if the error you got could be different by finding the Sheet1, you already will got back that error.
Are you shure about the location of your file?
In addition, I mean that i'm testing also it compiling the application in the same directory where the xlsx file is, writing in Edit2 just the filename without the whole path.
I repeat that it work fine
I repeat that it work fine
ASKER
So you are also using:
strConn:='Provider=Microso ft.ACE.OLE DB.12.0;' +
'Data Source=' + Edit2.Text + ';' +
'Extended Properties="Excel 12.0 Xml;HDR=YES";Persist Security Info=False';
strConn:='Provider=Microso
'Data Source=' + Edit2.Text + ';' +
'Extended Properties="Excel 12.0 Xml;HDR=YES";Persist Security Info=False';
ASKER
And also using:
SELECT * FROM [Sheet1$]
SELECT * FROM [Sheet1$]
Yes of course!
Try it by yourself
Try it by yourself
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That is very strange. Yes, it seems to work fine for now.