单元接口部分引用 comobj 单元(uses ) procedure TForm1.Button1Click(Sender: TObject); var excelx,excely : string; begin try ExcelApp := CreateOleObject('Excel.Application'); WorkBook := ExcelApp.WorkBooks.Open(opendialog.FileName);//使用opendialog对话框指定 //excel档路径 ExcelApp.Visible := false; ExcelRowCount := WorkBook.WorkSheets[1].UsedRange.Rows.Count; for i := 1 to excelrowcount 1 do begin excelx := excelapp.Cells[i,1].Value; excely := excelapp.Cells[i,2].Value; if ((excelapp.Cells[i,1].Value = '') and (ExcelApp.Cells[i,2].Value = '')) then //指定excel档的第 i 行 ,第 1,2(看情况而定)行如果为空就退出,这样的设定,最好是你的档案力这两行//对应数据库中不能为空的数据 exit else with query1 do begin close; sql.clear; sql.add(insert into test(name,address) values(:name,:address)); parambyname('name').asstring := excelx;//excel档的第一列插入到test表的 name栏位; parambyname('address').asstring := excely;//excel档的第二列插入到test表的 address 栏位; execsql; end; end; finally WorkBook.Close; ExcelApp.Quit; ExcelApp := Unassigned; WorkBook := Unassigned; end; end; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 用CreateOleObject( 'Excel.Application' );打开EXCEL文件,读入想要的单元格内容,再通过数据库控件写入数据库!看看下面代码: function import_abclib_from_excel(Gau:TGauge;var emsg:string;var errorrow:integer;total,stnum:integer):integer; var col:integer; temp:string; item_input:array[1..MAX_ABCLIB_COLS]of string; libno:longint; libtype:integer; libname,libfirst,liblast,liblist:string; begin emsg:=''; result:=0; ExcelApp.WorkSheets[2].Activate; while true do begin // 读取一行记录 for col:=1 to MAX_ABCLIB_COLS do begin temp:=create_excelrowcol_name(result+2,col); item_input[col]:=trim(get_excel_value(temp)); end; if item_input[ABCLIBNUM_POS]='' then break;//导入完毕 libno:=strtoint(item_input[LIBNO_POS]); libname:=item_input[LIBNAME_POS]; libtype:=0; if item_input[LIBTYPE_POS]='输入' then libtype:=1; libfirst:=item_input[LIBFIRST_POS]; liblast:=item_input[LIBLAST_POS]; liblist:=item_input[LIBLIST_POS]; liblist:=get_liblist(item_input[LIBLIST_POS]); if not insert_abclib(libno,libname,libtype,libfirst,liblast,liblist,emsg) then begin errorrow:=result+2; exit; end; result:=result+1; Gau.Progress:=(result+stnum)*100 div total; end; end; //////////////////////////////////////////////////////////////////////////////////////////// 您正在看的DELPHI是:Excel文件导入StringGrid。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.t 您正在看的DELPHI是:Excel文件导入StringGrid。ext:=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电子表格 中的数据在应用程序中得到利用。 <--EndFragment--> |