delphi7导出excel的问题
代码如下functionTForm12.ToExcel(sfilename:string;ADOQuery:TADOQuery):boolean;//生成excelcon...
代码如下
function TForm12.ToExcel(sfilename: string; ADOQuery: TADOQuery): boolean; //生成excelconst
xlNormal = -4143;
var
y: integer;
tsList: TStringList;
s, filename: string;
aSheet: Variant;
excel: OleVariant;
savedialog: tsavedialog;
begin
Result := true;
try
excel := CreateOleObject('Excel.Application');
excel.workbooks.add;
except
//screen.cursor:=crDefault;
showmessage('无法调用Excel!');
exit;
end;
savedialog := tsavedialog.Create(nil);
savedialog.FileName := sfilename; //存入文件
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;
aSheet := excel.Worksheets.Item[1];
tsList := TStringList.Create;
//tsList.Add('查询结果'); //加入标题
s := ''; //加入字段名 for y := 0 to adoquery.fieldCount - 1 do
begin
s := s + adoQuery.Fields.Fields[y].FieldName + #9;
Application.ProcessMessages;
end;
tsList.Add(s);
try
try
ADOQuery.First;
while not ADOQuery.Eof do
begin
s := '';
for y := 0 to ADOQuery.FieldCount - 1 do
begin
s := s + ADOQuery.Fields[y].AsString + #9;
Application.ProcessMessages;
end;
tsList.Add(s);
ADOQuery.next; end;
Clipboard.AsText := tsList.Text;
except
result := false;
end;
finally
tsList.Free;
end;
aSheet.Paste;
MessageBox(Application.Handle, '数据导出完毕!', '系统提示', MB_ICONINFORMATION or MB_OK);
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 := false; //true会自动打开已经保存的excel
Excel.Quit;
Excel := UnAssigned;
end;
短点的内容还好,长点的就成这样了,怎么解决啊?!!(不要看马赛克,看下面的) 展开
function TForm12.ToExcel(sfilename: string; ADOQuery: TADOQuery): boolean; //生成excelconst
xlNormal = -4143;
var
y: integer;
tsList: TStringList;
s, filename: string;
aSheet: Variant;
excel: OleVariant;
savedialog: tsavedialog;
begin
Result := true;
try
excel := CreateOleObject('Excel.Application');
excel.workbooks.add;
except
//screen.cursor:=crDefault;
showmessage('无法调用Excel!');
exit;
end;
savedialog := tsavedialog.Create(nil);
savedialog.FileName := sfilename; //存入文件
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;
aSheet := excel.Worksheets.Item[1];
tsList := TStringList.Create;
//tsList.Add('查询结果'); //加入标题
s := ''; //加入字段名 for y := 0 to adoquery.fieldCount - 1 do
begin
s := s + adoQuery.Fields.Fields[y].FieldName + #9;
Application.ProcessMessages;
end;
tsList.Add(s);
try
try
ADOQuery.First;
while not ADOQuery.Eof do
begin
s := '';
for y := 0 to ADOQuery.FieldCount - 1 do
begin
s := s + ADOQuery.Fields[y].AsString + #9;
Application.ProcessMessages;
end;
tsList.Add(s);
ADOQuery.next; end;
Clipboard.AsText := tsList.Text;
except
result := false;
end;
finally
tsList.Free;
end;
aSheet.Paste;
MessageBox(Application.Handle, '数据导出完毕!', '系统提示', MB_ICONINFORMATION or MB_OK);
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 := false; //true会自动打开已经保存的excel
Excel.Quit;
Excel := UnAssigned;
end;
短点的内容还好,长点的就成这样了,怎么解决啊?!!(不要看马赛克,看下面的) 展开
展开全部
你的问题,不是内容长短的问题。
从截图的序号“104”的具体任务有很多条,每条的后面带着回车换行符,所以造成了换行。
修改代码:
m:string;
for y := 0 to ADOQuery.FieldCount - 1 do
begin
m:=StringReplace(ADOQuery.Fields[y].AsString,chr(13)+chr(10),'',[rfReplaceAll]);
s := s + m + #9;
Application.ProcessMessages;
end;
我测试了一下,可以了。不知道适用不适用你的表格。你测试一下。
祝你好运!
从截图的序号“104”的具体任务有很多条,每条的后面带着回车换行符,所以造成了换行。
修改代码:
m:string;
for y := 0 to ADOQuery.FieldCount - 1 do
begin
m:=StringReplace(ADOQuery.Fields[y].AsString,chr(13)+chr(10),'',[rfReplaceAll]);
s := s + m + #9;
Application.ProcessMessages;
end;
我测试了一下,可以了。不知道适用不适用你的表格。你测试一下。
祝你好运!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询