procedure TArmExcel.createinstance;
begin
MsExcel:= CreateOleObject('Excel.Application');
WBook:=MsExcel.Application;
if (filename<>'') and (FileExists(FileName)) then
wbook.workbooks.Open(filename)
else
wbook.workbooks.add;
if activesheet<>'' then
wbook.Sheets[activesheet].Select;
WSheet:=WBook.ActiveSheet;
if showgrid then
DrawGrid;
PageSetup;
if FshowWindows then
ShowExcelWindow;
end;
destructor TArmExcel.Destroy;
begin
if fclose then
freeEXCElinstance;
Fheader.Free;
FFooter.Free;
Fgrid.Free;
FExportOptions.Free;
inherited Destroy;
end;
procedure TArmExcel.drawgrid;
VAR
ATXT:STRING;
begin
if Varisempty(wsheet) then
exit;
WITH GridBound DO
BEGIN
ATXT:=chr(64+left)+inttostr(top)+':'+chr(64+RIGHT)+inttostr(bottom);
WSheet.Range[ATXT].Borders[xlDiagonalDown].LineStyle:= xlNone;
WSheet.Range[ATXT].Borders[xlDiagonalUp].LineStyle:=xlNone;
WSheet.Range[ATXT].Borders[xlEdgeLeft].LineStyle:=xlContinuous;
WSheet.Range[ATXT].Borders[xlEdgeTop].LineStyle:=xlContinuous;
WSheet.Range[ATXT].Borders[xlEdgeBottom].LineStyle:=xlContinuous;
WSheet.Range[ATXT].Borders[xlEdgeRight].LineStyle:=xlContinuous;
WSheet.Range[ATXT].Borders[xlInsideVertical].LineStyle:=xlContinuous;
WSheet.Range[ATXT].Borders[xlInsideHorizontal].LineStyle:=xlContinuous;
END;
end;
procedure TArmExcel.FreeExcelInstance;
begin
if not varisempty(wbook) then
begin
Wbook.DisplayAlerts:=False;
Wbook.quit;
WBook:= UnAssigned;
end;
end;
function TArmExcel.GetCells(ARow,ACol: Integer): olevariant;
begin
if not varisempty(wsheet) then
result:=wsheet.cells[arow,acol].value;
end;
procedure TArmExcel.PageSetup;
begin
WSheet.PageSetup.PrintTitleRows:=PrintTitleRows;
if Header.left<>'' then
wsheet.pagesetup.LeftHeader:=Header.left;
if Header.right<>'' then
wsheet.pagesetup.RightHeader:=Header.RIGHT;
if Header.Center<>'' then
wsheet.pagesetup.CenterHeader:=Header.CENTER;
if FOOTER.left<>'' then
wsheet.pagesetup.LeftFooter:=Footer.left;
if FOOTER.right<>'' then
wsheet.pagesetup.RightFooter:=Footer.RIGHT;
if FOOTER.center<>'' then
wsheet.pagesetup.CenterFooter:=Footer.CENTER;
wsheet.pagesetup.PaperSize:=PaperSizeMetrics[PaperSize];
if (DirectPreview) then
wbook.ActiveWindow.SelectedSheets.PrintPreview;
end;
procedure TArmExcel.SetCells(ARow,ACol:Integer; const Value: olevariant);
begin
if not varisempty(wsheet) then
wsheet.cells[AROw,ACol].value:=value;
end;
procedure TArmExcel.ShowExcelWindow;
begin
WBook.Visible:=True;
end;
procedure TArmExcel.ExportData;
const
info='进度:%D/%D';
var
i,j,index:integer;
SavePlace: TBookmark;
begin
if ExportOptions.dataset=nil then
exit;
if not ExportOptions.dataset.active then
exit;
with ExportOptions.dataset,ExportOptions do
begin
SavePlace := GetBookmark;
disablecontrols;
first;
i:=1;
if ShowFieldName then
begin
for j:=0 to fieldcount-1 do
wsheet.cells[1+CellColOffset,j+1+CellRowOffset]:=fields[j].DisplayLabel;
i:=i+1;
end;
while not eof do
begin
for j:=0 to fieldcount-1 do
if fields[j].datatype in [Ftfloat,ftSmallint,ftInteger,ftWord,ftCurrency] then
wsheet.cells[i+CellColOffset,j+1+CellRowOffset]:=fields[j].asfloat
else
wsheet.cells[i+CellColOffset,j+1+CellRowOffset]:=fields[j].asstring;
next;
if Assigned(OnExportProgress) then
if ShowFieldName then
OnExportProgress(format(info,[i,RecordCount+1]),I,RecordCount)
else
OnExportProgress(format(info,[i,RecordCount]),I,RecordCount);
Application.ProcessMessages;
i:=i+1;
end;
if ShowSum and (Sumfields.Count<>0) then
begin
for j:=0 to fieldcount-1 do
if Tstringlist(sumfields).find(fields[j].FieldName,index) then
begin
if fields[j].datatype in [Ftfloat,ftSmallint,ftInteger,ftWord,ftCurrency] then
if ShowFieldName then
wsheet.Cells[i+CellColOffset,j+1+CellRowOffset]:='=sum('+chr(j+CellColOffset+65)
+inttostr(CellRowOffset+2)+':'+chr(j+CellColOffset+65)+inttostr(CellRowOffset+1+recordcount)+')'
else
wsheet.Cells[i+CellColOffset,j+1+CellRowOffset]:='=sum('+chr(j+CellColOffset+65)
+inttostr(CellRowOffset+1)+':'+chr(j+CellColOffset+65)+inttostr(CellRowOffset+recordcount)+')';
end;
end;
GotoBookmark(SavePlace);
FreeBookmark(SavePlace);
enablecontrols;
if DrawGrid then
begin
GridBound.top:=CellRowOffset+1;
if ShowFieldName then
GridBound.bottom:=RecordCount+CellRowOffset+1
else
GridBound.bottom:=RecordCount+CellRowOffset;
GridBound.left:=1+CellColOffset;
GridBound.Right:=CellColOffset+FieldCount;
self.DrawGrid;
end;
if SaveFile and (SaveFileName<>'')then
wbook.activeworkbook.saveas(SaveFileName);
if ShowExcel then
ShowExcelWindow;
end;
end;
procedure TArmExcel.CaculateSum;
begin
with ExportOptions,ExportOptions.Dataset do
begin
end;
end;
procedure TArmExcel.SetActiveSheet(const Value: string);
begin
FActiveSheet:= Value;
if (FActiveSheet<>'') and (Not Varisempty(wbook)) then
wbook.Sheets[activesheet].Select;
end;
procedure TArmExcel.SetShowGrid(const Value: boolean);
begin
FShowGrid := Value;
if FShowGrid then
DrawGrid
else
ClearGrid;
end;
procedure TArmExcel.ClearGrid;
VAR
ATXT:STRING;
begin
if Varisempty(wsheet) then
exit;
WITH GridBound DO
BEGIN
ATXT:=chr(64+left)+inttostr(top)+':'+chr(64+RIGHT)+inttostr(bottom);
WSheet.Range[ATXT].Borders[xlDiagonalDown].LineStyle:= xlNone;
WSheet.Range[ATXT].Borders[xlDiagonalUp].LineStyle:=xlNone;
WSheet.Range[ATXT].Borders[xlEdgeLeft].LineStyle:=xlNone;
WSheet.Range[ATXT].Borders[xlEdgeTop].LineStyle:=xlNone;
WSheet.Range[ATXT].Borders[xlEdgeBottom].LineStyle:=xlNone;
WSheet.Range[ATXT].Borders[xlEdgeRight].LineStyle:=xlNone;
WSheet.Range[ATXT].Borders[xlInsideVertical].LineStyle:=xlNone;
WSheet.Range[ATXT].Borders[xlInsideHorizontal].LineStyle:=xlNone;
END;
end;
procedure TArmExcel.SetPrintTitleRows(const Value: STRING);
begin
FPrintTitleRows := Value;
if not Varisempty(wsheet) then
WSheet.PageSetup.PrintTitleRows:=PrintTitleRows;
end;
procedure TArmExcel.SetPagerSize(const Value: TPaperSize);
begin
FPagerSize := Value;
if not Varisempty(wsheet) then
wsheet.pagesetup.PaperSize:=PaperSizeMetrics[PaperSize];
end;
{ TExportOptions }
constructor TExportOptions.create;
begin
inherited create;
FSumFields:=TStringlist.Create;
end;
destructor TExportOptions.Destroy;
begin
FSumFields.Free;
inherited Destroy;
end;
procedure TExportOptions.SetSumFields(const Value: TStrings);
begin
if Value = FSumFields then
Exit;
FSumFields.Assign(Value);
end;