?
Solved

SELECT * FROM EXCEL DOC

Posted on 2010-01-06
15
Medium Priority
?
619 Views
Last Modified: 2012-05-08
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

0
Comment
Question by:QC20N
  • 7
  • 7
15 Comments
 
LVL 4

Expert Comment

by:irishbuddha
ID: 26192014
Exactly what exception is being raised?
0
 

Author Comment

by:QC20N
ID: 26197957
Well, it is in line 28. ADOQuery1.Open is where the exception is raised.
0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 26198063
As the Exception handling code says, are you sure about the query text in the editbox (Edit3)?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:QC20N
ID: 26198151
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.
0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 26198480
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?
0
 

Author Comment

by:QC20N
ID: 26198654
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
0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 26198782
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?

0
 

Author Comment

by:QC20N
ID: 26198836
xlsl - you say? My excelfile is xlsx.

I will attach it.
AdoDelphiExcel.xlsx
0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 26198932
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?
0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 26198972
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
0
 

Author Comment

by:QC20N
ID: 26199260
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';
0
 

Author Comment

by:QC20N
ID: 26199267
And also using:

SELECT * FROM [Sheet1$]
0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 26199533
Yes of course!
Try it by yourself
0
 
LVL 23

Accepted Solution

by:
Ferruccio Accalai earned 1000 total points
ID: 26199545
Sorry, forgot to attach my files
AdoDelphiExcel.zip
0
 

Author Comment

by:QC20N
ID: 26199690
That is very strange. Yes, it seems to work fine for now.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Screencast - Getting to Know the Pipeline
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question