Reading Excel in Delphi 5 with Automation and Excel 2007

this piece of code was used in the delphi 5 program, which I was debugging:

s:string
vararray:variant;

vararray := VarArrayOf(['']);
vararray := ExcelWorkSheet1.UsedRange[0].Value;
s := VarToStr(vararray);

This works great when working with Excel XP and 2003. But since I switched to Excel 2007 (also Windows7), delphi gives a variant type conversion error on the last line ("Incorrect variant type conversion").

Can someone please help me with this? This code has not been affected over the past 5 years, and I cannot find a good source that documents these components.

thank!

+3
source share
2 answers

I managed to get the following to work:

Sheet := Workbook.Sheets.Item[1] as ExcelWorksheet;
value := Sheet.Range['A1','B1'].Value;
Edit1.Text := value[1,1];
Edit2.Text := value[1,2];

, Excel2000. , , , CreateComObject('Excel.Application').

Range[1,2], , , Excel.

, .

0

UsedRange . .value, .

, :

s:string
vararray:variant;

vararray := VarArrayOf(['']);
vararray := ExcelWorkSheet1.UsedRange[0].Value;
if vararray <> nul then s := VarToStr(vararray);
else s := ''; 
0

Source: https://habr.com/ru/post/1788847/


All Articles