目的:Delpho控制Excel
      并将Excel数据、图形保存到sql.text形态字段
请附代码或提供网址学习
谢谢

解决方案 »

  1.   

    uses :comobj,db;
    Procedure  TForm1.Excel_2_db(str :string);
    var          eclApp,WorkBook :variant ;
                 xlsFileName :string;
                 a_FiledCount:integer;       //数据库表中的列数
                 b_filedCount:integer;      //excel 文件中的 列数
                 b_row :integer;           // excel 文件的行熟
                 i,j :integer;
                 a_flag :boolean;
    begin
              Form1.OpenDialog1.Title :='Excel文件 导入到数据库'+str+'表';
              Form1.OpenDialog1.InitialDir :=ExtractFilePath(Application.ExeName );
              if    (Form1.OpenDialog1.Execute )
              then   xlsFileName :=ExtractFileName(Form1.OpenDialog1.FileName)
              else   exit;          try
                   eclApp := CreateOleObject('Excel.Application');
                   WorkBook :=CreateOleObject('Excel.Sheet');
              except
                     showmessage('您系统未安装MS-EXCEL');
                     exit;
              end;
              try
                  workBook :=eclApp.WorkBooks.add ;
                  eclApp.workBooks.open(Form1.OpenDialog1.FileName );
              except
                    on  EOleException do
                        begin
                                 WorkBook.close;
                                 eclApp.quit;
                                 eclApp:=Unassigned;
                                 exit;
                        end;
              end;
             eclApp.visible :=false;
              try   //try ..finally
                try //try  ..except
                     With  Data_Mod.DataModule1.kcinfo_Tab    do
                        begin
                             close ;
                             TableName :=str;
                             active :=true;
                             a_FiledCount :=FieldCount;                   end;                   b_filedCount :=eclApp.ActiveSheet.UsedRange.columns.Count;//返回excel 表中的列数
                       b_row :=eclApp.activesheet.UsedRange.rows.count;        //返回excel 表中的行数                   if    (a_FiledCount <>b_FiledCount)     //当数据 表和导入的excel表中的列数不一样,说明导入的excel文件不是正确的
                       then  begin
                              showmessage('您选择导入的excel文件错误'+#13+#10+'请您重新选择');
                              WorkBook.close;
                              eclApp.quit;
                              eclApp:=Unassigned;
                              exit;
                             end
                       else   begin  //列数正确,但是还要继续判断每列的字段名是否一致
                                 for  i :=1 to  b_filedCount do
                                    begin
                                         //showmessage(eclApp.activesheet.cells.item[1,i].value);
                                         //showmessage(DataMod.ADO_basic.Fields.Fields[i-1].FieldName );                                     if    eclApp.activesheet.cells.item[2,i].value<>Data_Mod.DataModule1.kcinfo_Tab.Fields[i-1].FieldName  //判断字段名是否相等
                                         //if    eclApp.activesheet.cells.item[1,i].value<>DataMod.ADO_basic.Fields.Fields[i-1].FieldName   //判断中文title.caption 是否相等
                                         then  begin
                                                       showmessage('您选择导入的excel文件错误'+#13+#10+'请您重新选择');
                                                       WorkBook.close;
                                                       eclApp.quit;
                                                       eclApp:=Unassigned;
                                                       Data_Mod.DataModule1.kcinfo_Tab.Close ;
                                                       exit;
                                               end;
                                    end; //for   i:=.....  
                            end;     //end with  else                  for  i :=3 to  b_row do            //行
                          begin
                                a_flag :=Data_Mod.DataModule1.kcinfo_Tab.Locate(eclApp.activesheet.cells.item[2,1],eclApp.activesheet.cells.item[i,1],[loCaseInsensitive]);
                                          if    (a_flag =true)
                                          then  begin
                                                   showmessage('该记录已经存在');
                                                   Data_Mod.DataModule1.kcinfo_Tab.Next ;
                                                   continue;
                                                end;                            With  Data_Mod.DataModule1.kcinfo_Tab  do
                                   begin
                                         close ;
                                         TableName :=str;
                                         active :=true;
                                         Append;
                                   end;
                                For j :=1 to    b_filedCount do    //列
                                   begin     //开始导入数据库
                                         //showmessage(eclApp.activesheet.cells.item[1,j]);//.Value);
                                         ////showmessage(eclApp.activesheet.cells.item[i,j].value);
                                         //showmessage(eclApp.activesheet.cells[i,j].value);                                     Data_Mod.DataModule1.kcinfo_Tab.FieldByName(eclApp.activesheet.cells.item[2,j]).Value :=eclApp.activesheet.cells[i,j].value;                               end ;      //end with For j :=1 to    b_filedCount do
                             Data_Mod.DataModule1.kcinfo_Tab.Post ;
                             Data_Mod.DataModule1.kcinfo_Tab.Refresh ;
                      end;
                     showmessage('导入数据成功');
                except
                       WorkBook.close;
                       eclApp.quit;
                       eclApp:=Unassigned;
                       Data_Mod.DataModule1.kcinfo_Tab.Close ;
                end;   //end  try  except
              finally  //操作错误,退出
                       WorkBook.close;
                       eclApp.quit;
                       eclApp:=Unassigned;
                       Data_Mod.DataModule1.kcinfo_Tab.Close ;
              end;
    end;