DELPHI 7.0 读取excel 文档的内容!
我想用一个按钮实现 对excel文档内容的读取,excel文档内有1500行 7列, 第一列为顺序号,第二列到第七列 为数据,
数据为这样 :
1 1 2 3 4 5 6
2 2 3 4 5 9 15
3 7 9 15 19 24 33
4 3 5 8 17 19 22
我想把这些值从第二列到第七列赋值给一个二维数组,
a[1][1]:=1; a[1][2]:=2;a[1][3]:=3; a[1][4]:=4; a[1][5]:=5; a[1][6]:=6;
a[2][1]:=2; a[2][2]:=3;a[2][3]:=4; a[2][4]:=5; a[2][5]:=9; a[2][6]:=15;
.......
把第一列的最后一个有效值赋值给一个integer变量,比如说1500, 然后通过通过这个interger变量,实现对读入1500行的的二维数组数据进行筛选等等其它动作。
请问我要怎么实现??请各位高手指导,最好给段源码,谢谢了!!
我想用一个按钮实现 对excel文档内容的读取,excel文档内有1500行 7列, 第一列为顺序号,第二列到第七列 为数据,
数据为这样 :
1 1 2 3 4 5 6
2 2 3 4 5 9 15
3 7 9 15 19 24 33
4 3 5 8 17 19 22
我想把这些值从第二列到第七列赋值给一个二维数组,
a[1][1]:=1; a[1][2]:=2;a[1][3]:=3; a[1][4]:=4; a[1][5]:=5; a[1][6]:=6;
a[2][1]:=2; a[2][2]:=3;a[2][3]:=4; a[2][4]:=5; a[2][5]:=9; a[2][6]:=15;
.......
把第一列的最后一个有效值赋值给一个integer变量,比如说1500, 然后通过通过这个interger变量,实现对读入1500行的的二维数组数据进行筛选等等其它动作。
请问我要怎么实现??请各位高手指导,最好给段源码,谢谢了!!
uses
comobjprocedure TForm1.Button1Click(Sender: TObject);
var
Excel, Books: OleVariant;
i, j: Integer;
Num: array[0..1499, 0..6] of Integer;
begin
if VarIsEmpty(Excel) then
Excel := CreateOleObject('Excel.Application');
if VarIsEmpty(Books) then
Books := CreateOleObject('Excel.Sheet');
try
Books := Excel.WorkBooks.Open('D:\新建 Microsoft Excel 工作表.xls');
for I := 1 to 1500 do
for j := 1 to 7 do
Num[I -1, J- 1] := Integer(Excel.Cells[J, I]);
finally
Excel.Quit;
Books.Quit;
end;
end;
uses
comobjprocedure TForm1.Button1Click(Sender: TObject);
var
Excel, Books: OleVariant;
i, j: Integer;
Num: array[0..1499, 0..6] of Integer;
begin
if VarIsEmpty(Excel) then
Excel := CreateOleObject('Excel.Application');
if VarIsEmpty(Books) then
Books := CreateOleObject('Excel.Sheet');
try
Books := Excel.WorkBooks.Open('D:\新建 Microsoft Excel 工作表.xls');
for I := 1 to 1500 do
for j := 1 to 7 do
Num[I -1, J- 1] := Integer(Excel.Cells[J, I]);
finally
Excel.Quit;
Books.Quit;
end;
end;
begin
if VarIsEmpty(Excel) then
Excel := CreateOleObject('Excel.Application');
if VarIsEmpty(Books) then
Books := CreateOleObject('Excel.Sheet');
try
Books := Excel.WorkBooks.Open('c:\数据.xls');
for ii := 1 to 990 do
for jj := 1 to 6 do
xxx[ii , jj] := Integer(Excel.Cells[ii, jj]);
finally
Excel.Quit;
// Books.Quit;
end; end;现在又有个新问题了: 文档地址我想做成模糊的。 就是说 我要把文档和project1.exe放在一起,不论我将它们两个拷贝到什么位置都可以正常读取,不用在程序里面指定地址,比如:'c:\数据.xls'。
请高手指导啊!!谢谢了!!
procedure TForm1.Button9Click(Sender: TObject); var Excel, Books: OleVariant;
var line :integer; //中奖号的期数 var a1,a2,a3,a4,a5,a6:integer; //双色球的六个红球 var give:integer; // 给定红球的上限 即33 var sum:double; //总和
var i,j,n,mm:integer;//中奖号由小到大排序用
type
x= array[0..2000,1..7] of integer; //定二维数组 放往期中奖号码
ppp = array [1..7] of Integer;
var p: ppp;
var xxx:x;
begin
give:=33; // 给定红球的上限 即33 sum:=0; line:=0;
listbox2.Items.Text:=''; begin
if VarIsEmpty(Excel) then
Excel := CreateOleObject('Excel.Application');
if VarIsEmpty(Books) then
Books := CreateOleObject('Excel.Sheet');
try
Books := Excel.WorkBooks.Open('c:\往期中奖号11.xls');
for i := 1 to 2000 do // 有问题啊
for j := 1 to 6 do
begin
if (Integer(Excel.Cells[i, j])<>0) then
begin
xxx[i , j] := Integer(Excel.Cells[i, j]);
line:=line+1;
end
else break;
end;
finally
Excel.Quit;
end; end;
line:=Trunc(line/6);//取整()
for a1:=1 to give do
for a2:=(a1+1) to give do
for a3:=(a2+1) to give do //(a2+1)
begin
for a4:=(a3+1) to give do //(a3+1)
begin
for a5:=(a4+1) to give do
begin
for a6:=(a5+1) to give do
begin for mm:=1 to line do begin if((a1=xxx[mm][1])and(a2=xxx[mm][2])and(a3=xxx[mm][3])and(a4=xxx[mm][4]))then
begin
sum:=sum+1;
ListBox1.Items.Add(inttostr(a1)+','+inttostr(a2)+','+inttostr(a3)+','+inttostr(a4)+','+inttostr(a5)+','+inttostr(a6));break;
end; // 有四个相同1
if((a1=xxx[mm][1])and(a2=xxx[mm][2])and(a3=xxx[mm][3])and(a5=xxx[mm][4]))then
begin
sum:=sum+1;
ListBox1.Items.Add(inttostr(a1)+','+inttostr(a2)+','+inttostr(a3)+','+inttostr(a4)+','+inttostr(a5)+','+inttostr(a6));break;
end;
if((a1=xxx[mm][1])and(a2=xxx[mm][2])and(a3=xxx[mm][3])and(a6=xxx[mm][4]))then
begin
sum:=sum+1;
ListBox1.Items.Add(inttostr(a1)+','+inttostr(a2)+','+inttostr(a3)+','+inttostr(a4)+','+inttostr(a5)+','+inttostr(a6));break;
end;
if((a1=xxx[mm][1])and(a2=xxx[mm][2])and(a4=xxx[mm][3])and(a5=xxx[mm][4]))then
begin
sum:=sum+1;
ListBox1.Items.Add(inttostr(a1)+','+inttostr(a2)+','+inttostr(a3)+','+inttostr(a4)+','+inttostr(a5)+','+inttostr(a6));break;
end;
if((a1=xxx[mm][1])and(a2=xxx[mm][2])and(a4=xxx[mm][3])and(a6=xxx[mm][4]))then
begin
sum:=sum+1;
ListBox1.Items.Add(inttostr(a1)+','+inttostr(a2)+','+inttostr(a3)+','+inttostr(a4)+','+inttostr(a5)+','+inttostr(a6));break;
end;
if((a1=xxx[mm][1])and(a2=xxx[mm][2])and(a5=xxx[mm][3])and(a6=xxx[mm][4]))then begin sum:=sum+1;ListBox1.Items.Add(inttostr(a1)+','+inttostr(a2)+','+inttostr(a3)+','+inttostr(a4)+','+inttostr(a5)+','+inttostr(a6));break; end;
if((a1=xxx[mm][1])and(a3=xxx[mm][2])and(a4=xxx[mm][3])and(a5=xxx[mm][4]))then begin sum:=sum+1;ListBox1.Items.Add(inttostr(a1)+','+inttostr(a2)+','+inttostr(a3)+','+inttostr(a4)+','+inttostr(a5)+','+inttostr(a6));break; end;
if((a1=xxx[mm][1])and(a3=xxx[mm][2])and(a4=xxx[mm][3])and(a6=xxx[mm][4]))then begin sum:=sum+1;ListBox1.Items.Add(inttostr(a1)+','+inttostr(a2)+','+inttostr(a3)+','+inttostr(a4)+','+inttostr(a5)+','+inttostr(a6));break; end;
if((a1=xxx[mm][1])and(a3=xxx[mm][2])and(a5=xxx[mm][3])and(a6=xxx[mm][4]))then begin sum:=sum+1;ListBox1.Items.Add(inttostr(a1)+','+inttostr(a2)+','+inttostr(a3)+','+inttostr(a4)+','+inttostr(a5)+','+inttostr(a6));break; end;
if((a1=xxx[mm][1])and(a4=xxx[mm][2])and(a5=xxx[mm][3])and(a6=xxx[mm][4]))then begin sum:=sum+1;ListBox1.Items.Add(inttostr(a1)+','+inttostr(a2)+','+inttostr(a3)+','+inttostr(a4)+','+inttostr(a5)+','+inttostr(a6));break; end;
if((a2=xxx[mm][1])and(a3=xxx[mm][2])and(a4=xxx[mm][3])and(a5=xxx[mm][4]))then begin sum:=sum+1;ListBox1.Items.Add(inttostr(a1)+','+inttostr(a2)+','+inttostr(a3)+','+inttostr(a4)+','+inttostr(a5)+','+inttostr(a6));break; end;
if((a2=xxx[mm][1])and(a3=xxx[mm][2])and(a4=xxx[mm][3])and(a6=xxx[mm][4]))then begin sum:=sum+1;ListBox1.Items.Add(inttostr(a1)+','+inttostr(a2)+','+inttostr(a3)+','+inttostr(a4)+','+inttostr(a5)+','+inttostr(a6));break; end;
if((a2=xxx[mm][1])and(a3=xxx[mm][2])and(a5=xxx[mm][3])and(a6=xxx[mm][4]))then begin sum:=sum+1;ListBox1.Items.Add(inttostr(a1)+','+inttostr(a2)+','+inttostr(a3)+','+inttostr(a4)+','+inttostr(a5)+','+inttostr(a6));break; end;
if((a2=xxx[mm][1])and(a4=xxx[mm][2])and(a5=xxx[mm][3])and(a6=xxx[mm][4]))then begin sum:=sum+1;ListBox1.Items.Add(inttostr(a1)+','+inttostr(a2)+','+inttostr(a3)+','+inttostr(a4)+','+inttostr(a5)+','+inttostr(a6));break; end;
if((a3=xxx[mm][1])and(a4=xxx[mm][2])and(a5=xxx[mm][3])and(a6=xxx[mm][4]))then begin sum:=sum+1;ListBox1.Items.Add(inttostr(a1)+','+inttostr(a2)+','+inttostr(a3)+','+inttostr(a4)+','+inttostr(a5)+','+inttostr(a6));break; end; end;
end
end
end
end;
label1.Caption:=FloatToStr(sum);//*输出总数*/
end;