我将dbgrid中的数据导入excel中后,想对某一字段排序,该怎么写呀?(100分)

B

bluedna

Unregistered / Unconfirmed
GUEST, unregistred user!
我将dbgrid中的数据导入excel中后,想对某一字段排序,该怎么写呀?
我是用的下面代码:
procedure TForm1.Button1Click(Sender: TObject);
var
myexcel:variant;
workbook:eek:levariant;
worksheet:eek:levariant;
i,j:integer;
begin
try
myexcel:=createoleobject('excel.application');
myexcel.application.workbooks.add;
myexcel.caption:='将数据导入到EXCEL表中';
myexcel.application.visible:=true;
workbook:=myexcel.application.workbooks[1];
worksheet:=workbook.worksheets.item[1];
except
showmessage('EXCEL不存在!');
end;
i:=0;
table1.first;
while not table1.eof do
begin
inc(i);
for j:=0 to table1.fieldcount-1 do
worksheet.cells[i,j+1]:=table1.fields[j].asstring;
table1.next;
end;
end;

 
在导入之前先排序不就行了吗?
 
我用的TTable控件怎么排序呀??
 
创建索引!
table1.indexfieldname:=fieldname
 
楼上大侠,我也试过 table1.indexfieldname:=fieldname 但总是出错:

project project1.exe raisde exception class edatabaseerror with message‘table1:no index for fields 'class''.process stopped use step or run to continue.
 
你到底想给哪个排序?
EXCEL表,可以使用数据栏目中排序...(非常简单的)
TABLE,可以参考设置索引字段.在很多数据库中,存放位置不是能控制的.但是加上索引或是用SQL语言调用在把结果加载到GRID中就可以了.在SQL语言中可以假如ORDER BY 语句.
 
使用控件是最好的
 
顶部