Link to home
Start Free TrialLog in
Avatar of QC20N
QC20NFlag for Denmark

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.
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;

Open in new window

Avatar of irishbuddha
irishbuddha
Flag of United States of America image

Exactly what exception is being raised?
Avatar of QC20N

ASKER

Well, it is in line 28. ADOQuery1.Open is where the exception is raised.
Avatar of Ferruccio Accalai
As the Exception handling code says, are you sure about the query text in the editbox (Edit3)?
Avatar of QC20N

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?
Avatar of QC20N

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.OLEDB.12.0;"

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?

Avatar of QC20N

ASKER

xlsl - you say? My excelfile is xlsx.

I will attach it.
AdoDelphiExcel.xlsx
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?
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
Avatar of QC20N

ASKER

So you are also using:

strConn:='Provider=Microsoft.ACE.OLEDB.12.0;' +
          'Data Source=' + Edit2.Text + ';' +
          'Extended Properties="Excel 12.0 Xml;HDR=YES";Persist Security Info=False';
Avatar of QC20N

ASKER

And also using:

SELECT * FROM [Sheet1$]
Yes of course!
Try it by yourself
ASKER CERTIFIED SOLUTION
Avatar of Ferruccio Accalai
Ferruccio Accalai
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of QC20N

ASKER

That is very strange. Yes, it seems to work fine for now.