解决方案 »
- 如何在传递表单参数?
- 对DateEdit赋值
- 关于SQL Server 2000的视图查询 长时间不出结果的问题
- 大家来讨论一下WEB SERVER+mts 四层结构的多帐套系统的设计方法
- 网络相关,第一个比较好解释给100分
- 请高手指教,怎么样屏蔽Ctrl+Alt+Del键,谢谢!
- 调查(来者有分):现在用delphi做分布式系统,真正用MIDAS的多吗?或者大家都在用什么做分布式系统?
- 如何运行?
- 统计一下,既会熟练使用delphi,也会使用pb的有那些大侠,请报个道
- 急,急,急 关于StringGrid的问题
- 如何遍历当前cxgrid的记录 ?
- RichEdit里面插入gif动画图片,怎么弄?
......implementationuses comobj;{$R *.dfm}Function DetailToExcel(xl_file:string;grid:Tdbgrid;T1:string='';T2:string='';T3:string=''):boolean;
var xlApp:Variant;
i,j:integer;
irow,icol:integer;
TitleLines:integer;
Titles:array [1..3] of string;
begin
result:=false;
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 !请安装!');
exit
end;
//计算标题:
Titles[1]:=T1;
Titles[2]:=T2;
Titles[3]:=T3;
if T3<>'' then TitleLines:=3
else if T2<>'' then TitleLines:=2
else if T1<>'' then TitleLines:=1
else TitleLines:=0;
//填充列标题;
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-1].Title.Caption;
xlapp.cells[TitleLines+1,Icol]:=inttostr(icol);
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 := -4108;
xlapp.selection.VerticalAlignment := -4108;
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[4].linestyle:=1;
xlapp.selection.Borders[3].linestyle:=1;
xlapp.selection.Borders[2].linestyle:=1;
xlapp.selection.Borders[1].linestyle:=1;
xlapp.selection.Borders[11].linestyle:=1;
xlapp.selection.Borders[12].linestyle:=1;
//填充内容;
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:=true;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
if DBGrid1.DataSource.DataSet.RecordCount=0 then
ShowMessage('没有内容!')
else if DetailToExcel(ExtractFilePath(application.ExeName)+'DayDuty.xls',DBgrid1,'日统计') then
ShowMessage('导出完成.')
end;
......
第一次运行时没有问题,再运行后,提示DayDuty.xls文件存在,是否覆盖?选否,进入另存为界面,另存后就提示:‘类 Workbook 的 Saveas 方法无效’。但是到存储路径查看表也保存上了。
deletefile(xl_file);