Код:
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, CheckLst, ComObj, Printers, ExtCtrls;
//------- Печать документа -----------------
Код:
xlDialogPrint=8;
xlDialogPrinterSetup=9;
xlDialogPrintPreview=222;
Код:
Function CreateExcel:boolean;
begin
CreateExcel:=true;
try
E:=CreateOleObject('Excel.Application');
except
CreateExcel:=false;
end;
End;
Function VisibleExcel(visible:boolean):boolean;
begin
VisibleExcel:=true;
try
E.visible:=visible;
except
VisibleExcel:=false;
end;
End;
Function AddWorkBook:boolean;
begin
AddWorkBook:=true;
try
E.Workbooks.Add;
except
AddWorkBook:=false;
end;
End;
Function OpenWorkBook(file_:string):boolean;
begin
OpenWorkBook:=true;
try
E.Workbooks.Open(file_);
except
OpenWorkBook:=false;
end;
End;
Function AddSheet(newsheet:string):boolean;
begin
AddSheet:=true;
try
E.ActiveWorkbook.Sheets.Add;
E.ActiveWorkbook.ActiveSheet.Name:=newsheet;
except
AddSheet:=false;
end;
End;
Function SaveWorkBookAs(file_:string):boolean;
begin
SaveWorkBookAs:=true;
try
E.DisplayAlerts:=False;
E.ActiveWorkbook.SaveAs(file_);
E.DisplayAlerts:=True;
except
SaveWorkBookAs:=false;
end;
End;
Function CloseWorkBook:boolean;
begin
CloseWorkBook:=true;
try
E.ActiveWorkbook.Close;
except
CloseWorkBook:=false;
end;
End;
Function CloseExcel:boolean;
begin
CloseExcel:=true;
try
E.Quit;
except
CloseExcel:=false;
end;
End;
Function SetRange(sheet:variant;range:string;value_:variant):boolean;
begin
SetRange:=true;
try
E.ActiveWorkbook.Sheets.Item[sheet].Range[range]:=value_;
except
SetRange:=false;
end;
End;
SetMergeCells(sheet:variant;range:string;MergeCells:boolean):boolean;
begin
SetMergeCells:=true;
try
E.ActiveWorkbook.Sheets.Item[sheet].Range[range].MergeCells:=MergeCells;
except
SetMergeCells:=false;
end;
end;
SetShrinkToFit(sheet:variant;range:string;ShrinkToFit:boolean):boolean;
begin
SetShrinkToFit:=true;
try
E.ActiveWorkbook.Sheets.Item[sheet].Range[range].ShrinkToFit:=ShrinkToFit;
except
SetShrinkToFit:=false;
end;
End;
Function PrintPreview:boolean;
begin
PrintPreview:=true;
try
E.ActiveWindow.SelectedSheets.PrintPreview;
except
PrintPreview:=false;
end;
End;
Function PrintPreviewEx:boolean;
begin
PrintPreviewEx:=true;
try
E.Dialogs.Item[xlDialogPrintPreview].Show;
except
PrintPreviewEx:=false;
end;
End;
Function ShowPrintDialog:boolean;
begin
ShowPrintDialog:=true;
try
E.Dialogs.Item[xlDialogPrint].Show;
except
ShowPrintDialog:=false;
end;
End;
Function SetBorderRange(sheet:variant;range:string;Edge,LineStyle,Weight,ColorIndex,Color:integer):boolean;
begin
SetBorderRange:=true;
try
E.ActiveWorkbook.Sheets.Item[sheet].Range[range].Borders.item[Edge].LineStyle:=LineStyle;
E.ActiveWorkbook.Sheets.Item[sheet].Range[range].Borders.item[Edge].Weight:=Weight; if ColorIndex>0 then E.ActiveWorkbook.Sheets.Item[sheet].Range[range].Borders.item[Edge].ColorIndex:=ColorIndex
else
E.ActiveWorkbook.Sheets.Item[sheet].Range[range].Borders.item[Edge].Color:=color;
except
SetBorderRange:=false;
end;
End;
Код:
Procedure Form1.Button1Click(Sender : TObject);
begin
if not createExcel then exit;
OpenWorkBook(getcurrentdir+'\ADP_VED\template.xls');
end;
var bl:boolean;
i,kol,j,jj,count:integer;
f:textfile;
s,ExportExcel,PrintExcel,SaveExcel:string;
begin
if strtoint(label11.Caption)>30 then begin
application.MessageBox('Маршрут №1. Максимум 30 заведений на один маршрут.','Ошибка',0);
exit;
end;
if strtoint(label13.Caption)>30 then begin
application.MessageBox('Маршрут №2. Максимум 30 заведений на один маршрут.','Ошибка',0);
exit;
end;
if strtoint(label13.Caption)>30 then begin
application.MessageBox('Маршрут №3. Максимум 30 заведений на один маршрут.','Ошибка',0);
exit;
end;
bar_vdm_form.show;
bar_vdm_form.ProgressBar1.Min:=0;
bar_vdm_form.ProgressBar1.Max:=100;
listbox1.clear;
listbox2.clear;
listbox1.Items.add('');
listbox2.Items.add('');
listbox3.clear;
listbox4.clear;
listbox3.Items.add('');
listbox4.Items.add('');
for i:=1 to checklistbox1.Items.count-1 do begin
if checklistbox1.Checked[i]=true then begin
listbox1.Items.Add(checklistbox1.Items.Strings[i]);
Assignfile(f,getcurrentdir+'\ADP_VED\Днепропетровск.adp');
Reset(f);
while not eof(f) do begin
readln(f,s);
if s=checklistbox1.Items.Strings[i] then begin
readln(f,s);
listbox2.Items.Add(s);
end;
end;
CloseFile(f) end;
end;
for i:=1 to checklistbox2.Items.count-1 do begin
if checklistbox2.Checked[i]=true then begin
listbox3.Items.Add(checklistbox2.Items.Strings[i]);
Assignfile(f,getcurrentdir+'\ADP_VED\Днепропетровск.adp');
Reset(f);
while not eof(f) do begin
readln(f,s);
if s=checklistbox2.Items.Strings[i] then begin
readln(f,s);
listbox4.Items.Add(s);
end;
end;
CloseFile(f) end;
end;
for i:=1 to checklistbox3.Items.count-1 do begin
if checklistbox3.Checked[i]=true then begin
listbox5.Items.Add(checklistbox3.Items.Strings[i]);
Assignfile(f,getcurrentdir+'\ADP_VED\Днепропетровск.adp');
Reset(f);
while not eof(f) do begin
readln(f,s);
if s=checklistbox3.Items.Strings[i] then begin
readln(f,s);
listbox6.Items.Add(s);
end;
end;
CloseFile(f) end;
end;
if not createExcel then exit;
OpenWorkBook(getcurrentdir+'\ADP_VED\template.xls');
if strtoint(label13.Caption)>0 then begin
for i:=1 to listbox1.Items.Count-1 do begin
if i mod 2=1 then begin
j:=j+8;
SetRange(1,'G'+inttostr(j),listbox1.Items.Strings[i]);
SetRange(1,'G'+inttostr(j+1),listbox2.Items.Strings[i]);
end;
if i mod 2=0 then begin
jj:=jj+8;
SetRange(1,'R'+inttostr(jj),listbox1.Items.Strings[i]);
SetRange(1,'R'+inttostr(jj+1),listbox2.Items.Strings[i]);
end;
end;
end;
if strtoint(label13.Caption)>0 then begin
j:=0;
jj:=0;
for i:=1 to listbox3.Items.Count-1 do begin
if i mod 2=1 then begin
j:=j+8;
SetRange(1,'AF'+inttostr(j),listbox3.Items.Strings[i]);
SetRange(1,'AF'+inttostr(j+1),listbox4.Items.Strings[i]);
end;
if i mod 2=0 then begin
jj:=jj+8;
SetRange(1,'AQ'+inttostr(jj),listbox3.Items.Strings[i]);
SetRange(1,'AQ'+inttostr(jj+1),listbox4.Items.Strings[i]);
end;
end;
end;
if strtoint(label13.Caption)>0 then begin
j:=0;
jj:=0;
for i:=1 to listbox5.Items.Count-1 do begin
if i mod 2=1 then begin
j:=j+8;
SetRange(1,'BD'+inttostr(j),listbox5.Items.Strings[i]);
SetRange(1,'BD'+inttostr(j+1),listbox6.Items.Strings[i]);
end;
if i mod 2=0 then begin
jj:=jj+8;
SetRange(1,'BO'+inttostr(jj),listbox5.Items.Strings[i]);
SetRange(1,'BO'+inttostr(jj+1),listbox6.Items.Strings[i]);
end;
end;
end;
bar_vdm_form.Caption:='Подождите, идет подготовка к печати...';
FirstVed;
bar_vdm_form.ProgressBar1.StepBy(33);
SecondVed;
bar_vdm_form.ProgressBar1.StepBy(33);
ThirdVed;
bar_vdm_form.ProgressBar1.StepBy(34);
bar_vdm_form.Close;
VisibleExcel(true);
PrintPreviewEx;
SaveWorkBookAs(Getcurrentdir+'\123.xls');
DeleteFile('C:\123.xls');
CloseWorkBook;
CloseExcel;
|