수집된 일부 흩어진 코드
function formatStr(const srcStr: string;Leng:integer): string;
var
i, iLen: integer;
begin
// Result
Result:= srcStr;
//
iLen:= Leng - Length(srcStr);
// ,
for i:= 1 to iLen do
begin
Result:= Result + ' ';
end;
end;
2. Panle에 색상 테두리 추가
코드:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls;
type
TPanel = class(ExtCtrls.TPanel)
public
procedure Paint; override;
end;
type
TForm1 = class(TForm)
Panel1: TPanel;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TPanel }
procedure TPanel.Paint;
begin
inherited;
Canvas.Pen.Color :=clBlue;
Canvas.Rectangle(ClientRect);
end;
end.
3.DBGRID 데이터를 EXCEL로 내보내기
function ExportToExcel(dbgrid:tdbgrid):boolean;
const
xlNormal=-4143;
var
i,j,k:integer;
str,filename:string;
excel:OleVariant;
SavePlace: TBookmark;
savedialog:tsavedialog;
ProgressBar1:TProgressBar;
begin
result:=false;
filename:='';
if dbgrid.DataSource.DataSet.RecordCount>65536 then
begin
if application.messagebox(' ,Excel 65536 , ?',' ',mb_yesno+mb_iconquestion)=idno then
exit;
end;
screen.Cursor:=crHourGlass;
try
excel:=CreateOleObject('Excel.Application');
excel.workbooks.add;
except
screen.cursor:=crDefault;
showmessage(' Excel!');
exit;
end;
savedialog:=tsavedialog.Create(nil);
savedialog.Filter:='Excel (*.xls)|*.xls';
if savedialog.Execute then
begin
if FileExists(savedialog.FileName) then
try
if application.messagebox(' , ?',' ',mb_yesno+mb_iconquestion)=idyes then
DeleteFile(PChar(savedialog.FileName))
else
begin
Excel.Quit;
savedialog.free;
screen.cursor:=crDefault;
Exit;
end;
except
Excel.Quit;
savedialog.free;
screen.cursor:=crDefault;
Exit;
end;
filename:=savedialog.FileName;
end;
savedialog.free;
application.ProcessMessages;
if filename='' then
begin
result:=false;
Excel.Quit;
screen.cursor:=crDefault;
exit;
end;
k:=0;
for i:=0 to dbgrid.Columns.count-1 do
begin
if dbgrid.Columns.Items[i].Visible then
begin
//Excel.Columns[k+1].ColumnWidth:=dbgrid.Columns.Items[i].Title.Column.Width;
excel.cells[1,k+1]:=dbgrid.Columns.Items[i].Title.Caption;
inc(k);
end;
end;
dbgrid.DataSource.DataSet.DisableControls;
saveplace:=dbgrid.DataSource.DataSet.GetBookmark;
dbgrid.DataSource.dataset.First;
i:=2;
if dbgrid.DataSource.DataSet.recordcount>65536 then
ProgressBar1:=ProgressBarform(65536)
else
ProgressBar1:=ProgressBarform(dbgrid.DataSource.DataSet.recordcount);
while not dbgrid.DataSource.dataset.Eof do
begin
k:=0;
for j:=0 to dbgrid.Columns.count-1 do
begin
if dbgrid.Columns.Items[j].Visible then
begin
excel.cells[i,k+1].NumberFormat:='@';
if not dbgrid.DataSource.dataset.fieldbyname(dbgrid.Columns.Items[j].FieldName).isnull then
begin
str := dbgrid.DataSource.dataset.fieldbyname(dbgrid.Columns.Items[j].FieldName).value;
Excel.Cells[i, k + 1] := Str;
end;
inc(k);
end
else
continue;
end;
if i=65536 then
break;
inc(i);
ProgressBar1.StepBy(1);
dbgrid.DataSource.dataset.next;
end;
progressbar1.Owner.Free;
application.ProcessMessages;
dbgrid.DataSource.dataset.GotoBookmark(SavePlace);
dbgrid.DataSource.dataset.EnableControls;
try
if copy(FileName,length(FileName)-3,4)<>'.xls' then
FileName:=FileName+'.xls';
Excel.ActiveWorkbook.SaveAs(FileName,xlNormal,'', '',False,False);
except
Excel.Quit;
screen.cursor:=crDefault;
exit;
end;
//Excel.Visible := true;
Excel.Quit;
screen.cursor:=crDefault;
Result:= true;
end;
4. Adoquery 데이터를 Excel로 내보내기
function queryExportToExcel(queryexport:tadoquery):boolean;
const
xlNormal=-4143;
var
i,j,k:integer;
str,filename:string;
excel:OleVariant;
savedialog:tsavedialog;
ProgressBar1:TProgressBar;
begin
result:=false;
filename:='';
if queryexport.RecordCount>65536 then
begin
if application.messagebox(' ,Excel 65536 , ?',' ',mb_yesno+mb_iconquestion)=idno then
exit;
end;
screen.Cursor:=crHourGlass;
try
excel:=CreateOleObject('Excel.Application');
excel.workbooks.add;
except
screen.cursor:=crDefault;
showmessage(' Excel!');
exit;
end;
savedialog:=tsavedialog.Create(nil);
savedialog.Filter:='Excel (*.xls)|*.xls';
if savedialog.Execute then
begin
if FileExists(savedialog.FileName) then
try
if application.messagebox(' , ?',' ',mb_yesno+mb_iconquestion)=idyes then
DeleteFile(PChar(savedialog.FileName))
else
begin
Excel.Quit;
savedialog.free;
screen.cursor:=crDefault;
Exit;
end;
except
Excel.Quit;
savedialog.free;
screen.cursor:=crDefault;
Exit;
end;
filename:=savedialog.FileName;
end;
savedialog.free;
application.ProcessMessages;
if filename='' then
begin
result:=false;
Excel.Quit;
screen.cursor:=crDefault;
exit;
end;
k:=0;
for i:=0 to queryexport.FieldCount-1 do
begin
excel.cells[1,k+1]:=queryexport.Fields[i].FieldName;
inc(k);
end;
queryexport.First;
i:=2;
if queryexport.recordcount>65536 then
ProgressBar1:=ProgressBarform(65536)
else
ProgressBar1:=ProgressBarform(queryexport.recordcount);
while not queryexport.Eof do
begin
k:=0;
for j:=0 to queryexport.FieldCount-1 do
begin
excel.cells[i,k+1].NumberFormat:='@';
if not queryexport.fieldbyname(queryexport.Fields[j].FieldName).isnull then
begin
str:=queryexport.fieldbyname(queryexport.Fields[j].FieldName).AsString;
Excel.Cells[i, k + 1] := Str;
end;
inc(k);
end;
if i=65536 then
break;
inc(i);
ProgressBar1.StepBy(1);
queryexport.next;
end;
progressbar1.Owner.Free;
application.ProcessMessages;
try
if copy(FileName,length(FileName)-3,4)<>'.xls' then
FileName:=FileName+'.xls';
Excel.ActiveWorkbook.SaveAs(FileName,xlNormal,'', '',False,False);
except
Excel.Quit;
screen.cursor:=crDefault;
exit;
end;
//Excel.Visible := true;
Excel.Quit;
screen.cursor:=crDefault;
Result := true;
end;
end.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[Delphi] TStringBuilder그리고 꼭 사용해야만 할까? 그림처럼 Heap 영역에 "Hello" 공간을 생성하고 포인팅을 한다. "Hello World" 공간을 새로 생성한 후 포인팅을 하게 된다. 결국 "Hello" 라는 String 객체가 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.