将DBGrid中个别列的数据(当然也可以是数据库中的数据)导入到excel中,每五条记录做一页,如何解决这个问题呢?
请教高手,最好能给予参考代码,谢谢!

解决方案 »

  1.   

    procedure TForm1.Button1Click(Sender: TObject);
       var i,k,j:integer;
       ExcelID: Variant;
       pri:integer;
       name1,name2,num,date:string;
       str,id,style,styleid:string;
    begin
    //启动EXCEL ,选择EXCEL表,获得行数
       if opendialog1.Execute then begin
          ExcelID:=CreateOleObject('Excel.Application');
          ExcelID.workbooks.open(opendialog1.FileName);
          ExcelID.WorkSheets[1].Activate;
          i:=ExcelID.worksheets[1].UsedRange.Rows.Count;
    //取得插入的主键 ,应该先把表的主键设置为非标识
           with adoquery2 do
               begin
                 close;
                 sql.Clear;
                 sql.Add('select max(QPL_ID) from qsproductlist');
                 open;
               end;
           pri:=adoquery2.Fields[0].AsInteger;
           pri:=pri+1;
    //循环插入数据
          for k:=1 to i do begin
    //解析要插入的字段
             name1:=ExcelID.Cells[k,1].Value;
             name2:=ExcelID.Cells[k,2].Value;
             num:=ExcelID.Cells[k,4].Value;
             date:=ExcelID.Cells[k,5].Value;
            { str:='select * from producttype';
             with adoquery2 do
               begin
                 close;
                 sql.Clear;
                 sql.Add(str);
                 open;
               end;
    //翻译外键
             for j:=0 to  adoquery2.RecordCount -1 do
                 begin
                  style:=adoquery2.Fields[1].AsString;
                  styleid:=adoquery2.Fields[0].AsString;
                  if pos(style,name2) > 0 then
                     id:=styleid;
                  adoquery2.Next;
                 end; }
                 id:='14';
    //判断是否已经有该QS号码
                 with adoquery2 do
                 begin
                  close;
                  sql.Clear;
                  sql.Add('select QPL_ID from qsproductlist where OPL_ExequaturNumber='''+num+'''');
                  open;
                 end;
                 if adoquery2.RecordCount <=0 then
                 begin
    //插入数据
                   adoquery1.Append;
                   adoquery1.Fields[0].Asinteger:=pri;
                   adoquery1.Fields[1].AsString:= id;
                   adoquery1.Fields[2].AsString:= name1;
                   adoquery1.Fields[3].AsString:= name2;
                   adoquery1.Fields[5].AsString:= num;
                   adoquery1.Fields[6].AsString:=date;
                   adoquery1.Post;
                   pri:=pri+1;
                end;
             end;
          end;
          adoquery1.Close;
          ExcelID.quit;
          showmessage('ok');
    end;