function ExportToExcel(dbgrid:tdbgrid):boolean; const xlNormal=-4143; var i,j,k:integer; str,filename:string; excel:OleVariant; SavePlace: TBook; savedialog:tsavedialog; ProgressBar1:TProgressBar; begin result:=false; filename:=''; if not dbgrid.DataSource.DataSet.Active then begin application.MessageBox('数据集尚未打开!','错误',0); 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; if filename='' then begin result:=true; 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;
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; inc(i); ProgressBar1.StepBy(1); dbgrid.DataSource.dataset.next; end; progressbar1.Parent.Free;
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; screen.cursor:=crDefault; Result := true; end;
function ProgressBarform(max:integer):tProgressBar; var ProgressBar1:tProgressBar; form:tform; begin application.CreateForm(tform,form); form.Position:=poScreenCenter; form.BorderStyle:=bsnone; form.Height:=30; form.Width:=260; ProgressBar1:=tProgressBar.Create(form); ProgressBar1.Smooth:=true; ProgressBar1.Max:=max; ProgressBar1.Parent:=form; ProgressBar1.Height:=20; ProgressBar1.Width:=250; ProgressBar1.Left:=5; ProgressBar1.Top:=5; ProgressBar1.Step:=1; form.Show; result:=ProgressBar1; end;
1、利用SetWindowLong是可以右对齐,可是右对齐后,鼠标就不能点击别的的方了。2、这只是将DBGrid中的内容导出到Excel中,我是想导出时格式有自己定义的,而不是默认的。 如: DBGrid中 ID Name 0001 员工1 0002 员工2 0003 员工3 Excel中 ID Name 而不是 ID Name 0001 员工1 1 员工1 0002 员工2 2 员工2 0003 员工3 3 员工3
procedure TForm1.Edit1Change(Sender: TObject);
var s:integer;
begin
try
s:=strtoint(edit1.text);
SetWindowLong(edit1.Handle, GWL_STYLE, ES_left);
except
SetWindowLong(edit1.Handle, GWL_STYLE, ES_RIGHT);
end;
end;2uses
Windows,Graphics,DB,Grids, DBGrids,StdCtrls,forms,Sysutils,classes,
Controls,comobj,comctrls,Dialogs,Variants;
function ExportToExcel(dbgrid:tdbgrid):boolean;
const
xlNormal=-4143;
var
i,j,k:integer;
str,filename:string;
excel:OleVariant;
SavePlace: TBook;
savedialog:tsavedialog;
ProgressBar1:TProgressBar;
begin
result:=false;
filename:='';
if not dbgrid.DataSource.DataSet.Active then
begin
application.MessageBox('数据集尚未打开!','错误',0);
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;
if filename='' then
begin
result:=true;
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.GetBook;
dbgrid.DataSource.dataset.First;
i:=2;
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;
inc(i);
ProgressBar1.StepBy(1);
dbgrid.DataSource.dataset.next;
end;
progressbar1.Parent.Free;
dbgrid.DataSource.dataset.GotoBook(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;
screen.cursor:=crDefault;
Result := true;
end;
function ProgressBarform(max:integer):tProgressBar;
var
ProgressBar1:tProgressBar;
form:tform;
begin
application.CreateForm(tform,form);
form.Position:=poScreenCenter;
form.BorderStyle:=bsnone;
form.Height:=30;
form.Width:=260;
ProgressBar1:=tProgressBar.Create(form);
ProgressBar1.Smooth:=true;
ProgressBar1.Max:=max;
ProgressBar1.Parent:=form;
ProgressBar1.Height:=20;
ProgressBar1.Width:=250;
ProgressBar1.Left:=5;
ProgressBar1.Top:=5;
ProgressBar1.Step:=1;
form.Show;
result:=ProgressBar1;
end;
如:
DBGrid中 ID Name
0001 员工1
0002 员工2
0003 员工3 Excel中 ID Name 而不是 ID Name
0001 员工1 1 员工1
0002 员工2 2 员工2
0003 员工3 3 员工3