query1为查询结果,filename为EXCEL中的模板文件,
将其按照模板格式导出到EXECL中并保存:
if dm1.Query1.RecordCount=0 then
messageDlg('当前表格中没有记录可供导出!',mtInformation,[mbOK],0)
else
begin
try
ExcelApp:=CreateOleObject('Excel.Application');
MyWorkBook1:=CreateOleobject('Excel.Sheet');
MyWorkBook:=CreateOleobject('Excel.Sheet');
except
on Exception do raise exception.Create('系统需要在EXCEL中输出数据,请确认您的机器是否已经安装EXCEL')
end;
filename:=modeldir+'/OUTPUT.XLS';
if NOT DirectoryExists(modeldir) then
begin
beep;
ExcelApp.Quit;
ExcelApp:=Unassigned;
raise exception.Create('不能连接服务器上指定模板目录'+modeldir+',请检查此刻网络连接是否正常!')
end
else
if not FileExists(filename) then
begin
beep;
ExcelApp.Quit;
ExcelApp:=Unassigned;
raise exception.Create('无法打开模板文件:'+modeldir+'/OUTPUT.XLS,请与系统维护人员联系!')
end
else
MyworkBook1:= ExcelApp.workBooks.Open(modeldir+'/OUTPUT.XLS');
//取模板格式
Myworkbook.worksheets[1].Rows[3].Value := Myworkbook1.worksheets[1].Rows[3].Value;
Myworkbook.worksheets[1].Rows[4].Value := Myworkbook1.worksheets[1].Rows[4].Value;
Myworkbook.worksheets[1].Rows[5].Value := Myworkbook1.worksheets[1].Rows[5].Value;
MyworkBook1.Close;
MyWorkBook.WorkSheets[1].Cells[3,3].font.size :=20;
MyWorkBook.WorkSheets[1].Cells[4,2].font.size :=11;
Myworkbook.worksheets[1].Rows[3].font.color :=clBlue;
Myworkbook.worksheets[1].Rows[4].font.color :=clBlue;
Myworkbook.worksheets[1].Rows[5].font.color :=clBlue;
Myworkbook.worksheets[1].Columns[1].ColumnWidth :=1;
Myworkbook.worksheets[1].Columns[2].ColumnWidth :=12;
Myworkbook.worksheets[1].Columns[3].ColumnWidth :=25;
Myworkbook.worksheets[1].Columns[4].ColumnWidth :=20;
Myworkbook.worksheets[1].Columns[5].ColumnWidth :=17;
//write to Excel:
dm1.query1.First;
i:=0;
while not dm1.query1.eof do
begin
MyWorkBook.WorkSheets[1].Cells[6+i,2].Value := dm1.Query1ORDNO.Value;
MyWorkBook.WorkSheets[1].Cells[6+i,3].Value := dm1.Query1TRANSCORP.Value+':';
MyWorkBook.WorkSheets[1].Cells[6+i,4].Value := dm1.Query1TRADEWAY.Value;
MyWorkBook.WorkSheets[1].Cells[6+i,5].Value := dm1.Query1CNO.Value;
Inc(i);
dm1.query1.next;
end; //end of while
//save output file
filename:=localdir+'/NewOuput.XLS';
SaveDialog1.InitialDir:=localdir;
SaveDialog1.FileName:=filename;
if SaveDialog1.Execute then
begin
filename:=SaveDialog1.FileName;
try
if FileExists(filename) then
DeleteFile(FileName);
MyWorkBook.saveas(filename);
MyWorkBook.close;
beep;beep;
showmessage('输出记录到 '+filename+' 执行完毕!');
except
MyWorkBook.close;
end;
end;
ExcelApp.Quit;
ExcelApp:=Unassigned;
end;
end;