function Xls_To_StringGrid(AGrid: TStringGrid; AXLSFile: string): Boolean;
const
xlCellTypeLastCell = $0000000B;
var
XLApp, Sheet: OLEVariant;
RangeMatrix: Variant;
x, y, k, r: Integer;
begin
Result := False;
// Create Excel-OLE Object
XLApp := CreateOleObject('Excel.Application');
try
// Hide Excel
XLApp.Visible := False;
// Open the Workbook
XLApp.Workbooks.Open(AXLSFile);
// Sheet := XLApp.Workbooks[1].WorkSheets[1];
Sheet := XLApp.Workbooks[ExtractFileName(AXLSFile)].WorkSheets[1];
// In order to know the dimension of the WorkSheet, i.e the number of rows
// and the number of columns, we activate the last non-empty cell of it
Sheet.Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Activate;
// Get the value of the last row
x := XLApp.ActiveCell.Row;
// Get the value of the last column
y := XLApp.ActiveCell.Column;
// Set Stringgrid's row &col dimensions.
AGrid.RowCount := x;
AGrid.ColCount := y;
// Assign the Variant associated with the WorkSheet to the Delphi Variant
RangeMatrix := XLApp.Range['A1', XLApp.Cells.Item[X, Y]].Value;
// Define the loop for filling in the TStringGrid
k := 1;
repeat
for r := 1 to y do
AGrid.Cells[(r - 1), (k - 1)] := RangeMatrix[K, R];
Inc(k, 1);
AGrid.RowCount := k + 1;
until k > x;
// Unassign the Delphi Variant Matrix
RangeMatrix := Unassigned;
finally
// Quit Excel
if not VarIsEmpty(XLApp) then
begin
// XLApp.DisplayAlerts := False;
XLApp.Quit;
XLAPP := Unassigned;
Sheet := Unassigned;
Result := True;
end;
end;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
if opendialog1.Execute then
Xls_To_StringGrid(StringGrid1, opendialog1.FileName);
end;
и кое что с кодом:
procedure TForm1.Button1Click(Sender: TObject);
var r,i,k,z,l,x,m,y,y2,nach,nach1:integer;
yy:extended;
begin
r:=strtoint(stringGrid1.cells[1,0]);
k:=2;
x:=strtoint(edit1.Text);
while r>=x do
begin
r:=strtoint(stringGrid1.cells[k,0]);
inc(k);
end;
for i:=k to 4 do
begin
nach:=strtoint(stringGrid1.cells[i,0]);
if (nach<x) and (nach>r) then
begin
y:=strtoint(stringGrid1.cells[i,1]);
r:=nach;
end;
end;
m:=strtoint(stringGrid1.cells[1,0]);
l:=2;
while m<=x do
begin
m:=strtoint(stringGrid1.cells[l,0]);
inc(l);
end;
for z:=l to 4 do
begin
nach1:=strtoint(stringGrid1.cells[z,0]);;
if (nach1>x) and (nach1<m) then
begin
y2:=strtoint(stringGrid1.cells[z,1]);
m:=nach1;
end;
end;
label1.Caption:=inttostr®;
label2.Caption:=inttostr(m);
label3.Caption:=inttostr(y);
label4.Caption:=inttostr(y2);
end;
Работа с той же таблицей из экселя. с клавиатуры вводится любое значение Х0 которое в ходит в промежуток (на что надо ещё сделать проверку, помогите пожалуйста и с этим
