导出excel问题,(str := str + DBGrid1.Columns.Items.Field.AsString + char(9);)这行代码高亮显示。不知哪里出问题了。
procedure TForm2.BitBtn5Click(Sender: TObject);
var
s:TStringList;
str:string;
i:Integer;
ASaveDialog : TSaveDialog;
tofileName : string;
begin
str:='';
tofileName := '';
DBGrid1.DataSource.DataSet.DisableControls;
try
for i:=0 to DBGrid1.Columns.Count - 1 do
str := str + DBGrid1.Columns.Items.Title.Caption + char(9);
str := str + #13;
if not DBGrid1.DataSource.DataSet.IsEmpty then
begin
DBGrid1.DataSource.DataSet.First;
while not(DBGrid1.DataSource.DataSet.eof) do
begin
for i := 0 to DBGrid1.Columns.Count - 1 do
str := str + DBGrid1.Columns.Items.Field.AsString + char(9);
str := str + #13;
DBGrid1.DataSource.DataSet.next;
end;//end while
end;
finally
DBGrid1.DataSource.DataSet.EnableControls;
end;
s := TStringList.Create;
try
s.Add(str);
ASaveDialog := TSaveDialog.Create(nil);
try
ASaveDialog.Title := '保存文件';
ASaveDialog.Filter := 'Microsof Excel(*.xls)|*.xls';
if ASaveDialog.Execute then
begin
tofileName := ASaveDialog.FileName;
if pos(uppercase('.xls'),uppercase(tofileName)) = 0 then
tofileName := tofileName +'.xls';
if FileExists(tofileName) then
begin
if MessageDLG('文件已存在,确认覆盖?',mtWarning,[mbOK,mbCancel],0) = mrOK then
begin
Deletefile(tofileName);
s.SaveToFile(tofileName);//保存
Application.MessageBox('Excel数据导出完毕!','提示', 64);
end;
end
else
begin
s.SaveToFile(tofileName);//保存
Application.MessageBox('Excel数据导出完毕!','提示', 64);
end;
end;
finally
FreeAndNil(ASaveDialog);
end;
finally
FreeAndNil(s);
end;end;
procedure TForm2.BitBtn5Click(Sender: TObject);
var
s:TStringList;
str:string;
i:Integer;
ASaveDialog : TSaveDialog;
tofileName : string;
begin
str:='';
tofileName := '';
DBGrid1.DataSource.DataSet.DisableControls;
try
for i:=0 to DBGrid1.Columns.Count - 1 do
str := str + DBGrid1.Columns.Items.Title.Caption + char(9);
str := str + #13;
if not DBGrid1.DataSource.DataSet.IsEmpty then
begin
DBGrid1.DataSource.DataSet.First;
while not(DBGrid1.DataSource.DataSet.eof) do
begin
for i := 0 to DBGrid1.Columns.Count - 1 do
str := str + DBGrid1.Columns.Items.Field.AsString + char(9);
str := str + #13;
DBGrid1.DataSource.DataSet.next;
end;//end while
end;
finally
DBGrid1.DataSource.DataSet.EnableControls;
end;
s := TStringList.Create;
try
s.Add(str);
ASaveDialog := TSaveDialog.Create(nil);
try
ASaveDialog.Title := '保存文件';
ASaveDialog.Filter := 'Microsof Excel(*.xls)|*.xls';
if ASaveDialog.Execute then
begin
tofileName := ASaveDialog.FileName;
if pos(uppercase('.xls'),uppercase(tofileName)) = 0 then
tofileName := tofileName +'.xls';
if FileExists(tofileName) then
begin
if MessageDLG('文件已存在,确认覆盖?',mtWarning,[mbOK,mbCancel],0) = mrOK then
begin
Deletefile(tofileName);
s.SaveToFile(tofileName);//保存
Application.MessageBox('Excel数据导出完毕!','提示', 64);
end;
end
else
begin
s.SaveToFile(tofileName);//保存
Application.MessageBox('Excel数据导出完毕!','提示', 64);
end;
end;
finally
FreeAndNil(ASaveDialog);
end;
finally
FreeAndNil(s);
end;end;
上句应写成:
str := str + DBGrid1.Columns[i].Title + char(9);
…………
str := str + DBGrid1.Columns.Items.Field.AsString + char(9);
这句要写成:
str := str + DBGrid1.Columns[i].Field.Value + char(9);
而且好像第三方的控件,比如dxdbgrid都提供了savetofile的方法,可以直接导成excel文件。这里有我以前网上找的一个导出execel的方法,直接输入连接数据库的连接字和sql语句就可以直接导出excel了,很方便,你可以参考一下http://www.wanggq.cn/post/32.html
var
ProgressBar1:tProgressBar;
form:tform;
begin
application.CreateForm(tform,form);
form.Position:=poScreenCenter;
form.BorderStyle:=bsnone;
form.Height:=30;
form.Width:=260;
ProgressBar1:=tProgressBar.Create(form);
ProgressBar1.Smooth:=true;
ProgressBar1.Max:=max;
ProgressBar1.Parent:=form;
ProgressBar1.Height:=20;
ProgressBar1.Width:=250;
ProgressBar1.Left:=5;
ProgressBar1.Top:=5;
ProgressBar1.Step:=1;
form.Show;
result:=ProgressBar1;
end;
function ExportToExcel(dbgrid:tdbgrid):boolean;
const
xlNormal=-4143;
var
i,j,k:integer;
str,filename:string;
excel:OleVariant;
SavePlace: TBook;
savedialog:tsavedialog;
ProgressBar1:TProgressBar;
begin
result:=false;
filename:='';
if dbgrid.DataSource.DataSet.RecordCount>65536 then
begin
if application.messagebox('需要导出的数据过大,Excel最大只能容纳65536行,是否还要继续?','询问',mb_yesno+mb_iconquestion)=idno then
exit;
end;
screen.Cursor:=crHourGlass;
try
excel:=CreateOleObject('Excel.Application');
excel.workbooks.add;
except
screen.cursor:=crDefault;
showmessage('无法调用Excel!');
exit;
end;
savedialog:=tsavedialog.Create(nil);
savedialog.Filter:='Excel文件(*.xls)|*.xls';
if savedialog.Execute then
begin
if FileExists(savedialog.FileName) then
try
if application.messagebox('该文件已经存在,要覆盖吗?','询问',mb_yesno+mb_iconquestion)=idyes then
DeleteFile(PChar(savedialog.FileName))
else
begin
Excel.Quit;
savedialog.free;
screen.cursor:=crDefault;
Exit;
end;
except
Excel.Quit;
savedialog.free;
screen.cursor:=crDefault;
Exit;
end;
filename:=savedialog.FileName;
end;
savedialog.free;
if filename='' then
begin
result:=true;
Excel.Quit;
screen.cursor:=crDefault;
exit;
end;
k:=0;
for i:=0 to dbgrid.Columns.count-1 do
begin
if dbgrid.Columns.Items[i].Visible then
begin
//Excel.Columns[k+1].ColumnWidth:=dbgrid.Columns.Items[i].Title.Column.Width;
excel.cells[1,k+1]:=dbgrid.Columns.Items[i].Title.Caption;
inc(k);
end;
end; dbgrid.DataSource.DataSet.DisableControls;
saveplace:=dbgrid.DataSource.DataSet.GetBook;
dbgrid.DataSource.dataset.First;
i:=2;
if dbgrid.DataSource.DataSet.recordcount>65536 then
ProgressBar1:=ProgressBarform(65536)
else
ProgressBar1:=ProgressBarform(dbgrid.DataSource.DataSet.recordcount);
while not dbgrid.DataSource.dataset.Eof do
begin
k:=0;
for j:=0 to dbgrid.Columns.count-1 do
begin
if dbgrid.Columns.Items[j].Visible then
begin
excel.cells[i,k+1].NumberFormat:='@';
if not dbgrid.DataSource.dataset.fieldbyname(dbgrid.Columns.Items[j].FieldName).isnull then
begin
str:=dbgrid.DataSource.dataset.fieldbyname(dbgrid.Columns.Items[j].FieldName).value;
Excel.Cells[i, k + 1] := Str;
end;
inc(k);
end
else
continue;
end;
if i=65536 then
break;
inc(i);
ProgressBar1.StepBy(1);
dbgrid.DataSource.dataset.next;
end;
progressbar1.Parent.Free; dbgrid.DataSource.dataset.GotoBook(SavePlace);
dbgrid.DataSource.dataset.EnableControls; try
if copy(FileName,length(FileName)-3,4)<>'.xls' then
FileName:=FileName+'.xls';
Excel.ActiveWorkbook.SaveAs(FileName, xlNormal, '', '', False, False);
except
Excel.Quit;
screen.cursor:=crDefault;
exit;
end;
Excel.Visible := true;
screen.cursor:=crDefault;
Result := true;
end;