Excel文件导入StringGrid

翻译|其它|编辑:郝浩|2006-07-12 14:53:00.000|阅读 1614 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>


EXCEL电子表格作为办公软件OFFICE中的重要组成部份,是日常办公系统的主要助手,因此许多日常所需的业务方面的数据通常是通过电子表格存取。有时我们需要从日常工作中创建的EXCEL中取得数据进行操作、打印、查询,统计等工作。在这里我将介绍如何利用delphi完成EXCEL电子表格中数据的操作。

一、 新建一项目,从控件栏servers中分别选取控件:excelapplication、excelworkbook1、excelworksheet,放到主窗体from1中,并加入stringgrid、三个按钮、五个显示字段内容的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应用程序
          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;//出现异常情况时关闭
            ExcelApplication1.Quit;showmessage('请选择EXCEL电子表格!');
            exit;
        end;
end;
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);//ExcelWorkbook1与Eexcelapplication1建立连接
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);//Excelworksheet1与Excelworkbook1建立连接
//开始从EXCEL中取数,放到stringgrid1中,取完数后关闭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;
        //将第一条数据赋给编辑框
        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表格。程序将启动EXCEL,并打开了选择的电子表格,这时请不要关闭EXCEL,当程序从EXCEL取数完毕将自动关闭EXCEL程序,应用程序取出了EXCEL数据,显示在stringgrid中,并将第一笔数据各字段的值赋给了左边的对应的edit字段。点击按钮2、按钮3可以查看下一条或上一条记录。也可以使用光标在stringgrid1上移动。


同时我们也可以对stringgrid中的记录进行查询、定位。相反,也可以将数据库中的数据输入到EXCEL中。 总之,只要我们从EXCEL提取出数据,并保存到stringgrid中,我们就可以进行相应操作,如统计、查询、重新输出,使平进的EXCEL电子表格中的数据在应用程序中得到利用。

 


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP