我是将一个表里的数据逐个导到了excel里,结果excel是打开的,我不用点excel上的保存怎样把它保存一个的地方?
我的转换数据代码如下:procedure TForm1.Button1Click(Sender: TObject);
var
aSheet:Variant;
i,tp:integer;
//tp:byte;
begin
//显示Ms-excel的执行过程
ExcelApplication1.Visible[0]:=True;
ExcelApplication1.WorkBooks.Add(xlWBATWorksheet,1);
aSheet:=ExcelApplication1.Worksheets.Item[1];
aSheet.cells[1,1].Value:='UserNum';
aSheet.cells[1,2].Value:='YYGLH';
......
aSheet.cells[1,12].Value:='DBZT';
i:=2;
with adoquery1 do
begin first; while not eof do begin
//用户编号
aSheet.cells[i,1].NumberFormatLocal := '@';
aSheet.cells[i,1].Value:=FieldbyName('User_id').Ass tring;
......
aSheet.cells[i,12].NumberFormatLocal := '@';
aSheet.cells[i,12].Value:='0'; i:=i+1; next;
end;end;
end;
我的转换数据代码如下:procedure TForm1.Button1Click(Sender: TObject);
var
aSheet:Variant;
i,tp:integer;
//tp:byte;
begin
//显示Ms-excel的执行过程
ExcelApplication1.Visible[0]:=True;
ExcelApplication1.WorkBooks.Add(xlWBATWorksheet,1);
aSheet:=ExcelApplication1.Worksheets.Item[1];
aSheet.cells[1,1].Value:='UserNum';
aSheet.cells[1,2].Value:='YYGLH';
......
aSheet.cells[1,12].Value:='DBZT';
i:=2;
with adoquery1 do
begin first; while not eof do begin
//用户编号
aSheet.cells[i,1].NumberFormatLocal := '@';
aSheet.cells[i,1].Value:=FieldbyName('User_id').Ass tring;
......
aSheet.cells[i,12].NumberFormatLocal := '@';
aSheet.cells[i,12].Value:='0'; i:=i+1; next;
end;end;
end;
过程的具体用法见下面的原函数吧,自己给参数/
ExcelWorkbook.SaveAs(Filename: OleVariant; FileFormat: OleVariant; Password: OleVariant;
WriteResPassword: OleVariant; ReadOnlyRecommended: OleVariant;
CreateBackup: OleVariant; AccessMode: XlSaveAsAccessMode;
ConflictResolution: OleVariant; AddToMru: OleVariant;
TextCodepage: OleVariant; TextVisualLayout: OleVariant;
lcid: Integer);
var
eclApp,WorkBook:Variant; {ÉùÃ÷ΪOLE Automation¶ÔÏó}
xlsFileName:string;
begin
xlsFileName:='c:\test.xls';
try
{´´½¨OLE¶ÔÏó£ºExcel ApplicationÓëWordBook}
eclApp:=CreateOleObject('Excel.Application');
WorkBook:=CreateOleObject('Excel.Sheet');
Except
//Application.MessageBox('ÄãµÄ»úÆ÷ûÓа²×°Microsoft Excel,ʹÓÃMicrosoft Excel',MB_OK+MB_ICONWarning,0);
Exit;
End;
Try
ShowMessage('ÏÂÃæÑÝʾ£ºÐ½¨Ò»¸öXLSÎļþ£¬²¢Ð´ÈëÊý¾Ý£¬²¢¹Ø±ÕËü¡£');
WorkBook:=eclApp.workbooks.Add;
EclApp.Cells(1,1):='×Ö·ûÐÍ';
EclApp.Cells(2,1):=''''+'00006220';
EclApp.Cells(1,2):='Money';
EclApp.Cells(2,2):=10.01;
EclApp.Cells(1,3):='ÈÕÆÚÐÍ';
EclApp.Cells(2,3):=Date;
WorkBook.SaveAS(xlsFileName);
WorkBook.close;
ShowMessage('ÏÂÃæÑÝʾ£º´ò¿ª¸Õ´´½¨µÄXLSÎļþ£¬²¢ÐÞ¸ÄÆäÖеÄÄÚÈÝ£¬È»ºó£¬ÓÉÓû§¾ö¶¨ÊÇ·ñ±£´æ¡£');
Workbook:=eclApp.WorkBooks.Open(xlsFileName);
EclApp.Cells(1,4):='ExcelÎļþÀàÐÍ';
If MessageDlg(xlsFileName+'ÒѾ­±»Ð޸ģ¬ÊÇ·ñ±£´æ£¿',
mtConfirmation,[mbYes,mbNo],0)=mrYes then
WorkBook.Save
Else
WorkBook.Saved:=True; {·ÅÆú±£´æ}
Workbook.Close;
EclApp.Quit; //Í˳öExcel Application
{ÊÍ·ÅVariant±äÁ¿}
eclApp:=Unassigned;exceptShowMessage('²»ÄÜÕýÈ·²Ù×÷ExcelÎļþ¡£¿ÉÄÜÊǸÃÎļþÒѱ»ÆäËû³ÌÐò´ò¿ª£¬»òϵͳ´íÎó¡£');WorkBook.close;EclApp.Quit;{ÊÍ·ÅVariant±äÁ¿}eclApp:=Unassigned;end;end;
ExcelWorkSheet1.Cells.NumberFormat :='@';