我想将query中的数据导出为Excel表,数据为Access!(100分)

  • 主题发起人 主题发起人 bigjader
  • 开始时间 开始时间
B

bigjader

Unregistered / Unconfirmed
GUEST, unregistred user!
BDE中有控件是Batchmove导出是Acess格式!
最好能提供完整的源码!
我是一名新手所以请大家指教!!!
 
//EHLIB的导出方法,引用DBGridEhImpExp
procedure TDBMDIChildForm.DataSetExportExecute(Sender: TObject);
//导出数据
var
ExpClass:TDBGridEhExportClass;
Ext:String;
begin
inherited;
if not DBG.DataSource.DataSet.Active then exit;
if dlgSave.Execute then
begin
case dlgSave.FilterIndex of
1: begin ExpClass := TDBGridEhExportAsText; Ext := 'txt'; end;
2: begin ExpClass := TDBGridEhExportAsCSV; Ext := 'csv'; end;
3: begin ExpClass := TDBGridEhExportAsHTML; Ext := 'htm'; end;
4: begin ExpClass := TDBGridEhExportAsRTF; Ext := 'rtf'; end;
5: begin ExpClass := TDBGridEhExportAsXLS; Ext := 'xls'; end;
else
ExpClass := nil; Ext := '';
end;
if ExpClass <> nil then
begin
if UpperCase(Copy(dlgSave.FileName,Length(dlgSave.FileName)-2,3)) <>
UpperCase(Ext) then
dlgSave.FileName := dlgSave.FileName + '.' + Ext;
SaveDBGridEhToExportFile(ExpClass,DBG,
dlgSave.FileName,not DBG.CheckCopyAction);
end;
end;
end;
 
EHLIB是一个什么样的控件?请多多指教?
 
procedure TForm5.BitBtn1Click(Sender: TObject);
var

excelapp,myworkbook:variant;
i:integer;
begin
screen.Cursor:=crhourglass;
g1.Visible :=true;
g1.MaxValue :=table1.RecordCount ;
try
ExcelApp:=CreateOleObject('Excel.Application');
label1.Caption:='正在导入数据...';
MyWorkBook:=CreateOleobject('Excel.Sheet');
except
application.Messagebox('无法打开Xls文件,请确认已 经安装EXCEL.',
'',mb_OK+mb_IconStop);
Exit;
end;
MyworkBook:=ExcelApp.workBooks.Add;
MyWorkBook.WorkSheets[1].cells[1,1].value:='车号' ;
MyWorkBook.WorkSheets[1].cells[1,2].value:='驾驶员';
MyWorkBook.WorkSheets[1].cells[1,3].value:='客户单位';
MyWorkBook.WorkSheets[1].cells[1,4].value:='起讫点';
MyWorkBook.WorkSheets[1].cells[1,5].value:='日期';
MyWorkBook.WorkSheets[1].cells[1,6].value:='贷物';
MyWorkBook.WorkSheets[1].cells[1,7].value:='数量';
MyWorkBook.WorkSheets[1].cells[1,8].value:='单位';
MyWorkBook.WorkSheets[1].cells[1,9].value:='单价';
MyWorkBook.WorkSheets[1].cells[1,10].value:='行程';
MyWorkBook.WorkSheets[1].cells[1,11].value:='运输费';
MyWorkBook.WorkSheets[1].cells[1,12].value:='过路费';
MyWorkBook.WorkSheets[1].cells[1,13].value:='实绩';
MyWorkBook.WorkSheets[1].cells[1,14].value:='经办人';
i:=2;
with table1 do begin
DisableControls ;
first;
while not eof do begin
g1.Progress :=RecNo;
MyWorkBook.WorkSheets[1].cells[i,1].value:=fieldbyname('num').asstring;
MyWorkBook.WorkSheets[1].cells[i,2].value:=fieldbyname('dname').asstring;
MyWorkBook.WorkSheets[1].cells[i,3].value:=fieldbyname('cust').asstring;
MyWorkBook.WorkSheets[1].cells[i,4].value:=fieldbyname('daddr').asstring;
MyWorkBook.WorkSheets[1].cells[i,5].value:=fieldbyname('date1').asstring;
MyWorkBook.WorkSheets[1].cells[i,6].value:=fieldbyname('hwu').asstring;
MyWorkBook.WorkSheets[1].cells[i,7].value:=fieldbyname('sliang').asstring;
MyWorkBook.WorkSheets[1].cells[i,8].value:=fieldbyname('danwei').asstring;
MyWorkBook.WorkSheets[1].cells[i,9].value:=fieldbyname('djia').asstring;
MyWorkBook.WorkSheets[1].cells[i,10].value:=fieldbyname('xingch').asstring;
MyWorkBook.WorkSheets[1].cells[i,11].value:=fieldbyname('ysfei').asstring;
MyWorkBook.WorkSheets[1].cells[i,12].value:=fieldbyname('glfei').asstring;
MyWorkBook.WorkSheets[1].cells[i,13].value:=fieldbyname('score').asstring;
MyWorkBook.WorkSheets[1].cells[i,14].value:=fieldbyname('getman').asstring;
i:=i+1;
next;
end;


//在此处插入读数据库及写Excel文档的代码


end;
screen.Cursor:=crdefault;
table1.EnableControls ;
g1.Visible:=false;
label1.Caption :='';
excelapp.visible:=true;
end;
g1 为 progressbar
 
给分吧。
以上是我通过的代码,还有进度条显示
 
Access->Excel 为什么用BDE而不用ADO? 用ADO导出简直是无比Easy:
procedure ExportXLS(tnsource, tndest, fn: string);
//tnsource :Access中的表名
//tndest: Excel中的表名
//fn: 导出的文件名 如 test.xls
var
s: string;
begin
if ADOQuery1.Active then
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
s := 'select * into ' + tndest + ' in "' + fn + '" "excel 8.0;" from ' + tnsource;
ADOQuery1.SQL.Add(s);
ADOQuery1.ExecSQL;
end;
 
我也建議用ADO要方便簡單一點
 
呵呵!这样的帖子每个月都有几个,其实你只要查询一下以前的贴子就会得到一大堆答案。
 
后退
顶部