A
A
Alex May2011-03-10 15:46:02
Delphi
Alex May, 2011-03-10 15:46:02

XLS Export to Delphi + Variant Array

Dear Delphi experts (I use Delphi 7)

Here is the task when exporting XLS file to stringgrid using COM tools.

The beginning and headings are standard, they are of no interest.

But, there is a type RangeMatrix: Variant, in which data is placed when calling
RangeMatrix := XLApp.Range['A1', XLApp.Cells.Item[X, Y]].Value;

Then the data from RangeMatrix is ​​looped into StringGrid.Cell[ROW,COL] Like

this:
StringGrid.Cells[ROW, COL] := RangeMatrix [ROW,COL];
This code works.

All data is placed normally, except for DateTime, which is placed as a Double, i.e. the time in the cell instead of 16:30 looks like 0.ХХХХХХХХ, i.е. 0.324234234

Question: How to make recognize the type of current data in RangeMatrix [ROW,COL]; in order to perform any actions on them during processing?

Unlike the code above, the code below, when the value of RangeMatrix [ROW,COL] is assigned to a variable, does not work
MyTest := RangeMatrix[ROW,COL];

This code gives an error: Variant or safe array index out of bounds My

head is completely broken. I ask for advice, why can't the variable MyTest be assigned RangeMatrix[ROW,COL]?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexander, 2011-03-11
@alexmay

StringGrid.Cells[ROW, COL] := RangeMatrix [ROW,COL];
This code works.

Show how you iterate over array indices. How do you define boundaries?

D
drVano, 2011-03-10
@drVano

MyTest := RangeMatrix[K, R];
This code throws an error: Variant or safe array index out of bounds

P
pokryshkin, 2011-03-10
@pokryshkin

so that there is no Variant or safe array index out of bounds, use .value as in the first case, i.e.
MyTest := RangeMatrix[ROW,COL].value;
with time, too, everything is correct - time is a fractional part of a number. But I don’t remember how to determine the number format, but it could be set through NumberFormat, maybe it will be determined through it.

X
xdenser, 2011-03-11
@xdenser

so in case
of AGrid.Cells[ROW, COL] := RangeMatrix[K, R];
there is a type casting to String;
try declaring
var
myTest: OleVariant;
or write
var
myTest: Variant;
myTest:=String(RangeMatrix[ROW,COL].Value);
but this does not solve the problem of determining the data type in the cell

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question