delphi6 通过按钮 导出excel 第三方控件 50
展开全部
Project”——“Import Tpye Library”,在弹出的对话框中点击“Add”。找到你安装Excel的盘,,默认的安装路径:“C:\Program Files\Microsoft Office\OFFICE11”.安装的版本如果不同的话“OFFICE11”文件名会不一样。然后你可以找到“XL5CHS32.OLB”和“XL5EN32.OLB”文件。这两个文件你可以任选一个。XL5CHS32.OLB是中文版Excel,XL5EN32.OLB是英文版的Excel。选中后回到了Import Tpye Library对话框,在此对话框中选择“create unit”.这个新创建的单元会放在你当前文件存放的同一个目录下。记住不能删。
2、在你放按钮的窗体中添加一个ExcelApplication1控件,它放在Servers栏中。
3、继续上一步,在窗体的代码implementation部分添加如下代码:
uses dm,tools,Excel_TLB,comobj;
{$R *.dfm}
Function DetailToExcel(xl_file:string;grid:Tdbgrid;T1:string='';T2:string='';T3:string=''):integer;
var xlApp:Variant;
i,j:integer;
irow,icol:integer;
TitleLines:integer;
Titles:array [1..3] of string;
begin
if (not grid.DataSource.DataSet.Active) or (grid.DataSource.DataSet.RecordCount=0) then exit;
Try
xlapp:=createoleobject('Excel.application');
xlapp.workbooks.Add(-4167);
xlapp.visible:=true;
Except
showmessage('未安装Microsoft Excel 2000!请安装!');
exit
end;
//计算标题:
TitleLines:=0;
Titles[1]:=T1;
Titles[2]:=T2;
Titles[3]:=T3;
if T1<>'' then TitleLines:=1;
if T2<>'' then TitleLines:=2;
if T3<>'' then TitleLines:=3;
//填充列标题;
irow:=grid.DataSource.DataSet.RecordCount;
icol:=0;
for i:=0 to grid.Columns.Count-1 do
begin
if grid.Columns[i].Visible then
begin
inc(icol);
xlapp.cells[TitleLines+1,Icol]:=grid.Columns[i].Title.Caption;
end;
end;
//填充标题;
For i:=1 to TitleLines do
begin
xlapp.activesheet.cells[i,1]:=Titles[i];
xlapp.activesheet.range['A'+IntToStr(i)+':'+chr(64+icol)+IntToStr(i)].select;
xlapp.selection.HorizontalAlignment := xlCenter;
xlapp.selection.VerticalAlignment := xlCenter;
xlapp.selection.MergeCells := True;
if i=1 then
begin
xlapp.selection.RowHeight:=26;
xlapp.selection.Font.size:=18;
end
else
begin
xlapp.selection.RowHeight:=20;
xlapp.selection.Font.size:=12;
end;
end;
//设置表格线;
xlapp.activesheet.range['A'+IntTostr(TitleLines+1)+':'+chr(64+icol)+Inttostr(irow+TitleLines+1)].select;
xlapp.selection.Borders[xlEdgeBottom].linestyle:=xlContinuous;
xlapp.selection.Borders[xlEdgetop].linestyle:=xlContinuous;
xlapp.selection.Borders[xlEdgeright].linestyle:=xlContinuous;
xlapp.selection.Borders[xlEdgeLeft].linestyle:=xlContinuous;
xlapp.selection.Borders[xlInsideVertical].linestyle:=xlContinuous;
xlapp.selection.Borders[xlInsideHorizontal].linestyle:=xlContinuous;
//填充内容;
grid.DataSource.DataSet.First;
i:=TitleLines+2;
while not grid.DataSource.DataSet.Eof do
begin
icol:=0;
for j:=1 to grid.Columns.Count do
begin
if grid.Columns[j-1].Visible then
begin
inc(icol);
xlapp.cells[i,icol]:=grid.Columns[j-1].Field.DisplayText;
end;
end;
grid.DataSource.DataSet.Next;
inc(i);
end;
For i:=1 to icol do
begin
xlapp.activesheet.Columns[i].select;
xlapp.activesheet.Columns[i].EntireColumn.AutoFit;
end;
xlapp.activesheet.cells[1,1].select;
if xl_File<>'' then
xlapp.workbooks[1].SaveAs(xl_file);
result:=0;
end;
4、最后,按钮单击事件如下代码:
procedure TDayDutyForm.Button3Click(Sender: TObject);
begin
if DBGrid1.DataSource.DataSet.RecordCount=0 then
ShowMessage('没有内容!')
else
DetailToExcel(ExtractFilePath(application.ExeName)+'DayDuty.xls',DBgrid1,'日统计');
end;
/*DayDuty.xls是你需要的Excel的文件名,日统计是你需要的Excel的标题*/
以上内容是老师教的,很好用。希望你能用得成功哈。
2、在你放按钮的窗体中添加一个ExcelApplication1控件,它放在Servers栏中。
3、继续上一步,在窗体的代码implementation部分添加如下代码:
uses dm,tools,Excel_TLB,comobj;
{$R *.dfm}
Function DetailToExcel(xl_file:string;grid:Tdbgrid;T1:string='';T2:string='';T3:string=''):integer;
var xlApp:Variant;
i,j:integer;
irow,icol:integer;
TitleLines:integer;
Titles:array [1..3] of string;
begin
if (not grid.DataSource.DataSet.Active) or (grid.DataSource.DataSet.RecordCount=0) then exit;
Try
xlapp:=createoleobject('Excel.application');
xlapp.workbooks.Add(-4167);
xlapp.visible:=true;
Except
showmessage('未安装Microsoft Excel 2000!请安装!');
exit
end;
//计算标题:
TitleLines:=0;
Titles[1]:=T1;
Titles[2]:=T2;
Titles[3]:=T3;
if T1<>'' then TitleLines:=1;
if T2<>'' then TitleLines:=2;
if T3<>'' then TitleLines:=3;
//填充列标题;
irow:=grid.DataSource.DataSet.RecordCount;
icol:=0;
for i:=0 to grid.Columns.Count-1 do
begin
if grid.Columns[i].Visible then
begin
inc(icol);
xlapp.cells[TitleLines+1,Icol]:=grid.Columns[i].Title.Caption;
end;
end;
//填充标题;
For i:=1 to TitleLines do
begin
xlapp.activesheet.cells[i,1]:=Titles[i];
xlapp.activesheet.range['A'+IntToStr(i)+':'+chr(64+icol)+IntToStr(i)].select;
xlapp.selection.HorizontalAlignment := xlCenter;
xlapp.selection.VerticalAlignment := xlCenter;
xlapp.selection.MergeCells := True;
if i=1 then
begin
xlapp.selection.RowHeight:=26;
xlapp.selection.Font.size:=18;
end
else
begin
xlapp.selection.RowHeight:=20;
xlapp.selection.Font.size:=12;
end;
end;
//设置表格线;
xlapp.activesheet.range['A'+IntTostr(TitleLines+1)+':'+chr(64+icol)+Inttostr(irow+TitleLines+1)].select;
xlapp.selection.Borders[xlEdgeBottom].linestyle:=xlContinuous;
xlapp.selection.Borders[xlEdgetop].linestyle:=xlContinuous;
xlapp.selection.Borders[xlEdgeright].linestyle:=xlContinuous;
xlapp.selection.Borders[xlEdgeLeft].linestyle:=xlContinuous;
xlapp.selection.Borders[xlInsideVertical].linestyle:=xlContinuous;
xlapp.selection.Borders[xlInsideHorizontal].linestyle:=xlContinuous;
//填充内容;
grid.DataSource.DataSet.First;
i:=TitleLines+2;
while not grid.DataSource.DataSet.Eof do
begin
icol:=0;
for j:=1 to grid.Columns.Count do
begin
if grid.Columns[j-1].Visible then
begin
inc(icol);
xlapp.cells[i,icol]:=grid.Columns[j-1].Field.DisplayText;
end;
end;
grid.DataSource.DataSet.Next;
inc(i);
end;
For i:=1 to icol do
begin
xlapp.activesheet.Columns[i].select;
xlapp.activesheet.Columns[i].EntireColumn.AutoFit;
end;
xlapp.activesheet.cells[1,1].select;
if xl_File<>'' then
xlapp.workbooks[1].SaveAs(xl_file);
result:=0;
end;
4、最后,按钮单击事件如下代码:
procedure TDayDutyForm.Button3Click(Sender: TObject);
begin
if DBGrid1.DataSource.DataSet.RecordCount=0 then
ShowMessage('没有内容!')
else
DetailToExcel(ExtractFilePath(application.ExeName)+'DayDuty.xls',DBgrid1,'日统计');
end;
/*DayDuty.xls是你需要的Excel的文件名,日统计是你需要的Excel的标题*/
以上内容是老师教的,很好用。希望你能用得成功哈。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询