看到100分口水都流出来了。给你一个函数把你连接DBF的数据集传到我的函数就可以了
uses ComObj;
procedure ExportToExcel(Dataset:TDataset);
var
Range,WorkBook,Sheet,Excel:variant;
CurCol,col,row:integer;
begin
Excel:=CreateOleObject('Excel.Application');
FrmWait:=TFrmWait.Create(application);
try
FrmWait.Show;
// Excel.visible:=true;
Application.ProcessMessages;
Excel.DisplayAlerts:=false;
Excel.WorkBooks.Add;
WorkBook:=Excel.WorkBooks[1];
Sheet := WorkBook.WorkSheets[1];
CurCol:=0;
Row:=4; //从第四行,第二列开始输出
for col:=1 to DataSet.FieldCount do //输出标题
if DataSet.Fields[Col-1].Visible then
begin
Sheet.Cells[Row,CurCol+2]:=DataSet.Fields[Col-1].DisplayLabel;
inc(CurCol);
end;
inc(Row); //数据从第五行开始输出
DataSet.DisableControls;
DataSet.First;
while not Dataset.Eof do
begin
CurCol:=0;
for Col:=1 to DataSet.FieldCount do
if DataSet.Fields[Col-1].Visible then
begin
Sheet.Cells[Row,CurCol+2]:=DataSet.Fields[Col-1].DisplayText;
inc(CurCol);
end;
DataSet.Next;
inc(Row);
end;
Range:=Sheet.Range['1:65536'];
Range.Verticalalignment:= -4108;//Center; //垂直中对齐
//选择画表格线的范围
Range:=Sheet.Range[Sheet.cells[4,2].Address+':'
+Sheet.cells[Row-1,CurCol+1].Address];
Range.Borders.LineStyle:=1;
Range.Font.Name:='宋体'; //设置字体和大小
Range.Font.Size:=11;
Sheet.Cells.Columns.AutoFit; //自动调整列宽
Excel.visible:=true;
finally
Dataset.EnableControls;
end;
end;