小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Excel文件導(dǎo)入StringGrid

 likejian 2010-08-07
這是我看到的文章-----不是源作我暫時還沒驗證正確性

EXCEL電子表格作為辦公軟件OFFICE中的重要組成部份,是日常辦公系統(tǒng)的主要助手,因此許多日常所需的業(yè)務(wù)方面的數(shù)據(jù)通常是通過電子表格存取。有時我們需要從日常工作中創(chuàng)建的EXCEL中取得數(shù)據(jù)進行操作、打印、查詢,統(tǒng)計等工作。在這里我將介紹如何利用delphi完成EXCEL電子表格中數(shù)據(jù)的操作。

一、新建一項目,從控件欄servers中分別選取控件:excelapplication、excelworkbook1、excelworksheet,放到主窗體from1中,并加入stringgrid、三個按鈕、五個顯示字段內(nèi)容的EDIT、二個操作顯示記錄的label、一個用于打開EXCEL電子表格的控件opendialog等,如下圖所示:



二、選擇excel表'按鈕,用于打開EXCEL文件,其代碼如下:
procedure TForm1.Button1Click(Sender: TObject);
var i,j:integer;
begin
opendialog1.InitialDir:=ExtractFileDir(paramstr(0));//文件的打存放初始路徑
opendialog1.Execute;
Try
ExcelApplication1.Connect;//EXCEL應(yīng)用程序
Except
MessageDlg('Excel may not be installed',mtError, [mbOk], 0);
Abort;
End;
ExcelApplication1.Visible[0]:=True;
ExcelApplication1.Caption:='Excel Application';
try
excelapplication1.Workbooks.Open(opendialog1.FileName,
null,null,null,null,null,null,null,null,null,null,null,null,0);//打開指定的EXCEL 文件
except
begin
ExcelApplication1.Disconnect;//出現(xiàn)異常情況時關(guān)閉
ExcelApplication1.Quit;showmessage('請選擇EXCEL電子表格!');
exit;
end;
end;
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);//ExcelWorkbook1與Eexcelapplication1建立連接
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);//Excelworksheet1與Excelworkbook1建立連接
//開始從EXCEL中取數(shù),放到stringgrid1中,取完數(shù)后關(guān)閉EXCEL
for i:=1 to 1000 do//最大取值1000
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
label3.caption:=inttostr(i-1);
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
//將第一條數(shù)據(jù)賦給編輯框
edit2.text:=stringgrid1.Cells[1,1];
edit1.text:=stringgrid1.Cells[2,1];
edit3.text:=stringgrid1.Cells[3,1];
edit4.text:=stringgrid1.Cells[4,1];
edit5.text:=stringgrid1.Cells[5,1];
exit;
end;
end;
end;

三、'下一條記錄'按鈕,完成記錄向下移動,代碼如下:
procedure TForm1.Button2Click(Sender: TObject);
var x:integer;
begin
x:=stringgrid1.row+1;
if x<> stringgrid1.RowCount then
begin
stringgrid1.row:=stringgrid1.row+1;
label1.caption:=inttostr(x);
edit2.text:=stringgrid1.Cells[1,x];
edit1.text:=stringgrid1.Cells[2,x];
edit3.text:=stringgrid1.Cells[3,x];
edit4.text:=stringgrid1.Cells[4,x];
edit5.text:=stringgrid1.Cells[5,x];
exit;
end
else
showmessage('已到第一條記錄!');
end;

四、'上一條記錄',完成記錄上移,代碼如下:
var x:integer;
begin
x:=stringgrid1.row-1;
if x<>0 then
begin
stringgrid1.row:=stringgrid1.row-1;
label1.caption:=inttostr(x);
edit2.text:=stringgrid1.Cells[1,x];
edit1.text:=stringgrid1.Cells[2,x];
edit3.text:=stringgrid1.Cells[3,x];
edit4.text:=stringgrid1.Cells[4,x];
edit5.text:=stringgrid1.Cells[5,x];
exit;
end
else
showmessage('已到最后一條記錄!');
end;


五、stringgrid中上下移動時代碼:
procedure TForm1.StringGrid1Click(Sender: TObject);
var i:integer;
begin
i:=stringgrid1.Row;
label1.caption:=inttostr(i);
edit1.text:=stringgrid1.Cells[2,i];
edit2.text:=stringgrid1.Cells[1,i];
edit3.text:=stringgrid1.Cells[3,i];
edit4.text:=stringgrid1.Cells[4,i];
edit5.text:=stringgrid1.Cells[5,i];
end;


六、運行程序,點擊按鈕1打開excel表格。程序?qū)覧XCEL,并打開了選擇的電子表格,這時請不要關(guān)閉EXCEL,當(dāng)程序從EXCEL取數(shù)完畢將自動關(guān)閉EXCEL程序,應(yīng)用程序取出了EXCEL數(shù)據(jù),顯示在stringgrid中,并將第一筆數(shù)據(jù)各字段的值賦給了左邊的對應(yīng)的edit字段。點擊按鈕2、按鈕3可以查看下一條或上一條記錄。也可以使用光標(biāo)在stringgrid1上移動。


同時我們也可以對stringgrid中的記錄進行查詢、定位。相反,也可以將數(shù)據(jù)庫中的數(shù)據(jù)輸入到EXCEL中??傊?,只要我們從EXCEL提取出數(shù)據(jù),并保存到stringgrid中,我們就可以進行相應(yīng)操作,如統(tǒng)計、查詢、重新輸出,使平進的EXCEL電子表格中的數(shù)據(jù)在應(yīng)用程序中得到利用。

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約