ls_1:=qj; for i:=1 to j do begin if ExcelApp.workbooks[1].sheets[i].name=ls_1 then begin try ExcelApp.workbooks[1].sheets[ls_1].UsedRange.Clear; //copy 公式 ExcelApp.workbooks[1].sheets['报表公式'].UsedRange.Copy; //从A1位置开始粘贴: ExcelApp.workbooks[1].sheets[ls_1].Range['A1'].PasteSpecial; ExcelApp.workbooks[1].Sheets[ls_1].Activate; ExcelApp.ActiveSheet.Range[ 'A1' ].Copy; // ExcelApp.ActiveSheet.Range['A1'].PasteSpecial; except //放弃存盘 ExcelApp.ActiveWorkBook.Saved := True; //关闭工作表 ExcelApp.workbooks[1].close; //关闭Excel ExcelApp.quit; exit; end; break; end; end; if i>j then begin //ExcelApp.workbooks[1].WorkSheets.Add; //ExcelApp.ActiveSheet.name:=ls_1; //ExcelApp.ActiveSheet.move(EmptyParam,ExcelApp.workbooks[1].WorkSheets[j+1]); ExcelApp.workbooks[1].sheets['报表公式'].copy(EmptyParam,ExcelApp.workbooks[1].WorkSheets[1]); //ExcelApp.workbooks[1].sheets['报表公式'].copy; //ExcelApp.workbooks[1].sheets[ls_1].paste; ExcelApp.ActiveSheet.name:=ls_1; end; // row_1:=ExcelApp.ActiveSheet.UsedRange.rows.count; column_1:=ExcelApp.ActiveSheet.UsedRange.columns.count; for i:=1 to row_1 do begin for j:=1 to column_1 do begin TRY ls_cell:=trim(ExcelApp.Cells[i,j].Value); except //放弃存盘 // ExcelApp.ActiveWorkBook.Saved := True; //关闭工作表 //ExcelApp.workbooks[1].close; //关闭Excel //ExcelApp.quit; //exit; continue; end; if ls_cell='' then continue; ls_cell:=UpperCase(ls_cell); site_1:=pos('ASC',ls_cell); if site_1=0 then continue; ls_cell_end:=ls_cell; while site_1>0 do begin ls_cell_left:=copy(ls_cell_end,1,site_1-1); ls_cell_right:=copy(ls_cell_end,site_1,length(ls_cell_end)-site_1+1); site_2:=pos(')',ls_cell_right); if site_2=0 then begin //放弃存盘 ExcelApp.ActiveWorkBook.Saved := True; //关闭工作表 ExcelApp.workbooks[1].close; //关闭Excel ExcelApp.quit; msgboxinfo('第'+inttostr(i)+'行'+inttostr(j)+'列公式错误 '+ls_cell); exit; end; ls_cell_min:=copy(ls_cell_right,1,site_2); ls_cell_right:=copy(ls_cell_right,site_2+1,length(ls_cell_end)-site_2); if not bbqs(ls_cell_min) then begin //放弃存盘 ExcelApp.ActiveWorkBook.Saved := True; //关闭工作表 ExcelApp.workbooks[1].close; //关闭Excel ExcelApp.quit; msgboxinfo('第'+inttostr(i)+'行'+inttostr(j)+'列公式错误 '+ls_cell); exit; end; ls_cell_end:=ls_cell_left+Zw_ReturnString1+ls_cell_right; site_1:=pos('ASC',ls_cell_end); end; try if checkFormula(ls_cell_end) then l_sql:='select '+ls_cell_end+' as exp1 ' else l_sql:='select '''+ls_cell_end+''' as exp1 '; with dm1.ADOquerymxkm do begin SQL.Clear; SQL.Add(l_sql); open; if IsEmpty then ls_cell_end:='' else // ls_cell_end:=floattostr(FieldByName('exp1').Value); ls_cell_end:=FieldByName('exp1').AsString; close; end; except //放弃存盘 ExcelApp.ActiveWorkBook.Saved := True; //关闭工作表 ExcelApp.workbooks[1].close; //关闭Excel ExcelApp.quit; msgboxinfo('第'+inttostr(i)+'行'+inttostr(j)+'列公式错误 '+ls_cell); exit; end; ExcelApp.Cells[i,j].Value:=ls_cell_end; end; end; // ExcelApp.ActiveWorkBook.Save; //关闭工作表 ExcelApp.workbooks[1].close; //关闭Excel ExcelApp.quit; // msgboxinfo('帐转表完成 '); exit;我没看懂这个程序是如何将表itembm中qcye值(期初余额)插入表中的,大概指点一下,可能在哪行
try
Self.ExcelApplication1:=TExcelApplication.Create(self);
Self.ExcelApplication1.Connect;
except
messagebox(application.Handle,'无法生成Excel报表,请确定安装了Excel后重试','信息',mb_ok or mb_iconinformation);
exit;
end;
Self.ExcelApplication1.Visible[0]:=True;
self.ExcelApplication1.DisplayAlerts[0]:=False;
Self.ExcelApplication1.Caption:='科目编码';
Self.ExcelApplication1.Workbooks.Add(EmptyParam,0);
Self.ExcelWorkbook1.ConnectTo(self.ExcelApplication1.Workbooks[1]);
self.ExcelWorksheet1:=TExcelWorkSheet.Create(self);
self.ExcelWorksheet1.ConnectTo(self.ExcelWorkbook1.Worksheets[1] as _worksheet);
adoquery1.close;
adoquery1.connection:=adoconnection1;
adoquery1.sql.clear;
adoquery1.sql.add('select distinct itembm from iteminfo');
adoquery1.open;
while not adoquey1.eof do
begin
i:=adoquer1.recno;
s1:=adoquery1.fieldByName('itembm').asstring;
self.ExcelWorksheet1.Cells.Item[i+1,1]:=s1;
adoquery1.next;
end;
ASC("ZW,1111,期末余额") 返回值就是ord('Z')的返回值90
for i:=1 to j do
begin
if ExcelApp.workbooks[1].sheets[i].name=ls_1 then
begin
try
ExcelApp.workbooks[1].sheets[ls_1].UsedRange.Clear;
//copy 公式
ExcelApp.workbooks[1].sheets['报表公式'].UsedRange.Copy;
//从A1位置开始粘贴:
ExcelApp.workbooks[1].sheets[ls_1].Range['A1'].PasteSpecial;
ExcelApp.workbooks[1].Sheets[ls_1].Activate;
ExcelApp.ActiveSheet.Range[ 'A1' ].Copy;
// ExcelApp.ActiveSheet.Range['A1'].PasteSpecial;
except
//放弃存盘
ExcelApp.ActiveWorkBook.Saved := True;
//关闭工作表
ExcelApp.workbooks[1].close;
//关闭Excel
ExcelApp.quit;
exit;
end;
break;
end;
end;
if i>j then
begin
//ExcelApp.workbooks[1].WorkSheets.Add;
//ExcelApp.ActiveSheet.name:=ls_1;
//ExcelApp.ActiveSheet.move(EmptyParam,ExcelApp.workbooks[1].WorkSheets[j+1]);
ExcelApp.workbooks[1].sheets['报表公式'].copy(EmptyParam,ExcelApp.workbooks[1].WorkSheets[1]);
//ExcelApp.workbooks[1].sheets['报表公式'].copy;
//ExcelApp.workbooks[1].sheets[ls_1].paste;
ExcelApp.ActiveSheet.name:=ls_1;
end;
//
row_1:=ExcelApp.ActiveSheet.UsedRange.rows.count;
column_1:=ExcelApp.ActiveSheet.UsedRange.columns.count;
for i:=1 to row_1 do
begin
for j:=1 to column_1 do
begin
TRY
ls_cell:=trim(ExcelApp.Cells[i,j].Value);
except
//放弃存盘
// ExcelApp.ActiveWorkBook.Saved := True;
//关闭工作表
//ExcelApp.workbooks[1].close;
//关闭Excel
//ExcelApp.quit;
//exit;
continue;
end;
if ls_cell='' then continue;
ls_cell:=UpperCase(ls_cell);
site_1:=pos('ASC',ls_cell);
if site_1=0 then continue;
ls_cell_end:=ls_cell;
while site_1>0 do
begin
ls_cell_left:=copy(ls_cell_end,1,site_1-1);
ls_cell_right:=copy(ls_cell_end,site_1,length(ls_cell_end)-site_1+1);
site_2:=pos(')',ls_cell_right);
if site_2=0 then
begin
//放弃存盘
ExcelApp.ActiveWorkBook.Saved := True;
//关闭工作表
ExcelApp.workbooks[1].close;
//关闭Excel
ExcelApp.quit;
msgboxinfo('第'+inttostr(i)+'行'+inttostr(j)+'列公式错误 '+ls_cell);
exit;
end;
ls_cell_min:=copy(ls_cell_right,1,site_2);
ls_cell_right:=copy(ls_cell_right,site_2+1,length(ls_cell_end)-site_2);
if not bbqs(ls_cell_min) then
begin
//放弃存盘
ExcelApp.ActiveWorkBook.Saved := True;
//关闭工作表
ExcelApp.workbooks[1].close;
//关闭Excel
ExcelApp.quit;
msgboxinfo('第'+inttostr(i)+'行'+inttostr(j)+'列公式错误 '+ls_cell);
exit;
end;
ls_cell_end:=ls_cell_left+Zw_ReturnString1+ls_cell_right;
site_1:=pos('ASC',ls_cell_end);
end;
try
if checkFormula(ls_cell_end) then
l_sql:='select '+ls_cell_end+' as exp1 '
else
l_sql:='select '''+ls_cell_end+''' as exp1 ';
with dm1.ADOquerymxkm do
begin
SQL.Clear;
SQL.Add(l_sql);
open;
if IsEmpty then
ls_cell_end:=''
else
// ls_cell_end:=floattostr(FieldByName('exp1').Value);
ls_cell_end:=FieldByName('exp1').AsString;
close;
end;
except
//放弃存盘
ExcelApp.ActiveWorkBook.Saved := True;
//关闭工作表
ExcelApp.workbooks[1].close;
//关闭Excel
ExcelApp.quit;
msgboxinfo('第'+inttostr(i)+'行'+inttostr(j)+'列公式错误 '+ls_cell);
exit;
end;
ExcelApp.Cells[i,j].Value:=ls_cell_end;
end;
end; //
ExcelApp.ActiveWorkBook.Save;
//关闭工作表
ExcelApp.workbooks[1].close;
//关闭Excel
ExcelApp.quit;
//
msgboxinfo('帐转表完成 ');
exit;我没看懂这个程序是如何将表itembm中qcye值(期初余额)插入表中的,大概指点一下,可能在哪行
的如果只是将 表itembm中qcye值(期初余额)插入Excel表中 用上面我给你的代码就行。