回 帖 发 新 帖 刷新版面

主题:Delphi数据如何右键发送到EXCEL表里

我用Delphi7写了一个小程序,我能查出数据,但不知道如何让查出来的数据右键发送到EXCEL表里,哪位大虾帮帮忙,告诉俺如何写可以做到右键发送!

回复列表 (共1个回复)

沙发

procedure DeriveToExcel(filename,Title:String;DBGrideh:TDBGrideh;Total:Boolean);
var//filename:文件名;title:标题; total:是否显示合计
    ExcelApp,WorkBook:Variant;
    i,j:Integer;
    Row,Col:Integer;
    FieldName:string;
    DataSet:TDataSet;
    S:String;
begin
   //数据发送到Excel
    try
     ExcelApp   :=   CreateOleObject('Excel.Application');
     WorkBook   :=   CreateOleObject('Excel.Sheet');
    except
     Application.MessageBox('你的机器里未安装Microsoft Excel.','',32);
     Exit;
    end;
    Application.ProcessMessages;
    WorkBook:=ExcelApp.WorkBooks.Add;
    Col:=1;
    ExcelApp.Cells(2,5):=Title;
    Row:=6;
    DataSet:=DBGrideh.DataSource.DataSet;
    for I:=0 to DBGrideh.Columns.Count-1   do
    begin
       if DBGrideh.Columns[I].Visible then
        begin
          FieldName:=DBGrideh.Columns[I].Title.Caption;
          ExcelApp.Cells(Row,Col):=FieldName;
          Col:=Col+1;
        end;
    end;
    Row:=Row+1;
    DataSet.First;
    while  not  DataSet.Eof   do
    begin
      Col:=1;
      for J:=0 to DBGrideh.Columns.Count-1 do
      begin
        FieldName:=DBGrideh.Columns[J].FieldName;
        ExcelApp.Cells(Row,Col):=' '+DataSet.FieldByName(FieldName).AsString+' ';
        Col:=Col+1;
      end;
        Row:=Row+1;
        DataSet.Next;
    end;
    if Total then
    begin
       Col:=1;
       for J:=0 to DBGrideh.Columns.Count-1 do
       begin
         S:=Char(64+((J+1) mod 26));
         if (J+1)>26 then
         begin
           S:=Char(65+(((J+1)-26)div 26))+S;
         end;
         if J=0 then
          begin
           ExcelApp.Cells(Row,Col):= '合计';
          end
          else if   DBGrideh.Columns[J].Field.DataType   in   [ftInteger,   ftSmallint,ftFloat,ftBCD]   then
           begin
               FieldName:=DBGrideh.Columns[J].FieldName;
               ExcelApp.Cells(Row,Col):='=SUM('+S+'4:'+S+IntToStr(Row-1)+')';
           end;
          Col:=Col+1;
        end;
    end;
    ExcelApp.Visible   :=   True;
    WorkBook.SaveAs(filename);
    WorkBook.Close;
    ExcelApp.Quit;
    ExcelApp:=Unassigned;
end;

我来回复

您尚未登录,请登录后再回复。点此登录或注册