unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, Menus;
type
TForm1 = class(TForm)
Button4: TButton;
StringGrid1: TStringGrid;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
Button6: TButton;
Button8: TButton;
MainMenu1: TMainMenu;
N2: TMenuItem;
N3: TMenuItem;
N4: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure N3Click(Sender: TObject);
procedure N4Click(Sender: TObject);
procedure N2Click(Sender: TObject);
procedure Button8Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure StringGrid1Click(Sender: TObject);
procedure StringGrid1KeyPress(Sender: TObject; var Key: Char);
procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;var CanSelect: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
ComObj;
const
cRow1 = 1;
cCol1 = 1;
//Очистка таблицы типа TStringGrid и сброс её размеров.
procedure SgClear(aSg : TStringGrid);
var
Row : Integer;
begin
for Row := aSg.FixedRows to aSg.RowCount - 1 do
aSg.Rows[Row].Clear;
aSg.RowCount := aSg.FixedRows + 1;
end;
//Чтение данных с листа рабочей книги Excel в таблицу
procedure ExcelToSg(aExSh : Variant; const aRow, aCol : Integer; aSg : TStringGrid);
const
SelfName = 'ExcelToSg()';
//Идентификатор типа ячейки в диапазоне
xlCellTypeLastCell = 11;
var
exRng, exCell1, exCell2 : Variant;
vArr : Variant;
i, j, Row : Integer;
begin
SgClear(aSg);
//Левая верхняя ячейка диапазона с данными
exCell1 := aExSh.Cells[aRow + aSg.FixedRows, aCol];
//Правая нижняя ячейка используемого диапазона на листе
exCell2 := aExSh.UsedRange.SpecialCells(xlCellTypeLastCell);
if (exCell2.Row < exCell1.Row) or (exCell2.Column < exCell1.Column) then
Exit;
//Определяем диапазон с данными в соответствие с количеством столбцов
exRng := aExSh.Range[exCell1,
exCell1.Offset[exCell2.Row - exCell1.Row, aSg.ColCount - 1]];
//Получаем данные диапазона в виде вариантного массива.
vArr := exRng.Value;
//Задаём количество строк данных таблицы,
//равным количеству строк в диапазоне
aSg.RowCount := aSg.FixedRows + VarArrayHighBound(vArr, 1);
//Копирование данных массива в ячейки нефиксированных строк таблицы
Row := aSg.FixedRows;
for i := 1 to VarArrayHighBound(vArr, 1) do
for j := 1 to VarArrayHighBound(vArr, 2) do
aSg.Cells[j - 1, Row + i - 1] := vArr[i, j];
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
StringGrid1.Options:=StringGrid1.Options+[goEditing];
//Шапка таблицы
StringGrid1.Cells[0,0]:=('ФИО');
StringGrid1.Cells[1,0]:=('Стоимость месяца');
StringGrid1.Cells[2,0]:=('Оплачено');
StringGrid1.Cells[3,0]:=('Осталось оплатить');
StringGrid1.Cells[4,0]:=('Сдано');
StringGrid1.Cells[5,0]:=('Занятие 1');
StringGrid1.Cells[6,0]:=('Занятие 2');
StringGrid1.Cells[7,0]:=('Занятие 3');
StringGrid1.Cells[8,0]:=('Занятие 4');
StringGrid1.Cells[9,0]:=('Занятие 5');
StringGrid1.Cells[10,0]:=('Занятие 6');
StringGrid1.Cells[11,0]:=('Занятие 7');
StringGrid1.Cells[12,0]:=('Занятие 8');
StringGrid1.Cells[13,0]:=('Занятие 9');
StringGrid1.Cells[14,0]:=('Занятие 10');
Form1.StringGrid1.Cells[1,1]:='600';
Form1.StringGrid1.Cells[2,1]:='0';
Form1.StringGrid1.Cells[4,1]:='0' ;
//осталось оплатить
end;
//Очистка таблицы.
procedure TForm1.Button4Click(Sender: TObject);
begin
SgClear(StringGrid1);
end;
//добавление ученика
procedure TForm1.Button6Click(Sender: TObject);
var
s:string;
begin
if StringGrid1.RowCount>1 then begin
StringGrid1.RowCount:=StringGrid1.RowCount+1;
Form1.StringGrid1.Cells[1,StringGrid1.RowCount-1]:='600';
Form1.StringGrid1.Cells[4,StringGrid1.RowCount-1]:='0';
Form1.StringGrid1.Cells[2,StringGrid1.RowCount-1]:='0';
end;
end;
//Запись данных на лис Excel
procedure TForm1.N3Click(Sender: TObject);
var
Excel, WorkBook, Sheet:variant;
i, j,ListInt: integer;
FName,List: string;
begin
if SaveDialog1.Execute then
FName := SaveDialog1.FileName
else
Exit;
Excel:=CreateOleObject('Excel.Application');
Excel.DisplayAlerts:=False;
Excel.Visible:= False;
Workbook:=Excel.
Workbooks.open(FName);
Workbook.SaveAs(FName);
List:=InputBox('Номер месяца', 'Введите номер месяца', '1');
ListInt:=StrToInt(Copy(List, 1, 2));
Sheet:= Workbook.Worksheets[ListInt];
for i:= 0 to StringGrid1.RowCount - 1 do
begin
for j:= 0 to StringGrid1.ColCount - 1 do
Sheet.Cells[i+1, j+1]:= StringGrid1.Cells[j, i];
end;
Workbook.Save;
Workbook.Close;
Excel.Quit;
Excel:= UnAssigned;
MessageBox(Handle,'Экспорт данных завершен!','Внимание!',0);
end;
//Чтение данных с листа MS Excel.
procedure TForm1.N4Click(Sender: TObject);
var
Excel, WorkBook, exSh : Variant;
Sg : TStringGrid;
Od : TOpenDialog;
List:string;
ListInt:integer;
begin
Sg := StringGrid1;
Od := OpenDialog1;
if Od.InitialDir = '' then
Od.InitialDir := ExtractFilePath( ParamStr(0) );
if not Od.Execute then Exit;
if not FileExists(Od.FileName) then begin
MessageBox(0, 'Файл с заданным именем не найден. Действие отменено.'
,'Файл не найден', MB_OK + MB_ICONEXCLAMATION + MB_APPLMODAL);
Exit;
end;
try
Excel := CreateOleObject('Excel.Application');
except
MessageBox(0, 'Не удалось запустить MS Excel. Действие отменено.',
'Ошибка', MB_OK + MB_ICONERROR + MB_APPLMODAL);
Exit;
end;
Excel.Visible := false;
WorkBook := Excel.WorkBooks.Open(FileName:=Od.FileName);
List:=InputBox('Номер месяца', 'Введите номер месяца', '1');
ListInt:=StrToInt(Copy(List, 1, 2));
exSh := WorkBook.Worksheets[ListInt];
ExcelToSg(exSh, cRow1, cCol1, Sg);
WorkBook.Close;
Excel.Quit;
Excel:= UnAssigned;
end;
//создание книги excel
procedure TForm1.N2Click(Sender: TObject);
var
Excel, WorkBook, Sheet:variant;
FName: string;
begin
if SaveDialog1.Execute then
FName := SaveDialog1.FileName
else
Exit;
Excel:=CreateOleObject('Excel.Application');
Excel.DisplayAlerts:=False;
Excel.Visible:= False;
Workbook:=Excel.
Workbooks.add;
Workbook.SaveAs(FName);
Workbook.Save;
Workbook.Close;
Excel.Quit;
Excel:= UnAssigned;
MessageBox(Handle,'Файл создан!','Внимание!',0);
end;
procedure TForm1.Button8Click(Sender: TObject);
var
S:String;
i:integer;
begin
For i:=1 to StringGrid1.RowCount-1 do begin
S:= Form1.StringGrid1.Cells[2,i] ;
Form1.StringGrid1.Cells[4,i]:=S;
Form1.StringGrid1.Cells[2,i]:='0';
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i,si,di,summi:integer;
summ,s,d:string;
begin
For i:=1 to StringGrid1.RowCount-1 do begin
s:= Form1.StringGrid1.Cells[2,i] ;
d:= Form1.StringGrid1.Cells[4,i] ;
si:=StrToInt(s);
di:=StrToInt(d);
summi:=si+di;
summ:=IntToStr(summi);
Form1.StringGrid1.Cells[3,i]:=(summ);
end;
end;
procedure TForm1.StringGrid1Click(Sender: TObject);
var
s,d,summ,st:string;
si,di,summi,i,sti:integer;
begin
try
for i:=1 to StringGrid1.RowCount-1 do begin
s:= Form1.StringGrid1.Cells[2,i] ;
d:= Form1.StringGrid1.Cells[4,i] ;
st:=Form1.StringGrid1.Cells[1,i] ;
si:=StrToInt(s);
di:=StrToInt(d);
sti:=StrToInt(st);
summi:=sti-(si+di);
summ:=IntToStr(summi);
Form1.StringGrid1.Cells[3,i]:=(summ);
end;
except
end;
end;
procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;var CanSelect: Boolean);
begin
if Acol=1 then
begin
CanSelect:=False;
StringGrid1.Options:=StringGrid1.Options-[goEditing];
end else
begin
CanSelect:=True;
StringGrid1.Options:=StringGrid1.Options+[goEditing];
end;
end;
procedure TForm1.StringGrid1KeyPress(Sender: TObject; var Key: Char);
const Digit=['0', '1'..'9'];
begin
if (not (Key in Digit)) then Key:=#0;
end;
end.