资料一。

从数据库导数据至excel 容易,但要是想把excel 里的数据通过delphi写的程式导入数据库就没那么简单了,并且网上讲的都不全面,没有几个完整可行的方案,以下是我收集的个人认为比较可行的方案,不敢独享,故拿出来供大家学习参考之用!

procedure load(rowCount,colCount:integer; fileName:String; var grid:TStringGrid);
//
Excel中读取数据到 Grid
var
   v:variant;
   i,j:integer;
begin
   grid.RowCount:=rowCount;
   grid.ColCount:=colCount;
   v:=createoleobject('Excel.Application');//
创建OLE对象
   try
     form2.show;
     form2.ProgressBar1.Position := 0;
     form2.ProgressBar1.Max :=   65535;
     V.workBooks.Open(fileName);
     //for i:=1 to rowCount do
     for i:=1 to 65535 do
       for j:=1 to colCount do
         //if grid.Cells[j-1,i-1] = '' then break;
         form2.ProgressBar1.Position := i;
         grid.Cells[j-1,i-1]:=v.workbooks[1].sheets[1].cells[i,j];
     v.workbooks[1].close;
   finally
     v.quit;
     form2.close;
   end
end;
{
procedure save(tableName:String;grid:TStringGrid);
//
Grid 中的数据保存到 SQL Server 数据表中
var
   valuesStr:string;
   i,j:integer;
begin
   if not CreateTable(tableName,grid.ColCount) then
   begin
     showmessage('Error On CreateTable');
     exit;
   end;
   for i:=1 to grid.RowCount-1 do
   begin
     valuesStr:=inttostr(i)+',';
     for j:=0 to grid.ColCount-1 do
       valuesStr:=valuesStr+Grid.Cells[j,i]+',';
     if not insertone(tableName,valuesStr) then
     begin
       showmessage('Error On Row('+inttostr(i)+')');
       exit;
     end;
   end;
   showmessage('
数据导入成功');
end;

function insertone(const tableName, ValuesStr: string): boolean;
//
插入一条记录
var
   tmpstr,s:string;
   p:integer;
begin
   result:=true;
   tmpstr:=ValuesStr;
   with query1 do
   begin
     close;
     sql.Clear;
     sql.Add('insert into '+tableName+' values(');
     s:='';
     while tmpstr<>'' do
     begin
       p:=pos(',',tmpstr);
       s:=s+''''+copy(tmpstr,1,p-1)+''',';
       system.Delete(tmpstr,1,p);
     end;
     s:=copy(s,1,length(s)-1);
     sql.Add(s);
     sql.Add(')');
     try
       execsql;
     except
       result:=false;
     end;
   end;
end;


function CreateTable(const tableName:String; aFieldCount: integer): boolean;
//
创建表
var
   tmpstr:string;
   i:integer;
begin
   result:=true;
   tmpstr:='if exists (select * from sysobjects where Name='''
     +tableName+''') drop table '+tableName+' create table '+tableName+'(';
   for i:=1 to aFieldCount do
     tmpstr:=tmpstr+'F'+inttostr(i)+' varchar(50),';
   delete(tmpstr,length(tmpstr),1);
   tmpstr:=tmpstr+')';
   with query1 do
   begin
     close;
     sql.Clear;
     sql.Add(tmpstr);
     try
       execsql;
     except
       result:=false;
     end;
   end;
end;
   }

资料二:

delphi excel导入delphi

20080222 星期五 23:52

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

一、 新建一项目,从控件栏servers中分别选取控件:excelapplicationexcelworkbook1excelworksheet, 放到主窗体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文件导入StringGridext:=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-->

 

posted on 2008-10-14 23:37  漂流侠  阅读(875)  评论(0编辑  收藏  举报