Hey Russell,
that works for me! thank you..
a just had to use the ".delimitedtext" instead of ".commatext" and set the delimiter to ";" because my csv file is semicolon delimited :)
thanks again..
/HMann
Main Topics
Browse All TopicsHey there,
i need to work with MS Excel within delphi!
i've found out how to open excel, open a new workbook/worksheet
i can also add data to the workbook and in other ways manipulate the cells etc.
something like:
var
OleApplication : Variant;
begin
OleApplication := CreateOleObject('Excel.App
OleWorkBook := OleApplication.Workbooks.o
..
..
..
end;
what i need is to be able to open a comma seperated file (.csv)!
not as a new workbook/worksheet, but open it into an existing (already open) workbook..
eg. i have a file with some headers/headlines in row 1. then i want to get the data from another file (the .csv file) into the same woorkbook!
if i open the .csv with the "open" procedure it will open as a new workbook/sheet..
i need to do the same as you do, when you in Excel choose "import data from textfile"..
i've seen somthing about a .opentext procedure which takes a lot of parameters, but i cant get it working..
anyone ?
/HMann
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: rllibbyPosted on 2004-01-13 at 09:08:22ID: 10105737
HMann,
; ; me); t])) > 0) then wCount]; , Pred(dwRows), 0, Pred(dwCols)], varVariant); t])) > 0) then wCount]; rtCell].Re size[dwRow s, dwCols];
: TObject);
bject('Exc el.Applica tion'); ue; n.Workbook s.open('c: \temp\book 1.xls'); on.ActiveS heet;
'A2', 'c:\temp\test.csv');
;
OpenText will open a new work sheet, so it isn't really what your after. You could use the QueryTables to import the data, but it is fairly simple if you just want to work with the range object. The example (function) below loads a CSV file, calculates the rows and columns, and then imports this data into your worksheet exactly where you want it (say for example, at cell A1).
Hope this helps,
Russell
-------
procedure MergeCSVFile(WorkSheet: Variant; StartCell: String; FileName: String);
var Range: Variant;
tsFile: TStringList;
tsLine: TStringList;
vaRange: Variant;
dwCount: Integer;
dwRows: Integer;
dwCols: Integer;
dwRow: Integer;
dwCol: Integer;
function Max(I1, I2: Integer): Integer;
begin
if (I1 > I2) then
result:=I1
else
result:=I2;
end;
begin
// Create string lists to the hold the file and one to parse the lines in the file
tsFile:=TStringList.Create
tsLine:=TStringList.Create
try
// Attempt to load the file
tsFile.LoadFromFile(FileNa
// Calculate the number of rows and max columns in the file
dwRows:=0;
dwCols:=0;
// Iterate the lines
for dwCount:=0 to Pred(tsFile.Count) do
begin
// Check for blank lines which will not get included
if (Length(Trim(tsFile[dwCoun
begin
// Increment the row count
Inc(dwRows);
// Clear the line parser
tsLine.Clear;
// Set the comma text
tsLine.CommaText:=tsFile[d
// Get the max column count
dwCols:=Max(dwCols, tsLine.Count);
end;
end;
// If no rows or cols then nothing to do
if (dwRows > 0) and (dwCols > 0) then
begin
// Create variant array to hold the range data
vaRange:=VarArrayCreate([0
try
// Fill the array in
dwRow:=0;
for dwCount:=0 to Pred(tsFile.Count) do
begin
// Break if row count has been hit
if (dwRow = dwRows) then break;
// Check for blank lines which will not get included
if (Length(Trim(tsFile[dwCoun
begin
// Clear the line parser
tsLine.Clear;
// Set the comma text
tsLine.CommaText:=tsFile[d
// Set the columns
for dwCol:=0 to Pred(dwCols) do
begin
if (dwCol >= tsLine.Count) then
vaRange[dwRow, dwCol]:=''
else
vaRange[dwRow, dwCol]:=tsLine[dwCol];
end;
// Increment the row counter
Inc(dwRow);
end;
end;
// Get the range and resize it
Range:=WorkSheet.Range[Sta
// Set the variant array data to the desired range
Range.Value:=vaRange;
finally
// Clear the variant data
VarClear(vaRange);
end;
end;
finally
// Free the string lists
tsLine.Free;
tsFile.Free;
end;
end;
procedure TForm1.Button1Click(Sender
var OleApplication: Variant;
OleWorkBook: Variant;
OleWorkSheet: Variant;
begin
OleApplication:=CreateOleO
OleApplication.Visible:=Tr
OleWorkBook:=OleApplicatio
OleWorkSheet:=OleApplicati
// Param1 - Worksheet to import data into
// Param2 - Upper/Left cell to start the import at
// Param3 - CSV file to import in
MergeCSVFile(OleWorkSheet,
OleApplication.Quit;
OleApplication:=Unassigned
end;