我现在需要把EXCEL的数据存入到数据库里,在存入前需要显示EXCEL的内容,并且设置哪些数据允许保存,但是我现在没有思路,对EXCEL的操作以前没有作过,哪位大侠能够帮助我解决,最后提供原码,谢谢。

解决方案 »

  1.   

    --------------------------------------
    //导入excel表到db
    ---------------------------------------}
    procedure TFrm_ExMend.ExportReport(FileName: String);
    var
      SheetName:string;
      RowsCounts,k:integer;
      Repi,Repm:integer;
    begin
    //  Fexcel := CreateOleObject( 'Excel.Application' );
      if not VarIsEmpty(Fexcel) then  Fexcel.application.quit;
      if not Checkexcel then Abort;  if  FileName <> ''  then
      begin
        DB_Link.ADOC_DBLink.BeginTrans ;
        tempQry:=Tadoquery.Create(nil);  //创建查询assest表组件
        tempqry.Connection:= DB_Link.ADOC_DBLink;
        db_link.ADOC_DBLink.Connected:=true;
        tempqry.Close;
        tempqry.SQL.Clear;
        tempqry.SQL.Add('select * from dcb_tempasset where 1=2');
        try
          tempqry.Open;
        except
        on E:exception do
          begin
            DB_Link.ADOC_DBLink.RollbackTrans  ;
            ShowMessage(e.Message);
          end;
        end;    try
            Fexcel.application.workbooks.open(FileName);
            //ReadSheetName;
            SheetName:= Fexcel.Application.Worksheets.Item[1].Name;
            //得到工作表的行数和列数
            RowsCounts:=Fexcel.Application.Worksheets[SheetName].UsedRange.Rows.Count;
            //鼠标变化
            Screen.Cursor:=crHourGlass;
              //将Excel表的值传输到DB中
              for Repi:=2 to RowsCounts do//行
              IF length(Fexcel.application.Worksheets[SheetName].cells[Repi,1].value)> 0  THEN
              begin
                tempqry.Insert;
                TempQry.FieldByName('Asset').AsString  :=Fexcel.application.Worksheets[SheetName].cells[Repi,1].value;
                TempQry.FieldByName('Passet').AsString :=Fexcel.application.Worksheets[SheetName].cells[Repi,2].value;            TempQry.FieldByName('Base_area').AsString  :=Fexcel.application.Worksheets[SheetName].cells[Repi,3].value;
                TempQry.FieldByName('Base_Name').AsString :=Fexcel.application.Worksheets[SheetName].cells[Repi,4].value;
                TempQry.FieldByName('Property').AsString :=Fexcel.application.Worksheets[SheetName].cells[Repi,5].value;            TempQry.FieldByName('Asset_origin').AsString :=Fexcel.application.Worksheets[SheetName].cells[Repi,6].value;
                TempQry.FieldByName('description').AsString :=Fexcel.application.Worksheets[SheetName].cells[Repi,7].value;     //设备状态
                TempQry.FieldByName('Barcode_id').AsString :=Fexcel.application.Worksheets[SheetName].cells[Repi,8].value;     //设备来源
                TempQry.FieldByName('Manufacturer').AsString :=Fexcel.application.Worksheets[SheetName].cells[Repi,9].value;            TempQry.FieldByName('Supplier').AsString :=Fexcel.application.Worksheets[SheetName].cells[Repi,10].value;
                TempQry.FieldByName('ModelNumber').AsString :=Fexcel.application.Worksheets[SheetName].cells[Repi,11].value;
                TempQry.FieldByName('Serial_code').AsString :=Fexcel.application.Worksheets[SheetName].cells[Repi,12].value;
                try
                  TempQry.FieldByName('DatePurchased').AsString :=Fexcel.application.Worksheets[SheetName].cells[Repi,13].value;            except
                  DB_Link.ADOC_DBLink.RollbackTrans  ;
                  Application.MessageBox('采购日期有错,请确认!','系统提示');
                end;            try
                  tempqry.FieldByName('DateWarrantyExpire').AsString :=Fexcel.application.Worksheets[SheetName].cells[Repi,14].value;
                except
                  DB_Link.ADOC_DBLink.RollbackTrans  ;
                  Application.MessageBox('保修日期有错,请确认!','系统提示');
                end;
                tempqry.FieldByName('CostInstall').AsString :=Fexcel.application.Worksheets[SheetName].cells[Repi,15].value;            try
                  tempqry.FieldByName('beginusedatetim').AsString :=Fexcel.application.Worksheets[SheetName].cells[Repi,16].value;
                except
                  DB_Link.ADOC_DBLink.RollbackTrans  ;
                  Application.MessageBox('开始使用日期有错,请确认!','系统提示');
                end;
                try
                  tempqry.FieldByName('scrapdatetime').AsString :=Fexcel.application.Worksheets[SheetName].cells[Repi,17].value;
                except
                  DB_Link.ADOC_DBLink.RollbackTrans  ;
                  Application.MessageBox('报废日期有错,请确认!','系统提示');
                end;
                tempqry.FieldByName('untread').AsString :=Fexcel.application.Worksheets[SheetName].cells[Repi,18].value;
                tempqry.FieldByName('mend').AsString :=Fexcel.application.Worksheets[SheetName].cells[Repi,19].value;
                tempqry.FieldByName('validate').AsString :=Fexcel.application.Worksheets[SheetName].cells[Repi,20].value;
                tempqry.FieldByName('memo').AsString :=Fexcel.application.Worksheets[SheetName].cells[Repi,21].value;            //ADOQ_inputexcel.FieldByName('validate').AsString :='否';            //是否验收
                tempqry.post;
             //   DataS_list.DataSet := tempqry;
            //    cxGrid1DBTableView1.DataController.DataSource := DataS_list;
            //    cxGrid1DBTableView1.CreateColumn;
                Repm:=1;
              end
              else begin
                Repm:=Repm+1 ;                        //如果EXCEL末尾空格数大于10就退出
                if Repm > 10 then
                  break;
              end;
         DB_Link.ADOC_DBLink.CommitTrans ;
         ADOQ_list.Close;
         ADOQ_list.Open;
      finally
          tempqry.close;
          tempqry.Free;
          Screen.Cursor:=crarrow;
          Fexcel.application.workbooks.close;
          if not VarIsEmpty(Fexcel) then
             Fexcel.application.quit;
             Fexcel := unassigned;
        end;
      end;
    end;