发布网友 发布时间:2023-04-02 07:30
共1个回答
热心网友 时间:2023-11-27 12:54
首先在Uses处加上ComObjprocereTForm1.Button1Click(Sender:TObject);varh,k:integer;Excelid:OleVariant;s:string;begintryExcelid:=CreateOLEObject('Excel.Application');exceptApplication.MessageBox('Excel没有安装!','提示信息',MB_OK+MB_ICONASTERISK+MB_DEFBUTTON1+MB_APPLMODAL);Exit;end;tryADOQuery1.Close;ADOQuery1.SQL.Clear;ADOQuery1.SQL.Add('select*fromjj_department');ADOQuery1.Open;k:=ADOQuery1.RecordCount;Excelid.Visible:=True;Excelid.WorkBooks.Add;Excelid.worksheets[1].range['A1:c1'].Merge(True);Excelid.WorkSheets[1].Cells[1,1].Value:='部门编码表';Excelid.worksheets[1].Range['a1:a1'].HorizontalAlignment:=$FFFFEFF4;Excelid.worksheets[1].Range['a1:a1'].VerticalAlignment:=$FFFFEFF4;Excelid.WorkSheets[1].Cells[2,1].Value:='组别编号';Excelid.WorkSheets[1].Cells[2,2].Value:='公司编号';Excelid.WorkSheets[1].Cells[2,3].Value:='组别名称';Excelid.worksheets[1].Range['A1:c1'].Font.Name:='宋体';Excelid.worksheets[1].Range['A1:c1'].Font.Size:=9;Excelid.worksheets[1].range['A1:c2'].font.bold:=true;Excelid.worksheets[1].Range['A2:c2'].Font.Size:=9;Excelid.worksheets[1].Range['A2:c2'].HorizontalAlignment:=$FFFFEFF4;Excelid.worksheets[1].Range['A2:c2'].VerticalAlignment:=$FFFFEFF4;h:=3;ADOQuery1.First;whilenotADOQuery1.EofdobeginExcelid.WorkSheets[1].Cells[h,1].Value:=Adoquery1.FieldByName('Fdept_id').AsString;Excelid.WorkSheets[1].Cells[h,2].Value:=Adoquery1.FieldByName('Ffdept_id').AsString;Excelid.WorkSheets[1].Cells[h,3].Value:=Adoquery1.FieldByName('Fdept_name').AsString;Inc(h);Adoquery1.Next;end;s:='A2:f'+IntToStr(k+2);Excelid.worksheets[1].Range[s].Font.Name:='宋体';Excelid.worksheets[1].Range[s].Font.size:=9;Excelid.worksheets[1].Range[s].Borders.LineStyle:=1;Excelid.Quit;exceptApplication.MessageBox('导入数据出错!请检查文件的格式是否正确!','提示信息',MB_OK+MB_ICONASTERISK+MB_DEFBUTTON1+MB_APPLMODAL);end;MessageBox(GetActiveWindow(),'EXCEL数据导出成功!','提示信息',MB_OK+MB_ICONWARNING);end;