I have an Excel spreadsheet, but now my problem is that I want to be able to read only specific columns, the columns in the spreadsheet are more than 20, I need to read only 3 columns.
procedure TForm1.sh1(SheetIndex: integer);
Var
Xlapp1,Xlrange, Sheet:Variant ;
MaxRow, MaxCol,X, Y:integer ;
str:string;
arrData:Variant;
begin
try
Str:=trim(form1.OpenDialog1.FileName);
XLApp1 := createoleobject('excel.application');
XLApp1.Workbooks.open(Str) ;
Sheet := XLApp1.WorkSheets[SheetIndex] ;
MaxRow := Sheet.Usedrange.EntireRow.count ;
MaxCol := sheet.Usedrange.EntireColumn.count;
arrData:= Sheet.UsedRange.Value;
stringgrid1.RowCount:=maxRow+1;
StringGrid1.ColCount:=maxCol+1;
for x := 1 to maxCol do
for y := 1 to maxRow do
stringgrid1.Cells[x,y]:=arrData[y, x];
XLApp1.Workbooks.close;
Except
on E : Exception do begin
XLApp1.Workbooks.close;
ShowMessage(E.Message);
end;
end;
end;