我写了一个将dbgrid中数据倒入excel中的过程,但用户的要求是。
数据倒入后还要求和。如果我直接写求和的代码,则求和成功。如果是用程序动态生成的代码则求和不成功。为什么??
大家可以将我的过程加入到你的程序中试一下,拜托!!!
procedure Tpublic.SaveToExcel(title, tip_msg: string; dbgrid1: TDbgrid);
var
XL, Xarr,sumstr: Variant;
I,clums: Integer; //列数
j,rows : Integer; //行数
fieldcunt:integer;//dbgrid1的字段个数
begin
{note the ComObj (example OleAuto not correct) in the uses}
// Create an array of query element size
try
screen.Cursor:=crhourglass;
fieldcunt:=dbgrid1.FieldCount;
Xarr:=VarArrayCreate([1,fieldcunt],varVariant);
XL:=CreateOLEObject('Excel.Application'); // Ole object creation
XL.WorkBooks.add; for i := 0 to dbgrid1.Columns.Count - 1 do
begin //添加标题信息
XL.range[chr(65+i)+'2',chr(65+i)+'2'] := dbgrid1.Columns.Items[i].Title.Caption;
end;
j := 3; //行数
dbgrid1.DataSource.DataSet.First;
while not dbgrid1.DataSource.DataSet.Eof do
begin //添加内容信息
for i:= 0 to fieldcunt - 1 do //列数
begin
XL.range[chr(65+i)+inttostr(j),chr(65+i)+inttostr(j)] := dbgrid1.Columns.Items[i].Field.AsString;
clums:=i;
end;
Inc(j);
rows:=j;
dbgrid1.DataSource.DataSet.Next;
end;
//---------------进行求和操作---------------------
sumstr:='=SUM(R[-'+inttostr(rows-3)+']C:R[-1]C)';
for i:=2 to clums do
begin
XL.Range[chr(64+i)+'3',chr(64+i)+inttostr(rows-1)].Select;
XL.Range[chr(64+i)+inttostr(rows)].Activate; XL.ActiveCell.FormulaR1C1 :='=SUM(R[-11]C:R[-1]C)';//直接写的求和公式
// XL.ActiveCell.FormulaR1C1 :=sumstr;//程序生成的求和公式
end;
//XL.Range['B4','B15'].Select;
//-----------------选择所有的信息设置内容字体及大小----------
XL.cells.select; // Select everything
XL.Selection.Font.Name:='宋体';
XL.Selection.Font.Size:=12;
XL.selection.Columns.AutoFit;
//----------------再次选择设置标题------------------------------------
XL.RANGE['A1',chr(65+dbgrid1.Columns.Count-1)+'1'].SELECT;
XL.SELECTION.Merge; //对选中的单元格进行合并操作
XL.SELECTION.value:=title;
XL.Selection.Font.Name:='宋体';
XL.Selection.Font.Size:=16;
XL.Selection.Font.ColorIndex := 3 ;
XL.Selection.Font.Bold := True;//-------------------------------------------------------------------
screen.Cursor:=crdefault;
XL.visible:=true;
except
messagebox(0,pchar(tip_msg),'系统提示',mb_ok+mb_iconwarning+mb_taskmodal);
end;
end;
数据倒入后还要求和。如果我直接写求和的代码,则求和成功。如果是用程序动态生成的代码则求和不成功。为什么??
大家可以将我的过程加入到你的程序中试一下,拜托!!!
procedure Tpublic.SaveToExcel(title, tip_msg: string; dbgrid1: TDbgrid);
var
XL, Xarr,sumstr: Variant;
I,clums: Integer; //列数
j,rows : Integer; //行数
fieldcunt:integer;//dbgrid1的字段个数
begin
{note the ComObj (example OleAuto not correct) in the uses}
// Create an array of query element size
try
screen.Cursor:=crhourglass;
fieldcunt:=dbgrid1.FieldCount;
Xarr:=VarArrayCreate([1,fieldcunt],varVariant);
XL:=CreateOLEObject('Excel.Application'); // Ole object creation
XL.WorkBooks.add; for i := 0 to dbgrid1.Columns.Count - 1 do
begin //添加标题信息
XL.range[chr(65+i)+'2',chr(65+i)+'2'] := dbgrid1.Columns.Items[i].Title.Caption;
end;
j := 3; //行数
dbgrid1.DataSource.DataSet.First;
while not dbgrid1.DataSource.DataSet.Eof do
begin //添加内容信息
for i:= 0 to fieldcunt - 1 do //列数
begin
XL.range[chr(65+i)+inttostr(j),chr(65+i)+inttostr(j)] := dbgrid1.Columns.Items[i].Field.AsString;
clums:=i;
end;
Inc(j);
rows:=j;
dbgrid1.DataSource.DataSet.Next;
end;
//---------------进行求和操作---------------------
sumstr:='=SUM(R[-'+inttostr(rows-3)+']C:R[-1]C)';
for i:=2 to clums do
begin
XL.Range[chr(64+i)+'3',chr(64+i)+inttostr(rows-1)].Select;
XL.Range[chr(64+i)+inttostr(rows)].Activate; XL.ActiveCell.FormulaR1C1 :='=SUM(R[-11]C:R[-1]C)';//直接写的求和公式
// XL.ActiveCell.FormulaR1C1 :=sumstr;//程序生成的求和公式
end;
//XL.Range['B4','B15'].Select;
//-----------------选择所有的信息设置内容字体及大小----------
XL.cells.select; // Select everything
XL.Selection.Font.Name:='宋体';
XL.Selection.Font.Size:=12;
XL.selection.Columns.AutoFit;
//----------------再次选择设置标题------------------------------------
XL.RANGE['A1',chr(65+dbgrid1.Columns.Count-1)+'1'].SELECT;
XL.SELECTION.Merge; //对选中的单元格进行合并操作
XL.SELECTION.value:=title;
XL.Selection.Font.Name:='宋体';
XL.Selection.Font.Size:=16;
XL.Selection.Font.ColorIndex := 3 ;
XL.Selection.Font.Bold := True;//-------------------------------------------------------------------
screen.Cursor:=crdefault;
XL.visible:=true;
except
messagebox(0,pchar(tip_msg),'系统提示',mb_ok+mb_iconwarning+mb_taskmodal);
end;
end;
在EXCEL中的SUM格式应该是这样的
'=SUM(c8:d9)'
大家将程序复制到实际环境中试一下吧!!!!!!!!!!1
Cells.Item[X,Y]:='=SUM(X:Y)'
Cells[X,Y].Formula:='=sum(X:Y)'