unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Grids, StdCtrls; type TForm1 = class(TForm) StringGrid1: TStringGrid; Edit1: TEdit; Label1: TLabel; Button1: TButton; Button2: TButton; Button3: TButton; Edit2: TEdit; Label2: TLabel; Button4: TButton; Button5: TButton; procedure Button1Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure StringGrid1Click(Sender: TObject); procedure StringGrid1KeyPress(Sender: TObject; var Key: Char); procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); procedure Button2Click(Sender: TObject); procedure Edit1KeyPress(Sender: TObject; var Key: Char); procedure Button4Click(Sender: TObject); procedure Button5Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; i,j,k,k1,min,max:integer; Key:Char; //m:array [1..1000] of integer; implementation uses ComObj; {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin form1.Button2.Enabled:=true; if StrToInt(Edit1.Text) <2 then begin form1.StringGrid1.ColCount:=3; form1.StringGrid1.RowCount:=3; for i:=1 to StrToInt(Edit1.Text) do begin StringGrid1.Cells[2,2]:=IntToStr(i); end; end else begin form1.StringGrid1.ColCount:=strtoint(Edit1.Text)+1; form1.StringGrid1.RowCount:=strtoint(Edit1.Text)+1; for i:=1 to StrToInt(Edit1.Text)+1 do begin StringGrid1.Cells[i,0]:=IntToStr(i); StringGrid1.Cells[0,i]:=IntToStr(i); end; end; for i:=1 to strtoint(Edit1.Text) do StringGrid1.Cells[i,i]:='0'; for j:=1 to strtoint(Edit1.Text) do for i:=1 to strtoint(Edit1.Text) do if StringGrid1.Cells[i,j]<>'0' then StringGrid1.Cells[i,j]:='0' else Break; StringGrid1.Refresh; end; procedure TForm1.Button3Click(Sender: TObject); begin Form1.Close; end; procedure TForm1.StringGrid1Click(Sender: TObject); begin //StringGrid1.Cells[2,1]:=StringGrid1.Cells[1,2]; end; procedure TForm1.StringGrid1KeyPress(Sender: TObject; var Key: Char); begin case Key of #8,'0'..'9' : ; // цифры и клавиша #13: // клавиша if StringGrid1.Col < StringGrid1.ColCount-1 then StringGrid1.Col := StringGrid1.Col + 1; else key := Chr(0); // остальные символы запрещены end; end; procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); Const clPaleGreen = TColor($CCFFCC); clPaleRed = TColor($CCCCFF); begin if (gdFocused in State) then begin StringGrid1.Canvas.Brush.Color := clBlack; StringGrid1.Canvas.Font.Color := clWhite; end else if ACol = 2 then StringGrid1.Canvas.Brush.color := clPaleGreen else StringGrid1.canvas.brush.Color := clPaleRed; end; procedure TForm1.Button2Click(Sender: TObject); var n,min:integer; label konec; begin Edit2.Text:=''; n:=strtoint(Edit1.Text); for i:=1 to n do for j:=1 to n do if StringGrid1.Cells[i,j]='' then begin MessageBox(handle, PChar('Введите город!'+'['+inttostr(i)+','+inttostr(j)+']'), PChar('Внимание'),(MB_OK+MB_ICONWARNING )); goto konec; end; j:=1;i:=1;k:=1;k1:=1; min:=StrToInt(StringGrid1.Cells[2,1]); while k1<>n do begin k:=k1; for i:=1 to n do if StringGrid1.Cells[i,k]<>'0' then if min>=StrToInt(StringGrid1.Cells[i,k]) then begin min:=StrToInt(StringGrid1.Cells[i,k]); k1:=StrToInt(StringGrid1.Cells[i,0]); end; if k1<>n then min:=StrToInt(StringGrid1.Cells[k1+1,k1]); Form1.Edit2.Text:=Form1.Edit2.Text+IntToStr(k)+'->'; end; konec: form1.Edit2.Text:= form1.Edit2.Text+StringGrid1.Cells[k1,0] end; procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); begin case Key of '0'..'9': ; // цифра #8 : ; // клавиша #13 : Edit1.SetFocus ; // клавиша , переводим фокус на второй Edit // остальные символы — запрещены else Key :=Chr(0); // символ не отображать end; end; //////////////////--------------------------------------------- function SaveAsExcelFile(stringGrid: TstringGrid; FileName: string): Boolean; const xlWBATWorksheet = -4167; var Row, Col: Integer; GridPrevFile: string; XLApp, Sheet: OLEVariant; begin Result := False; XLApp := CreateOleObject('Excel.Application'); try XLApp.Visible := False; XLApp.Workbooks.Add(xlWBatWorkSheet); Sheet := XLApp.Workbooks[1].WorkSheets[1]; Sheet.Name := 'My Sheet Name'; for col := 0 to stringGrid.ColCount - 1 do for row := 0 to stringGrid.RowCount - 1 do Sheet.Cells[row + 1, col + 1] := stringGrid.Cells[col, row]; try XLApp.Workbooks[1].SaveAs(FileName); Result := True; except // Error ? end; finally if not VarIsEmpty(XLApp) then begin XLApp.DisplayAlerts := False; XLApp.Quit; XLAPP := Unassigned; Sheet := Unassigned; end; end; end; ////////////--------------------------------------------------------------- procedure TForm1.Button4Click(Sender: TObject); begin if SaveAsExcelFile(stringGrid1, 'c:\MyExcelFile.xls') then ShowMessage('Готово!'); end; /////========================================================= 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; XLApp := CreateOleObject('Excel.Application'); try XLApp.Visible := False; XLApp.Workbooks.Open(AXLSFile); Sheet := XLApp.Workbooks[ExtractFileName(AXLSFile)].WorkSheets[1]; Sheet.Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Activate; x := XLApp.ActiveCell.Row; y := XLApp.ActiveCell.Column; AGrid.RowCount := x; AGrid.ColCount := y; RangeMatrix := XLApp.Range['A1', XLApp.Cells.Item[X, Y]].Value; 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; RangeMatrix := Unassigned; finally if not VarIsEmpty(XLApp) then begin XLApp.Quit; XLAPP := Unassigned; Sheet := Unassigned; Result := True; end; end; end; ////========================================================== procedure TForm1.Button5Click(Sender: TObject); begin form1.Button2.Enabled:=true; if Xls_To_StringGrid(StringGrid1, 'C:\MyExcelFile.xls') then ShowMessage('Готово'); end; end.