// 判断是否有Execl,且将其打开 function OpenExcel(ExcelApplication: TExcelApplication):boolean; begin Result := True; try ExcelApplication.Connect; ExcelApplication.Visible[1] := True; if not (ExcelApplication.Workbooks.Count > 0) then begin ExcelApplication.Workbooks.add(emptyparam,1); end; CellRow := 1; CellCol := 1; except Application.MessageBox('您没有安装Excel,请安装后再使用','提示',mb_ok); Result := False; end; end;
供参考://输出到 Excel 文件 procedure OutputExcel(ExcelApplication: TExcelApplication;DataSet: TQuery;Flag: integer); var Sheet1 : OleVariant; // Excel文件中的一页 j : integer; //循环控制表量 FieldNum: integer; //字段个数 begin Sheet1 := ExcelApplication.Workbooks[1].Sheets[1]; Sheet1.visible := true; if Flag = 0 then begin FieldNum := DataSet.FieldCount -1; end else begin FieldNum := DataSet.FieldCount -3; end; for j := 0 to FieldNum do // 输出标题 begin Sheet1.Cells[CellRow,CellCol + j] := '''' + DataSet.Fields[j].DisplayLabel; Sheet1.Cells[cellrow,CellCol + j].Font.Name := '宋体'; Sheet1.Cells[cellrow,CellCol + j].Font.Size := '12'; Sheet1.Cells[cellrow,CellCol + j].Font.Bold := True; end; DataSet.First; while not DataSet.Eof do // 输出内容 begin Inc(CellRow); for j := 0 to FieldNum do begin Sheet1.Cells[Cellrow,CellCol+j].Font.Name := '宋体'; Sheet1.Cells[cellrow,CellCol+j].Font.Size := '9'; Sheet1.Cells[CellRow,CellCol+j] := '''' + DataSet.Fields[j].AsString; end; DataSet.Next; end; end; //关闭EXCEL procedure CloseExcel(ExcelApplication : TExcelApplication); begin ExcelApplication.Quit; ExcelApplication.Disconnect; end;
function OpenExcel(ExcelApplication: TExcelApplication):boolean;
begin
Result := True;
try
ExcelApplication.Connect;
ExcelApplication.Visible[1] := True;
if not (ExcelApplication.Workbooks.Count > 0) then
begin
ExcelApplication.Workbooks.add(emptyparam,1);
end;
CellRow := 1;
CellCol := 1;
except
Application.MessageBox('您没有安装Excel,请安装后再使用','提示',mb_ok);
Result := False;
end;
end;
procedure OutputExcel(ExcelApplication: TExcelApplication;DataSet: TQuery;Flag: integer);
var
Sheet1 : OleVariant; // Excel文件中的一页
j : integer; //循环控制表量
FieldNum: integer; //字段个数
begin
Sheet1 := ExcelApplication.Workbooks[1].Sheets[1];
Sheet1.visible := true;
if Flag = 0 then
begin
FieldNum := DataSet.FieldCount -1;
end
else
begin
FieldNum := DataSet.FieldCount -3;
end;
for j := 0 to FieldNum do // 输出标题
begin
Sheet1.Cells[CellRow,CellCol + j] := '''' + DataSet.Fields[j].DisplayLabel;
Sheet1.Cells[cellrow,CellCol + j].Font.Name := '宋体';
Sheet1.Cells[cellrow,CellCol + j].Font.Size := '12';
Sheet1.Cells[cellrow,CellCol + j].Font.Bold := True;
end;
DataSet.First;
while not DataSet.Eof do // 输出内容
begin
Inc(CellRow);
for j := 0 to FieldNum do
begin
Sheet1.Cells[Cellrow,CellCol+j].Font.Name := '宋体';
Sheet1.Cells[cellrow,CellCol+j].Font.Size := '9';
Sheet1.Cells[CellRow,CellCol+j] := '''' + DataSet.Fields[j].AsString;
end;
DataSet.Next;
end;
end;
//关闭EXCEL
procedure CloseExcel(ExcelApplication : TExcelApplication);
begin
ExcelApplication.Quit;
ExcelApplication.Disconnect;
end;