如何判定当前Excel中有多少条数据? ( 积分: 50 )

  • 主题发起人 主题发起人 张利中
  • 开始时间 开始时间

张利中

Unregistered / Unconfirmed
GUEST, unregistred user!
现在我采用delphi自带的三个Excel控件连接Excel文件。
假设文件中有10行数据。那么我怎么用编程的方法得到
当前数据的实际行数(即10)。
我采用下面代码,但得到是错误的。
procedure TForm1.Button1Click(Sender: TObject);
var
Current_File:string;
str:string;
sheet:variant;
i,j:integer;
begin
j:=0;
str:='';
if (combobox1.Text<>'') AND (combobox2.Text<>'') and (combobox3.Text<>'')
AND (combobox4.Text<>'')
then
begin

Current_File:='d:/grade_info/program_file/excel_file/'
+combobox1.text+'_'+combobox2.Text+'_'+combobox3.Text+'_'+combobox4.Text+'.xls';
//启动excel,读取数据

ExcelApplication1.Connect;

ExcelWorkBook1.ConnectTo
(ExcelApplication1.Workbooks.Open(Current_File, EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam, EmptyParam,EmptyParam,0));

ExcelWorkBook1.Activate;
ExcelWorksheet1.ConnectTo(ExcelWorkBook1.ActiveSheet as _WorkSheet);
ExcelWorkSheet1.Activate;
sheet:=ExcelApplication1.ActiveSheet;


for i:=0 to 100 do
begin
if trim(ExcelWorkSheet1.cells.item[i+2,1])<>'' then
str:=trim(ExcelWorkSheet1.cells.item[i+2,1]);
combobox5.Items.Add(str);
j=j+1;
end;

edit2.text:=IntToStr(j);

end;

ExcelWorkSheet1.Disconnect;
ExcelWorkBook1.Disconnect;
ExcelApplication1.Disconnect;
ExcelApplication1.quit;
 
现在我采用delphi自带的三个Excel控件连接Excel文件。
假设文件中有10行数据。那么我怎么用编程的方法得到
当前数据的实际行数(即10)。
我采用下面代码,但得到是错误的。
procedure TForm1.Button1Click(Sender: TObject);
var
Current_File:string;
str:string;
sheet:variant;
i,j:integer;
begin
j:=0;
str:='';
if (combobox1.Text<>'') AND (combobox2.Text<>'') and (combobox3.Text<>'')
AND (combobox4.Text<>'')
then
begin

Current_File:='d:/grade_info/program_file/excel_file/'
+combobox1.text+'_'+combobox2.Text+'_'+combobox3.Text+'_'+combobox4.Text+'.xls';
//启动excel,读取数据

ExcelApplication1.Connect;

ExcelWorkBook1.ConnectTo
(ExcelApplication1.Workbooks.Open(Current_File, EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam, EmptyParam,EmptyParam,0));

ExcelWorkBook1.Activate;
ExcelWorksheet1.ConnectTo(ExcelWorkBook1.ActiveSheet as _WorkSheet);
ExcelWorkSheet1.Activate;
sheet:=ExcelApplication1.ActiveSheet;


for i:=0 to 100 do
begin
if trim(ExcelWorkSheet1.cells.item[i+2,1])<>'' then
str:=trim(ExcelWorkSheet1.cells.item[i+2,1]);
combobox5.Items.Add(str);
j=j+1;
end;

edit2.text:=IntToStr(j);

end;

ExcelWorkSheet1.Disconnect;
ExcelWorkBook1.Disconnect;
ExcelApplication1.Disconnect;
ExcelApplication1.quit;
 
rowscount := Sheet1.usedRange.Cells.rows.count
 
很好解决。你做的单循环的计数器变量j的值即为实际记录数据。若
为双层循环,则需用j去除以外循环次数,所得结果即为实际记录数据。
 
多人接受答案了。
 
后退
顶部