Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Excel To StringGrid


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 1 - Last Post: Apr 26, 2016 10:20 AM Last Post By: Peter Below
Melissa Torn

Posts: 143
Registered: 4/30/09
Excel To StringGrid  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 26, 2016 9:25 AM
I have below code it works (**transfers datas to stringgrid**)
but at the end i am getting below error msg.
"Could not convert variant of type(dispatch) into type (olestr)"
in my excel file, some rows are empty, for example
A1 has data but A2,A3 are empty A4 has data ......
how can prevent to transfer those empty rows into s.grid

Thank You

procedure TForm1.BitBtn1Click(Sender: TObject);
var
Excel: Variant;
i,j:integer;
begin
Excel:=CreateOleObject('Excel.Application');
if not OpenDialog1.Execute then Abort;
Excel.Application.WorkBooks.Add(OpenDialog1.FileName);

with StringGrid1 do
for i:=1 to RowCount-1 do
for j:=1 to ColCount-1 do
Cells[j, i]:=Excel.WorkSheets.Item['Sheet1'].Cells[i, j];
end;
Peter Below

Posts: 1,227
Registered: 12/16/99
Re: Excel To StringGrid  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 26, 2016 10:20 AM   in response to: Melissa Torn in response to: Melissa Torn
Melissa Torn wrote:

I have below code it works (**transfers datas to stringgrid**)
but at the end i am getting below error msg.
"Could not convert variant of type(dispatch) into type (olestr)"
in my excel file, some rows are empty, for example
A1 has data but A2,A3 are empty A4 has data ......
how can prevent to transfer those empty rows into s.grid

Thank You

procedure TForm1.BitBtn1Click(Sender: TObject);
var
Excel: Variant;
i,j:integer;
begin
Excel:=CreateOleObject('Excel.Application');
if not OpenDialog1.Execute then Abort;
Excel.Application.WorkBooks.Add(OpenDialog1.FileName);

with StringGrid1 do
for i:=1 to RowCount-1 do
for j:=1 to ColCount-1 do
Cells[j, i]:=Excel.WorkSheets.Item['Sheet1'].Cells[i, j];

Introduce a local variable of type OleVariant and first assign the
variant returned by Excel to this variable. You can then check what it
contains, or whether it contains anything at all, before you try to
assign the content to your stringgrid cell.

Delphi has a host of variant support routines in the System and
System.Variants units. VarIsNull and VarIsEmpty will allow you to check
for empty cells, for example. VarType gives you the content type, which
you could then check for types you can convert to a string for display.

--
Peter Below
TeamB
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02