I read excel files using ADO and functions like below
procedure TForm1.ConnectToExcel;
var strConn : widestring;
begin
strConn:='Provider=Microsoft.Jet.OLEDB.4.0;' +
'Data Source=' + Edit1.Text + ';' +
'Extended Properties=Excel 8.0;';
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 ' + Edit1.Text + ' exist!');
raise;
end;
end;(*ConnectToExcel*)
procedure TForm1.FetchData;
begin
StatusBar1.SimpleText:='';
if not AdoConnection1.Connected then ConnectToExcel;
AdoQuery1.Close;
AdoQuery1.SQL.Text:='select * from' + SheetName;
try
AdoQuery1.Open;
except
ShowMessage('Unable to read data from Excel, make sure the query ' + SheetName + ' is meaningful!');
raise;
end;
end;
for several excel files this code worked quite well but now I try to open a new one with a strange error.
Inside the excel file the worksheet name is MYLIST, if i retrieve the list of Worksheet names i get the name MYLIST$ back. I can not open this worksheet even with MYLIST or MYLIST$. What might be the problem .
Microsoft Jet Engine does not allow "$" symbols in identifiers, only in built-in functions. If you use this symbol in your names, these identifiers should be quoted.
So try this:
select * from [MYLIST$]
Related
I am using Delphi to write a program which builds a Pivot Table. Because I want to use Distinct Count, I have to 'Add to Data Model', which apparently creates an OLAP cube in the background. This means that I cannot use the "normal" Excel_TLB unit, but instead have to use the GetActiveOLE based routines...
I can connect, I can build the data model, I can create the pivot cache and pivot table object, but when I go to add the fields to the Pivot, I get an error at runtime, normally 'Member not Found'.
When I step through my code, after I create the PivotTable object, I can go to excel, and I see the list of possible columns for my pivot table. They are listed under a 'Range' level. (Which is what I would expect when you 'Add to Data Model' for a pivot). When I turn on record Macro, and manually add the field (category) to the filter section, the VBA code looks like this...
With ActiveSheet.PivotTables("PivotTable1").CubeFields("[Range].[Category]")
.Orientation = xlPageField
.Position = 1
End With
Below is my Delphi Code. It is the last commented lines which do NOT work.
procedure Build_Pivot;
var
// Connection Info
SheetName: String;
ConnRange, ConnName, ConnString, ConnCommand: String;
ConnPath, ConnFile : String;
ConnCount : Integer;
XLApp: Variant;
gConnection: Variant;
// Now define variables for the Pivot
myPivotCache: Variant;
myPivotTable: Variant;
StartCellRange: Variant;
fFilter1, fFilter2: Variant;
begin
XLApp := GetActiveOleObject('Excel.Application');
// Create the connection if we are adding to DataModel
// Now parse our gFileName
ConnPath := ExtractFileDir(gFileName); // Global Variable of the FileName
ConnFile := ExtractFileName(gFileName);
SheetName := 'RawData';
ConnRange := '$A:$H';
ConnCount := XLApp.ActiveWorkbook.Connections.Count;
ConnName := 'WorksheetConnection' + IntToStr(ConnCount) + '_' + SheetName + '!' + ConnRange;
ConnString := 'WORKSHEET;' + ConnPath + '[' + ConnFile + ']' + SheetName;
ConnCommand := SheetName + '!' + ConnRange;
gConnection := XLApp.ActiveWorkbook.Connections.Add2(ConnName, '', ConnString, ConnCommand, 7, True, False);
// Create the PivotCache
myPivotCache := XLApp.ActiveWorkbook.PivotCaches.Create(xlExternal, gConnection, 7);
// Create the PivotTable object;
StartCellRange := XLApp.ActiveWorkbook.ActiveSheet.Range['A5', 'A5'];
myPivotTable := myPivotCache.CreatePivotTable(StartCellRange, 'PivotTable1', True, 7);
// I can now see my Pivot Object in Excel
// Now add Filter Fields. I have tried multiple variations, but cannot get anything to work
// fFilter1 := XLApp.ActiveWorkbook.ActiveSheet.PivotTables('PivotTable1').CubeFields('[Range].[Category]');
// fFilter1 := myPivotCache.PivotTable.CubeFields('[Range].[Category]');
// fFilter1 := myPivotTable.CubeFields('[Range].[Category]');
fFilter1.Orientation := xlPageField;
Any help or ideas greatly appreciated.
Found the answer. The parameter needs to be passed as an array, and not within parenthesis. This code works...
fFilter1 := myPivotTable.CubeFields['[Range].[Category]'];
fFilter1.Orientation := xlPageField;
Delphi Tokyo - I have a Delphi app which is working against an Excel spreadsheet. I have a source sheet... called RawData. I need to copy specific rows (based on a filter) to another worksheet in the same workbook (called ActivitySheet). While I can get this to work... I am getting an Excel error message at the end of the process with the message 'The picture is too large and will be truncated.' This makes me believe that the command I am using is using the Clipboard, so I don't want to do that.
Here is what I am doing now...
var
ActivitySheet : _WorkSheet;
destRange : OleVariant;
begin
oExcel.Worksheets.Add(EmptyParam, EmptyParam, 1, xlWorksheet, LOCALE_USER_DEFAULT);
ActivitySheet:= oExcel.ActiveSheet as _Worksheet;
ActivitySheet.Name := 'Activity';
destRange := ActivitySheet.Range['A1', 'A1'];
// Set RawData Sheet as active
RawDataSheet.Activate(LOCALE_USER_DEFAULT);
Application.ProcessMessages;
// Now add the both filters that we want... (Not real filters, but easier to understand)
RawDataSheet.UsedRange[LOCALE_USER_DEFAULT].AutoFilter(1, '=*Blue*', xlOr, '=*Red*', True );
RawDataSheet.UsedRange[LOCALE_USER_DEFAULT].AutoFilter(2, '=Car', xlOr, '=Truck', True );
ShowMessage(IntToStr(RawDataSheet.UsedRange[LOCALE_USER_DEFAULT].Rows.Count));
RawDataSheet.UsedRange[LOCALE_USER_DEFAULT].Copy(destRange);
...
While this code works, and only copies the rows that are valid based on the current filter, it gives me the error at the 'Save Spreadsheet' stage later in my code. I thought about using a Vararray, but the ShowMessage line shows me the TOTAL rows in RawSheet, not the Filtered rows...so I would have to iterate through all the rows in the sheet (as opposed to just filtered rows) and then determine if the row is valid...
What is the best way to copy a set of filtered rows to a new sheet?
When I first saw this q, I had no idea how to do what you are asking. After a number
of false starts, I came across this answer
Excel Filtering and Copying in VBA
which shows that it can be done very simply, if you know the exact "magic spell" to do it.
Here is the code I wrote. I have used late binding to Excel throughout because it
avoids having to spray references to LOCALE_USER_DEFAULT everywhere.
type
TDefaultForm = class(TForm)
[...]
public
vExcel,
vWB,
vRange,
vSheet,
vActivitySheet,
vRawDataSheet : OleVariant;
end;
[...]
procedure TDefaultForm.CopyFilteredRange;
var
i : Integer;
vRange : OleVariant;
vDestRange : OleVariant;
begin
vRange := vRawDataSheet.Range['A1', 'A100'];
vDestRange := vActivitySheet.Range['A1', 'A1'];
vRange.SpecialCells(xlCellTypeVisible).Copy(vDestRange);
end;
procedure TDefaultForm.TestFilter;
var
vRange : OleVariant;
begin
vExcel := CreateOleObject('Excel.Application');
vExcel.Visible := True;
vWB := vExcel.WorkBooks.Add;
vRawDataSheet := vWB.ActiveSheet;
vActivitySheet := vWB.Sheets[2];
vRange := vRawDataSheet.Range['A1', 'A100'];
vRange.Item[1, 1] := 'Vehicle';
vRange.Item[2, 1] := 'Car';
vRange.Item[3, 1] := 'Truck';
vRange.Item[4, 1] := 'Truck';
vRange.Item[5, 1] := 'Car';
vRange.Item[6, 1] := 'Truck';
vRange.Item[7, 1] := 'Truck';
vRawDataSheet.UsedRange.Select;
vRawDataSheet.UsedRange.AutoFilter(Field := 1, Criteria1 := 'Car');
CopyFilteredRange;
end;
Btw, in case you are not familiar with it, the way of passing the arguments in the call
vRawDataSheet.UsedRange.AutoFilter(Field := 1, Criteria1 := 'Car');
is a special Delphi syntax for late-binding which avoids the rigmarole of passing
all the arguments required for early binding.
Also btw, I got the "Picture too large ..." message, but it seems easy to avoid: just include ClipBrd in the Uses list, and call ClipBoard.Clear after copying the filtered rows. Ordinarily, I don't like messing with the Clipboard because it's a system-wide resource, but as a fast fix here, clearing it may be acceptable.
I want to reaf an excel file with the help of Client_OLE2 in my Oracle forms.
I succeed to do that if the excel just has one sheet because this sheet is the activesheet, but if I have several sheets in my excel,I am not able to read the desire sheet.
----------------Get the file to open ---------------------------
v_fName := :upload.ti_file_name_upload;
IF ( v_fName IS NOT NULL ) THEN
clear_form ( no_validate );
--------------INITIATE EXCEL APPLICATION---------------------------
application := client_OLE2.create_obj('Excel.Application');
client_OLE2.set_property(application,'Visible', 'false');
----------------GET WORKBOOKS FROM EXCEL APPLICATION---------------
workbooks := client_OLE2.Get_Obj_Property(application, 'Workbooks');
----------------OPEN REQUIRED WORKBOOK-----------------------------
args := client_OLE2.CREATE_ARGLIST;
client_OLE2.add_arg(args,v_fName);
workbook := client_OLE2.GET_OBJ_PROPERTY(workbooks,'Open',args);
client_OLE2.destroy_arglist(args);
----------------OPEN REQUIRED WORKSHEET----------------------------
worksheets := client_OLE2.GET_OBJ_PROPERTY(workbook, 'Worksheets');
worksheet := client_OLE2.GET_OBJ_PROPERTY(application,'activesheet');
----------------Specify columns that must be read------------------
Thank you!
I replaced
worksheets := client_OLE2.GET_OBJ_PROPERTY(workbook, 'Worksheets');
worksheet := client_OLE2.GET_OBJ_PROPERTY(application,'activesheet');
with
args:= client_OLE2.create_arglist;
client_OLE2.add_arg(args, 'Sheet Name');
worksheet := client_OLE2.GET_OBJ_PROPERTY(workbook, 'Sheets', args);
client_OLE2.destroy_arglist(args);
and now it works fine!
I'm trying to make use of the TOpenDialog in order to pass the path to selected file to the AdoConection and load the content of the Excel file to the table. I'm currently attempting the code below but the last part of the code does not connect to the Excel returning an error:
[dcc32 Error] sample_map.pas(80): E2010 Incompatible types: 'string' and 'TOpenDialog'
procedure TForm1.Button1Click(Sender: TObject);
var
openDialog : TOpenDialog; // Open dialog variable
strConn : WideString; // Declare wide string for the connection
begin
// Create the open dialog object - assign to our open dialog variable
openDialog := TOpenDialog.Create(self);
// Set up the starting directory to be the current one
openDialog.InitialDir := GetCurrentDir;
// Only allow existing files to be selected
openDialog.Options := [ofFileMustExist];
// Allow only .dpr and .pas files to be selected
openDialog.Filter :=
'Excel 2003 and older|*.xls|Excel 2007 and older|*.xlsx';
// Select pascal files as the starting filter type
openDialog.FilterIndex := 2;
// Display the open file dialog
if openDialog.Execute
then ShowMessage('File : '+openDialog.FileName)
else ShowMessage('Open file was cancelled');
// Free up the dialog
openDialog.Free;
// Connect the Excel file
strConn:='Provider=Microsoft.Jet.OLEDB.4.0;' +
'Data Source=' + openDialog + ';' +
'Extended Properties=Excel 8.0;';
AdoConnection1.Connected:=False;
AdoConnection1.ConnectionString:=strConn;
end;
openDialog is an instance of a file dialog. It is not a string. You need to read the FileName property of the file dialog object like this:
openDialog.FileName
In fact you already use that in one of your ShowMessage calls.
Do make sure that you read this property before calling Free, a mistake present in the code in the question.
In fact you do need to get in to the habit of using try/finally to protect resources. Any time you create an instance of a class you need to make sure that it will be destroyed even in the face of an exception. In your code you need to write it like this:
openDialog := TOpenDialog.Create(self);
try
.... // use openDialog to let user choose file:
strConn := 'Provider=Microsoft.Jet.OLEDB.4.0;' +
'Data Source=' + openDialog.FileName + ';' +
'Extended Properties=Excel 8.0;';
finally
openDialog.Free; // executes no matter what, even if exception raised, etc.
end;
I also don't think you need to use WideString here. If you use a Unicode Delphi then you can use the native string type which is an alias for UnicodeString. If your Delphi is pre-Unicode, then you can also safely use string, an alias for AnsiString in that case. The literals you use are ASCII. The file dialog is an ANSI control and so openDialog.FileName is also ANSI. Nothing to be gained using WideString.
Finally, you are mixing up, all in one function, code to select a filename, and code to work on a database connection. It is better to separate concerns. Create a method that simply returns a filename, obtained by letting the user choose through a dialog. And add a method to work on the database connection, that is passed a filename as a parameter.
You need to get the OpenDialog.FileName prior to freeing the dialog:
OpenDialog := TOpenDialog.Create(nil);
try
// Set up the OpenDialog as before
// Display the open file dialog
if openDialog.Execute then
begin
strConn := 'Provider=Microsoft.Jet.OLEDB.4.0;' +
'Data Source=' + openDialog.FileName + ';' +
'Extended Properties=Excel 8.0;';
// Connect the Excel file
AdoConnection1.Connected:=False;
AdoConnection1.ConnectionString:=strConn;
else
ShowMessage('Open file was cancelled');
finally
// Free up the dialog
openDialog.Free;
end;
Of course, you're working much too hard. The Dialogs unit has a much easier way to do this using the PromptForFilename function, which eliminates the need to create and free the dialog entirely:
var
FileName: string;
begin
FileName := '';
if PromptForFileName(FileName, // Chosen filename holder
'Excel 2003 and older|*.xls|Excel 2007 and older|*.xlsx'; // Filter(s) (optional)
'.xlsx', // Default extension (opt)
'Choose file', // Dialog title (opt)
GetCurrentDir, // Initial dir (opt)
False) then // Is it a save dlg? (opt)
begin
strConn := 'Provider=Microsoft.Jet.OLEDB.4.0;' +
'Data Source=' + FileName + ';' +
'Extended Properties=Excel 8.0;';
// Connect the Excel file
AdoConnection1.Connected:=False;
AdoConnection1.ConnectionString:=strConn;
end
else
ShowMessage('Dialog cancelled.');
end;
As a side note, in case you don't know: You can select all Excel files (both .xls and .xlsx) with a single filter if you enter it as .xls*, as in Excel Files|*.xls*.
And, as David says, the best way would be to separate out the code that gets the filename and the code that does the connection as separate functions. Call the first to get the filename, and then pass that filename to the second to actually do the connecting if a filename is selected.
I have the following code to open my Excel Worksheet, using TADOConnection and TADOQuery:
conExcel.ConnectionString := 'Provider=Microsoft.JET.OLEDB.4.0;Data Source="D:\temp\file.xls";Extended Properties="Excel 8.0;HDR=No"';
conExcel.Connected := true;
When I run the following code:
queryExcel1.SQL.Text := 'SELECT * FROM [Hoja1$]';
queryExcel1.Open;
while not queryExcel1.eof do
begin
showmessage(queryExcel1.Fields.Fields[0].AsString);
queryExcel1.Next;
end;
I get each line of the file, so it is working ok, but I want to update a cell.
I am using this code:
queryExcel2.SQL.Text := 'UPDATE [Hoja1$] SET F1 = 555555';
queryExcel2.ExecSQL;
Which somehow updates every "A" cell on the worksheet to 555555, but what I really want is to just set the A1 cell to 555555. Any hints on how to include the WHERE section of the query. Thanks.
If you want to use an Adoquery you will have to set ParamCheck to false, since you will have to use : for the range you want to update.
An other option would be to use the connection directly.
In the example below the range B2:B2 is used with F1 which is the default name of Jet for the first column. (B2 is the cell we want to update)
Q.SQL.Text := 'UPDATE [Hoja1$B2:B2] SET [F1] = 555555';
Q.ExecSQL;
// or:
//AdoConnection1.Execute('UPDATE [Hoja1$B2:B2] SET [F1] = 555555');