procedure TForm1.Button1Click(Sender: TObject); var i,j:integer;
begin Opendialog1.Execute; ExcelApplication1.Connect; ExcelApplication1.Visible[0]:=false; excelapplication1.Workbooks.Open(Opendialog1.FileName , emptyParam,emptyParam,emptyParam,emptyParam,emptyParam, emptyParam,emptyParam,emptyParam,emptyParam,emptyParam, emptyParam,emptyParam,emptyParam,emptyParam,0); ExcelApplication1.Disconnect;//出现异常情况时关闭 ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]); ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet); for i:=1 to ExcelWorkbook1.cells.rows do for j:=1 to 6 do begin if trim(excelworksheet1.cells.item[i+1,1])<> '' then begin stringgrid1.rowCount:=i+1; stringgrid1.Cells[j,i]:=ExcelWorksheet1.Cells.Item[i+1,j]; end else begin ExcelApplication1.Disconnect; ExcelApplication1.Quit; end; end; end;以上是我找来的代码,运行后取出的全是些数值,,,不是我单元格的内容.... 请老师.........以上代码取出数据不是我要的中数据,咋全是些数字呢?????请老师指正.
下面是我用的代碼,有一edit控件用于xianshi,用一button操作 var OpenDialog1:TOpenDialog; ExcelApp:Variant; expath:String; ExCountRow,ExCountCol:integer; i,j:integer; begin stringgrid1.Clear; stringgrid1.RowCount:=2; stringgrid1.ColCount:=2; OpenDialog1:=TOpenDialog.Create(Application); with OpenDialog1 do begin Title:='xls選擇'; Filter:='Excel File(*.xls)|*.xls'; DefaultExt:='*.xls'; end; if OpenDialog1.Execute then begin Edit1.Text:=opendialog1.FileName; end; opendialog1.Free; opendialog1:=nil; application.ProcessMessages; try Screen.Cursor:=crhourglass; ExcelApp:=CreateOleObject('excel.application'); ExcelApp.visible:=false; ExcelApp.workbooks.open(edit1.Text); ExCountRow:=0; while not(length(ExcelApp.cells[ExCountRow+1,1])=0) do begin Inc(ExCountRow); end; ExCountCol:=0; while not(length(ExcelApp.cells[1,ExCountCol+1])=0) do begin Inc(ExCountCol); end; stringgrid1.RowCount:=ExCountRow; stringgrid1.ColCount:=ExCountCol+1; if (excountrow>0) and (excountcol>0) then begin for j:=1 to advstringgrid1.ColCount do stringgrid1.Cells[j,0]:=ExcelApp.cells[1,j].value; for i:=1 to stringgrid1.RowCount do for j:=1 to stringgrid1.ColCount do stringgrid1.Cells[j,i]:=ExcelApp.cells[i+1,j].value; end; ExcelApp.workbooks.close; Excelapp.quit; stringgrid1.AutoSize:=true; screen.Cursor:=crdefault; finally ExcelApp.workbooks.close; Excelapp.quit; screen.Cursor:=crdefault; end; end;
sgrid.Cells[0,0]:='序号'; i:=0; for j:= 1 to 6 do if EA.Cells[i+1,j]<>'' then begin sgrid.Cells[j,i]:=EA.Cells[j,i+1]; //sgrid.Cells[2,0]:=EA.Cells[1,2]; ea.quit; ea:=unassigned;以上是我加的判断...运行出错...
关于用程序读取Excel的你参照一下这个 http://blog.csdn.net/zxf_feng/archive/2008/03/03/2142264.aspx 对于向stringgrid写数据的参照一下这个 procedure SetGridInfo(StrGrid:TStringGrid;sOraQuery:TOraQuery); var i,j:integer; begin StrGrid.RowCount:=2; i:=1; while not sOraQuery.eof do begin StrGrid.Cells[0,i]:=inttostr(i); for j:=1 to sOraQuery.FieldCount do begin StrGrid.Cells[j,i]:=sOraQuery.Fields[j-1].AsString; end; i:=i+1; StrGrid.RowCount:=StrGrid.RowCount+1; sOraQuery.Next; end; end;
if EA.Cells[i+1,j] <>'' then ->if EA.Cells[i+1,j] <>' '(有个空格) then
var
i,j:integer;
begin
Opendialog1.Execute;
ExcelApplication1.Connect;
ExcelApplication1.Visible[0]:=false;
excelapplication1.Workbooks.Open(Opendialog1.FileName ,
emptyParam,emptyParam,emptyParam,emptyParam,emptyParam,
emptyParam,emptyParam,emptyParam,emptyParam,emptyParam,
emptyParam,emptyParam,emptyParam,emptyParam,0);
ExcelApplication1.Disconnect;//出现异常情况时关闭
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);
for i:=1 to ExcelWorkbook1.cells.rows do
for j:=1 to 6 do
begin
if trim(excelworksheet1.cells.item[i+1,1])<> '' then
begin
stringgrid1.rowCount:=i+1;
stringgrid1.Cells[j,i]:=ExcelWorksheet1.Cells.Item[i+1,j];
end
else
begin
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
end;
end;
end;以上是我找来的代码,运行后取出的全是些数值,,,不是我单元格的内容....
请老师.........以上代码取出数据不是我要的中数据,咋全是些数字呢?????请老师指正.
http://blog.csdn.net/tabby/archive/2007/11/16/1889217.aspx
procedure Tfrmmain.exceltorec(fname: string);
var
ss:string;
i:integer;
ea:variant;
begin
try
EA:=CreateOleObject('Excel.Application');
except
application.MessageBox('NO EXCEL','', MB_OK);
EXIT;
end;
EA.Visible := true;
EA.WorkBooks.Open(fname);
sgrid.Cells[1,1]:=EA.Cells[1,1];
sgrid.Cells[1,2]:=EA.Cells[1,2];
ea.quit;
ea:=unassigned;
end;
var
OpenDialog1:TOpenDialog;
ExcelApp:Variant;
expath:String;
ExCountRow,ExCountCol:integer;
i,j:integer;
begin
stringgrid1.Clear;
stringgrid1.RowCount:=2;
stringgrid1.ColCount:=2;
OpenDialog1:=TOpenDialog.Create(Application);
with OpenDialog1 do
begin
Title:='xls選擇';
Filter:='Excel File(*.xls)|*.xls';
DefaultExt:='*.xls';
end; if OpenDialog1.Execute then
begin
Edit1.Text:=opendialog1.FileName;
end;
opendialog1.Free;
opendialog1:=nil; application.ProcessMessages;
try
Screen.Cursor:=crhourglass;
ExcelApp:=CreateOleObject('excel.application');
ExcelApp.visible:=false;
ExcelApp.workbooks.open(edit1.Text); ExCountRow:=0;
while not(length(ExcelApp.cells[ExCountRow+1,1])=0) do
begin
Inc(ExCountRow);
end; ExCountCol:=0;
while not(length(ExcelApp.cells[1,ExCountCol+1])=0) do
begin
Inc(ExCountCol);
end;
stringgrid1.RowCount:=ExCountRow;
stringgrid1.ColCount:=ExCountCol+1;
if (excountrow>0) and (excountcol>0) then
begin
for j:=1 to advstringgrid1.ColCount do
stringgrid1.Cells[j,0]:=ExcelApp.cells[1,j].value; for i:=1 to stringgrid1.RowCount do
for j:=1 to stringgrid1.ColCount do
stringgrid1.Cells[j,i]:=ExcelApp.cells[i+1,j].value;
end; ExcelApp.workbooks.close;
Excelapp.quit;
stringgrid1.AutoSize:=true;
screen.Cursor:=crdefault;
finally
ExcelApp.workbooks.close;
Excelapp.quit;
screen.Cursor:=crdefault;
end;
end;
i:=0;
for j:= 1 to 6 do
if EA.Cells[i+1,j]<>'' then
begin
sgrid.Cells[j,i]:=EA.Cells[j,i+1];
//sgrid.Cells[2,0]:=EA.Cells[1,2];
ea.quit;
ea:=unassigned;以上是我加的判断...运行出错...
http://blog.csdn.net/zxf_feng/archive/2008/03/03/2142264.aspx
对于向stringgrid写数据的参照一下这个
procedure SetGridInfo(StrGrid:TStringGrid;sOraQuery:TOraQuery);
var
i,j:integer;
begin
StrGrid.RowCount:=2;
i:=1;
while not sOraQuery.eof do
begin
StrGrid.Cells[0,i]:=inttostr(i);
for j:=1 to sOraQuery.FieldCount do
begin
StrGrid.Cells[j,i]:=sOraQuery.Fields[j-1].AsString;
end;
i:=i+1;
StrGrid.RowCount:=StrGrid.RowCount+1;
sOraQuery.Next;
end;
end;