给你一段代码
procedure Tfrmzcfzb.okClick(Sender: TObject);
var
filename:olevariant;
i:integer;
Begin
for i:=4 to 38 do//先初始化,如果以后仍为此值,则把相应的单元格设为''(空空);
begin
s1[i]:=-123456;
s2[i]:=-123456;
s3[i]:=-123456;
s4[i]:=-123456;
end;
Try
ExcelApplication1.Connect;
filename:=ExtractFilePath(Application.ExeName)+'\资产负债表.xls';
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks.Add(filename,0));
loadtoexcel;
ExcelApplication1.Visible[0]:=True;
ExcelWorksheet1.Disconnect;
ExcelWorkbook1.Disconnect;
ExcelApplication1.Disconnect;
Except
application.messagebox('连接出错,请检查EXCEL是否完好','提示',Mb_iconinformation+Mb_ok);
ExcelWorksheet1.Disconnect;
ExcelWorkbook1.Disconnect;
ExcelApplication1.Disconnect;
end;
end;procedure Tfrmzcfzb.loadtoexcel;
var
i:integer;
begin
loaddate();
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets.Item[1] as _worksheet);
for i:=4 to 38 do
begin
if (i=9) or (i=19) or (i=25) or (i=29) or (i=33) or (i=38) then continue;
//为自动加上去的
if s1[i]<>-123456 then
ExcelWorksheet1.Cells.Item[i,3]:=floattostr(s1[i])
else
ExcelWorksheet1.Cells.Item[i,3]:='';
if s2[i]<>-123456 then
ExcelWorksheet1.Cells.Item[i,4]:=floattostr(s2[i])
else
ExcelWorksheet1.Cells.Item[i,4]:='';
end;
for i:=4 to 38 do
begin
if (i=18) or (i=24) or (i=27) or (i=36) or (i=38) then continue;
if s3[i]<>-123456 then
ExcelWorksheet1.Cells.Item[i,7]:=floattostr(s3[i])
ELSE
ExcelWorksheet1.Cells.Item[i,7]:='';
if s4[i]<>-123456 then
ExcelWorksheet1.Cells.Item[i,8]:=floattostr(s4[i])
ELSE
ExcelWorksheet1.Cells.Item[i,8]:=''
end;
end;导出数据到EXCELprocedure Txx.btnExportToExcelClick(Sender: TObject);
var
I, iCol, iRow: Integer;
begin
try
ExcelApplication1.Connect;//try..except..fianlly
ExcelApplication1.Visible[0] := True;
ExcelApplication1.Caption := 'Excel';
ExcelApplication1.Workbooks.Add(Null, 0);
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);
ADOQX.Open;//try..except
ADOQX.First;
iRow := 1;
while not ADOQX.Eof do
begin
iCol := 1;
for I := 1 to ADOQX.FieldCount - 1 do
begin
ExcelWorksheet1.Cells.Item[iRow, iCol] := ADOQX.Fields[I - 1].AsString;
iCol := iCol + 1;
end;
ADOQX.Next;
iRow := iRow + 1;
end;
finally
ExcelApplication1.Disconnect;
end;
end;
procedure Tfrmzcfzb.okClick(Sender: TObject);
var
filename:olevariant;
i:integer;
Begin
for i:=4 to 38 do//先初始化,如果以后仍为此值,则把相应的单元格设为''(空空);
begin
s1[i]:=-123456;
s2[i]:=-123456;
s3[i]:=-123456;
s4[i]:=-123456;
end;
Try
ExcelApplication1.Connect;
filename:=ExtractFilePath(Application.ExeName)+'\资产负债表.xls';
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks.Add(filename,0));
loadtoexcel;
ExcelApplication1.Visible[0]:=True;
ExcelWorksheet1.Disconnect;
ExcelWorkbook1.Disconnect;
ExcelApplication1.Disconnect;
Except
application.messagebox('连接出错,请检查EXCEL是否完好','提示',Mb_iconinformation+Mb_ok);
ExcelWorksheet1.Disconnect;
ExcelWorkbook1.Disconnect;
ExcelApplication1.Disconnect;
end;
end;procedure Tfrmzcfzb.loadtoexcel;
var
i:integer;
begin
loaddate();
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets.Item[1] as _worksheet);
for i:=4 to 38 do
begin
if (i=9) or (i=19) or (i=25) or (i=29) or (i=33) or (i=38) then continue;
//为自动加上去的
if s1[i]<>-123456 then
ExcelWorksheet1.Cells.Item[i,3]:=floattostr(s1[i])
else
ExcelWorksheet1.Cells.Item[i,3]:='';
if s2[i]<>-123456 then
ExcelWorksheet1.Cells.Item[i,4]:=floattostr(s2[i])
else
ExcelWorksheet1.Cells.Item[i,4]:='';
end;
for i:=4 to 38 do
begin
if (i=18) or (i=24) or (i=27) or (i=36) or (i=38) then continue;
if s3[i]<>-123456 then
ExcelWorksheet1.Cells.Item[i,7]:=floattostr(s3[i])
ELSE
ExcelWorksheet1.Cells.Item[i,7]:='';
if s4[i]<>-123456 then
ExcelWorksheet1.Cells.Item[i,8]:=floattostr(s4[i])
ELSE
ExcelWorksheet1.Cells.Item[i,8]:=''
end;
end;导出数据到EXCELprocedure Txx.btnExportToExcelClick(Sender: TObject);
var
I, iCol, iRow: Integer;
begin
try
ExcelApplication1.Connect;//try..except..fianlly
ExcelApplication1.Visible[0] := True;
ExcelApplication1.Caption := 'Excel';
ExcelApplication1.Workbooks.Add(Null, 0);
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);
ADOQX.Open;//try..except
ADOQX.First;
iRow := 1;
while not ADOQX.Eof do
begin
iCol := 1;
for I := 1 to ADOQX.FieldCount - 1 do
begin
ExcelWorksheet1.Cells.Item[iRow, iCol] := ADOQX.Fields[I - 1].AsString;
iCol := iCol + 1;
end;
ADOQX.Next;
iRow := iRow + 1;
end;
finally
ExcelApplication1.Disconnect;
end;
end;
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货