Delphi中国  
首页 | 电脑常识 | 业界动态 | Delphi相关 | 最新源码 | 网络文摘 | 常用工具 | 专题 | 会员中心
  当前位置:首页>Delphi相关>参考资料>文章内容

将excel导入到Delphi中
来源:www.delphi86.com 作者:蜗牛 发布时间:2008-07-04  

单元接口部分引用 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];
(阅读次数:

共2页: 上一页 1 [2] 下一页
上一篇:Delphi中各种进制的转换   下一篇:Delphi中实现JPG、BMP图像的数据库存取
[收藏] [推荐] [评论(0条)] [返回顶部] [打印本页] [关闭窗口]  
用户名: 新注册) 密码: 匿名评论
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
 §最新评论
  热点文章
·一个完整身份证效验程序
·Delphi7.0常用函数速查手册
·Delphi游戏开发相关网址
·Delphi 6 程序员代码编写标准指
·一些界面标准规范
·公用函数
·Windows消息大全使用详解
·关于文件操作集锦
·Delphi中资源文件使用详解
·Delphi编译错误信息对照表
·Delphi6开发基础模拟题
·Delphi编程代码规范
  相关文章
·使用Delphi编写聊天程序
·Delphi的简介与例题解析
·系统进程详解
·Delphi进程查杀
·IDE的全面介绍
·正则表达式--17种
·局部函数、局部过程的讲解
·Delphi.NET的简介
·有关于Windows98/2000/xp互访问
·Delphi笔试题
·Dephi快捷健
·Delphi6开发基础模拟题三

Delphi中国
苏ICP备07008953