修改的一个导出DataSet到xls的单元[3]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 cornermoss 的 blog

; finally
    try
      (wkbookres.sheets[1] as _worksheet).activate(lcid_res);
      wkbookres.close(not(bdontsave) ,path+'\'+filename,emptyparam,lcid_res);
      xlsappres.quit;
      xlsappres.disconnect;
    finally
      //杀死未关闭的excel进程
      xlsapphwnd := findwindow( nil,_xlsrescaption );
      if xlsapphwnd<>0 then sendmessage( xlsapphwnd, wm_close, 0, 0);
    end;
    try
      //wkbooktmp.close(false ,path+'\'+tmpflag+inttostr(i)+filename,emptyparam,lcid_tmp);
      xlsapptmp.quit;
      xlsapptmp.disconnect;
    finally
      xlsapphwnd := findwindow( nil,_xlstmpcaption );
      if xlsapphwnd<>0 then sendmessage( xlsapphwnd, wm_close, 0, 0);
        //terminateprocess(xlsapphwnd,0);
    end;
    mcompleted := true;
    screen.cursor := crdefault;
  end;
end;

procedure datasettoxls(ds:tdataset;fname:string);
var c,r:integer;
  xls:txlswriter;
begin
  xls:=txlswriter.create(fname);
  if ds.fieldcount > xls.maxcols then
    xls.maxcols:=ds.fieldcount+1;
  try
    xls.writebof;
    xls.writedimension;
    for c:=0 to ds.fieldcount-1 do
      xls.cellstr(0,c,ds.fields[c].displaylabel);
    r:=1;
    ds.first;
    while (not ds.eof) and (r <= xls.maxrows) do begin
      for c:=0 to ds.fieldcount-1 do
        if ds.fields[c].asstring<>'' then
          xls.writefield(r,c,ds.fields[c]);
      inc(r);
      ds.next;
    end;
    xls.writeeof;
  finally
    xls.free;
  end;
end;

procedure dbgridtoxls(grid:tdbgrid;fname:string; bsetfieldname:boolean;callfunc:texportxls_callbackproc;  baskforstop:boolean=true);
var c,r,i :integer;
  xls:txlswriter;
  ntotalcount, ncurrentcount : integer;
  bdontsave:boolean;
begin
  bdontsave := false;
  grid.datasource.dataset.disablecontrols;
  xls:=txlswriter.create(fname);
  if grid.fieldcount > xls.maxcols then
    xls.maxcols:=grid.fieldcount+1;
  try      
    g_xlswriterisruning := true;
    xls.writebof;
    xls.writedimension;
    if bsetfieldname then
    begin
      for c:=0 to grid.fieldcount-1 do
        xls.cellstr(0,c,grid.fields[c].fieldname);
      r :=2;
    end
    else r:=1;
    for c:=0 to grid.fieldcount-1 do
      xls.cellstr(r-1,c,grid.fields[c].displaylabel);
    ntotalcount := grid.datasource.dataset.recordcount;
    ncurrentcount := 0;
    bdontsave := false;
    grid.datasource.dataset.first;
    for i:=0 to ntotalcount-1 do
    begin
      application.processmessages;
      if r > xls.maxrows then raise exception.create('导出的数据超过'+inttostr(xls.maxrows)+'条记录,操作失败!');
      inc(ncurrentcount);
      callfunc(ncurrentcount/ntotalcount);
      if g_usercmd=userstop then
      begin
        if baskforstop then
        case application.messagebox('您停止了导出数据,请问需要保存吗?(选择“取消”继续导出)','询问',mb_yesnocancel) of
          idyes: break;
          idno: begin
                  bdontsave := true;
                  raise exception.create('用户停止,导出数据未保存!');
     

本文关键:修改的一个导出DataSet到xls的单元
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top