delphi2010怎么操作EXCEL??

nkym0626 2009-10-08 12:21:06
已经uses了ComObj
可是

var
ExcelApp:Variant;

ExcelApp:=CreateOleObject( 'Excel.Application' );


ExcelApp.ActiveSheet.Columns[1].Delete;这句语法错误,郁闷
...全文
405 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
nkym0626 2009-10-09
  • 打赏
  • 举报
回复
请问下怎么进行列删除呢?
qinmaofan 2009-10-08
  • 打赏
  • 举报
回复
Function StatData(aqy, AdoQuery: TAdoQuery; const TermName, gcName: string): boolean;
var
i,j,k,m,RecNum,SubID: integer;
f: double;
str,ExcelName,tmp,TemplFile: string;
excel,WorkBook,Sheet: Variant;
begin
result:= false;
ExcelName:= IncludeTrailingBackslash(ExtractFilePath(ParamStr(0))) + 'report.xls';
TemplFile:= IncludeTrailingBackslash(ExtractFilePath(ParamStr(0))) + 'Templ.xlt';
if not FileExists(TemplFile) then begin
MessageBox(GetForegroundWindow, pchar(Format('生成统计报表必需的模板文件 %s 找不到。', [TemplFile])), pchar('error'), mb_IconError);
exit;
end;

try
Excel := CreateOleObject( 'Excel.Application' );
except
MessageBox(Application.handle, '未安装Excel。', pchar(_Caption), mb_IconError);
exit;
end;
WorkBook := Excel.Workbooks.Open(TemplFile);
excel.visible:= true;
try

with aqy do begin
{ Excel 中增加工作表,改名。 }
if gcName = '' then begin
Close;
sql.Text := 'SELECT FullName from ClassData WHERE gcid in (SELECT DISTINCT gcid FROM SessionData WHERE TermName = ' + '''' + TermName + '''' + ') ORDER BY FullName';
Open;
k:= 0;
First;
While not Eof do begin
if k > 0 then
WorkBook.sheets[1].copy(WorkBook.sheets[1]);
tmp:= Fields[0].AsString ;
WorkBook.sheets[1].Name:= tmp;
Next;
Inc(k);
end;
end else
WorkBook.sheets[1].Name:= gcName;

{ 算平均分 }
if gcName <> '' then begin
Close;
sql.Text:= 'SELECT FullName, SubID, AVG(SubScore) as avg01, Count(*) as count01 FROM StudentAnswer ' +
'INNER JOIN ' +
'(SELECT SessionID, FullName FROM SessionData INNER JOIN ClassData ' +
'ON SessionData.gcid = ClassData.gcID WHERE (TermName = ' + '''' + TermName + '''' + ') and (FullName = ' + '''' + gcName + '''' + ')) AA ' +
'ON StudentAnswer.SessionID = AA.SessionID ' +
'GROUP BY FullName, SubID ORDER BY FullName, SubID' ;
Open;
end else
begin
Close;
sql.Clear;
sql.Add('SELECT FullName, SubID, AVG(SubScore) as avg01, Count(*) as count01 FROM StudentAnswer ');
sql.Add('INNER JOIN ');
sql.Add('(SELECT SessionID, FullName FROM SessionData INNER JOIN ClassData ');
sql.Add('ON SessionData.gcid = ClassData.gcID WHERE (TermName = ' + '''' + TermName + '''' + ')) AA ');
sql.Add('ON StudentAnswer.SessionID = AA.SessionID ');
sql.Add('GROUP BY FullName, SubID ORDER BY FullName, SubID');
Open;
end;

{ 写入excel报表 }
str:= '';
First;
while not Eof do begin
tmp:= FieldByName('FullName').AsString;
if tmp <> str then begin
WorkBook.Sheets[tmp].Activate;
WorkBook.ActiveSheet.Cells[2, 3].value:= tmp;
WorkBook.ActiveSheet.Cells[2, 8].value:= FieldByName('count01').AsInteger;
end;
str:= tmp;
SubID:= FieldByName('SubID').AsInteger;
f:= RoundTo(FieldByName('avg01').AsFloat, -2);
WorkBook.ActiveSheet.Cells[22, SubID + 3].value:= FloatToStr(f);
Next;
end;

{ 算百分比 }
if gcName <> '' then begin
Close;
sql.Text := 'SELECT FullName, SubID, ' +
'SUM(field01) as sum01, SUM(field02) as sum02, SUM(field03) as sum03, SUM(field04) as sum04, ' +
'SUM(field05) as sum05, SUM(field06) as sum06, SUM(field07) as sum07, SUM(field08) as sum08, ' +
'SUM(field09) as sum09, SUM(field10) as sum10, SUM(field11) as sum11, SUM(field12) as sum12, ' +
'SUM(field13) as sum13, SUM(field14) as sum14, SUM(field15) as sum15, SUM(field16) as sum16, ' +
'SUM(field17) as sum17, SUM(field18) as sum18 ' +
'FROM studentanswer INNER JOIN ' +
'(SELECT SessionID, FullName FROM SessionData ' +
'INNER JOIN ClassData ON SessionData.gcid = ClassData.gcID WHERE (TermName = ' + '''' + TermName + '''' + ') and (FullName = ' + '''' + gcName + '''' + ')) AA ' +
'ON StudentAnswer.SessionID = AA.SessionID ' +
'GROUP BY FullName, SubID ORDER BY FullName, SubID';
Open;
end else
begin
Close;
sql.Text := 'SELECT FullName, SubID, ' +
'SUM(field01) as sum01, SUM(field02) as sum02, SUM(field03) as sum03, SUM(field04) as sum04, ' +
'SUM(field05) as sum05, SUM(field06) as sum06, SUM(field07) as sum07, SUM(field08) as sum08, ' +
'SUM(field09) as sum09, SUM(field10) as sum10, SUM(field11) as sum11, SUM(field12) as sum12, ' +
'SUM(field13) as sum13, SUM(field14) as sum14, SUM(field15) as sum15, SUM(field16) as sum16, ' +
'SUM(field17) as sum17, SUM(field18) as sum18 ' +
'FROM studentanswer INNER JOIN ' +
'(SELECT SessionID, FullName FROM SessionData ' +
'INNER JOIN ClassData ON SessionData.gcid = ClassData.gcID WHERE (TermName = ' + '''' + TermName + '''' + ')) AA ' +
'ON StudentAnswer.SessionID = AA.SessionID ' +
'GROUP BY FullName, SubID ORDER BY FullName, SubID';
Open;
end;

{ 写入excel报表 }
str:= '';
RecNum:= 0;
First;
while not Eof do begin
tmp:= FieldByName('FullName').AsString;
if tmp <> str then begin
WorkBook.Sheets[tmp].Activate;
RecNum:= WorkBook.ActiveSheet.Cells[2, 8].value;
end;
str:= tmp;
SubID:= FieldByName('SubID').AsInteger;
for i:= 1 to STAT_COUNT do begin
m:= Abs(Fields[i + 1].AsInteger);
WorkBook.ActiveSheet.Cells[i + 3, SubID + 3].value:= FloatToStr(RoundTo(100* m / RecNum, -2)) + '%';
end;
Next;
end;

end; {of WITH}

finally
//Excel.ActiveWorkBook.SaveAs(ExcelName);
Excel:= Unassigned;
end;
end;
qinmaofan 2009-10-08
  • 打赏
  • 举报
回复
给你个例子看看吧
nkym0626 2009-10-08
  • 打赏
  • 举报
回复
已经uses了ComObj
可是

var
ExcelApp:Variant;

ExcelApp:=CreateOleObject( 'Excel.Application' );


ExcelApp.ActiveSheet.Columns[1].Delete;
这条在delphi2010下不通过...
Q315054403 2009-10-08
  • 打赏
  • 举报
回复
没创建相关的Docment and Sheet吧
s11ss 2009-10-08
  • 打赏
  • 举报
回复
语法没错阿

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧