我的源码是这样的,希望高手多多赐教:
procedure DataToExcel(Grid:TDbGrid;DataSet:TDataset;Title:String;sExcelFile:String);
var
i,j,Row:integer;
WB: _WorkBook;
WBs: Workbooks;
FExcelWasFound:Boolean;
ExcelFile:string;
FileHandle: integer;
irange:range;
iWidth:integer;
//oFont:olevariant;
begin
try
Screen.Cursor :=crHourGlass ;
{for i:=0 to Grid.Columns.Count -1 do
begin
Dataset.Fields[i].DisplayWidth :=Grid.Columns[i].Width;
end; }
ExcelFile:=sExcelFile;
if not fileExists(ExcelFile) then
begin
FileHandle:=FileCreate(ExcelFile);
Fileclose(FileHandle);
end;
FExcelWasFound := True;
try
FApp := CreateOleObject('Excel.Application.9') as _Application; //调用Excel2000
except
FExcelWasFound := False;
end;
if not FExcelWasFound then //如果不存在,则调用Excel97
try
FApp := CreateOleObject('Excel.Application.8') as _Application;
FExcelWasFound := True;
except
FExcelWasFound := False;
ShowMessage('Excel调用失败!');
end;
if FExcelWasFound then
begin
InitVariables;
New(FSPms);
with FApp ,FSPms^ do
begin
App_SheetsInNewWorkbook := Get_SheetsInNewWorkbook(0);
App_DisplayFormulaBar := Get_DisplayFormulaBar(0);
App_ReferenceStyle := Get_ReferenceStyle(0);
App_DisplayStatusBar := Get_DisplayStatusBar(0);
Set_SheetsInNewWorkbook(0, 1);
WBs := Get_Workbooks; //打开Excel文件
WB := WBs.Open(excelFile, 3, false, 1,
'', '', True, $00000002, 1, False,
False, Null, False, 0);
MakeVBScript(WB); //初始化文件属性
end;
with FApp do
begin
Set_DisplayFormulaBar(0, False);
Set_ReferenceStyle(0, Integer(xlR1C1));
Set_DisplayStatusBar(0, False);
Set_Caption(Title);
end;
try
Row:=1;
irange:=Fapp.ActiveCell ;
irange.Font.Size :=9;
for j:=0 to Grid.FieldCount -1 do
begin
if Grid.Columns[j].Visible =true then
begin
if DataSet.Fields[j].displaywidth>254 then
iRange.ColumnWidth:=100
else
begin
//iWidth:=Grid.Columns[j].Width;
iRange.ColumnWidth :=Grid.Columns[j].Field.DisplayWidth ;
end;
irange.Font.Size :=9; //ljq 2001/03/09
irange.value:=Grid.Columns[j].Title.Caption ;
irange:=irange.Next;
end;
end;
except
ShowMessage('调用Excel出错!');
fApp._Release;
Screen.Cursor :=crDefault ;
exit;
end;
Row:=Row+2;
DataSet.DisableControls;
DataSet.First;
FApp.Get_ActiveWindow.DisplayZeros := True;
irange.NumberFormat:=10;
for i:=0 to DataSet.RecordCount -1 do
begin
irange:=Fapp.Range['A'+IntToStr(Row),'A'+intToStr(Row)];
for j:=0 to Grid.FieldCount -1 Do
begin
if Grid.Columns[j].Visible =True then
begin
if Grid<>nil then
begin
iRange.Font.Size :=Grid.Font.Size;
iRange.Font.Name :=Grid.Font.Name;
end
else
begin
irange.Font.Size :=FFontSize;
irange.Font.Name :=FFontName;
end; //edit by ljq 2001/03/09
iRange.Value :=Grid.Columns[j].Field.AsString ;
irange:=iRange.Next ;
end;
end;
DataSet.next;
Row:=Row+1;
end;
Screen.Cursor :=crDefault ;
DataSet.EnableControls;
irange:=FApp.Range['A1','K'+intToStr(Row-1)];
FApp.Set_Visible(0,True);
CreateToolBar(False); //屏蔽Excel的系统菜单,采用自定义菜单实现
end else
begin
ShowMessage('调用Excel2000或Excel97失败,请确认是否安装!'+#13#13+' 如果未安装,请先安装office');
Screen.Cursor :=crDefault ;
end;
except
ShowMessage('调用Excel出错!');
fApp._Release;
Screen.Cursor :=crDefault ;
exit;
end;
end;
procedure DataToExcel(Grid:TDbGrid;DataSet:TDataset;Title:String;sExcelFile:String);
var
i,j,Row:integer;
WB: _WorkBook;
WBs: Workbooks;
FExcelWasFound:Boolean;
ExcelFile:string;
FileHandle: integer;
irange:range;
iWidth:integer;
//oFont:olevariant;
begin
try
Screen.Cursor :=crHourGlass ;
{for i:=0 to Grid.Columns.Count -1 do
begin
Dataset.Fields[i].DisplayWidth :=Grid.Columns[i].Width;
end; }
ExcelFile:=sExcelFile;
if not fileExists(ExcelFile) then
begin
FileHandle:=FileCreate(ExcelFile);
Fileclose(FileHandle);
end;
FExcelWasFound := True;
try
FApp := CreateOleObject('Excel.Application.9') as _Application; //调用Excel2000
except
FExcelWasFound := False;
end;
if not FExcelWasFound then //如果不存在,则调用Excel97
try
FApp := CreateOleObject('Excel.Application.8') as _Application;
FExcelWasFound := True;
except
FExcelWasFound := False;
ShowMessage('Excel调用失败!');
end;
if FExcelWasFound then
begin
InitVariables;
New(FSPms);
with FApp ,FSPms^ do
begin
App_SheetsInNewWorkbook := Get_SheetsInNewWorkbook(0);
App_DisplayFormulaBar := Get_DisplayFormulaBar(0);
App_ReferenceStyle := Get_ReferenceStyle(0);
App_DisplayStatusBar := Get_DisplayStatusBar(0);
Set_SheetsInNewWorkbook(0, 1);
WBs := Get_Workbooks; //打开Excel文件
WB := WBs.Open(excelFile, 3, false, 1,
'', '', True, $00000002, 1, False,
False, Null, False, 0);
MakeVBScript(WB); //初始化文件属性
end;
with FApp do
begin
Set_DisplayFormulaBar(0, False);
Set_ReferenceStyle(0, Integer(xlR1C1));
Set_DisplayStatusBar(0, False);
Set_Caption(Title);
end;
try
Row:=1;
irange:=Fapp.ActiveCell ;
irange.Font.Size :=9;
for j:=0 to Grid.FieldCount -1 do
begin
if Grid.Columns[j].Visible =true then
begin
if DataSet.Fields[j].displaywidth>254 then
iRange.ColumnWidth:=100
else
begin
//iWidth:=Grid.Columns[j].Width;
iRange.ColumnWidth :=Grid.Columns[j].Field.DisplayWidth ;
end;
irange.Font.Size :=9; //ljq 2001/03/09
irange.value:=Grid.Columns[j].Title.Caption ;
irange:=irange.Next;
end;
end;
except
ShowMessage('调用Excel出错!');
fApp._Release;
Screen.Cursor :=crDefault ;
exit;
end;
Row:=Row+2;
DataSet.DisableControls;
DataSet.First;
FApp.Get_ActiveWindow.DisplayZeros := True;
irange.NumberFormat:=10;
for i:=0 to DataSet.RecordCount -1 do
begin
irange:=Fapp.Range['A'+IntToStr(Row),'A'+intToStr(Row)];
for j:=0 to Grid.FieldCount -1 Do
begin
if Grid.Columns[j].Visible =True then
begin
if Grid<>nil then
begin
iRange.Font.Size :=Grid.Font.Size;
iRange.Font.Name :=Grid.Font.Name;
end
else
begin
irange.Font.Size :=FFontSize;
irange.Font.Name :=FFontName;
end; //edit by ljq 2001/03/09
iRange.Value :=Grid.Columns[j].Field.AsString ;
irange:=iRange.Next ;
end;
end;
DataSet.next;
Row:=Row+1;
end;
Screen.Cursor :=crDefault ;
DataSet.EnableControls;
irange:=FApp.Range['A1','K'+intToStr(Row-1)];
FApp.Set_Visible(0,True);
CreateToolBar(False); //屏蔽Excel的系统菜单,采用自定义菜单实现
end else
begin
ShowMessage('调用Excel2000或Excel97失败,请确认是否安装!'+#13#13+' 如果未安装,请先安装office');
Screen.Cursor :=crDefault ;
end;
except
ShowMessage('调用Excel出错!');
fApp._Release;
Screen.Cursor :=crDefault ;
exit;
end;
end;
解决方案 »
- SeverSocket当 客户段非法关闭时无法获知客户端已经关闭
- union all select 语法错误
- 召集 10 位 delphi 骨灰级大牛,共同集资购买巨牛的 RemoteObjects 框架
- 三层中,客户端启动到登录界面要30秒,太慢了.请有经验的大虾看看
- 在线等待。请问delphi里把十六进制转为字符串用什么函数?
- delphi 为什么编译不了?
- indy批量传输文件
- 我的问题是:你们好啊!如何我把listbox中的每条记录 在我创建的sql数据库中查找到 并输出sql中的和这条记录相关的其他字段到另一个listb
- 怎么使在编辑stringgrid时能够多行显示?
- 如何判断 CAPSLOCK,MUMLOCK等键 的状态
- 计算结果排序?
- 大家都说程序员做到30岁,那30岁以后干嘛去。。
1000条数据4秒之内。其他的Tquery组件多半不行。
1000条数据4秒之内。其他的Tquery组件多半不行。不信你去试试,多数数据库都可以通过ado组访问,