我用delphi7查询出一个报表,用DBGrid显示出来,我想把此表的内容保存为Excel的格式,怎么才能导出去啊???
 谢谢!!!!!!!非常谢谢大哥大姐们指点一下啊!!!!

解决方案 »

  1.   

    if not SaveDialog1.Execute then
        exit;  if uppercase(copy(SaveDialog1.FileName,length(SaveDialog1.FileName)-3,4))<>'.XLS' then
          SaveDialog1.FileName:=SaveDialog1.FileName+'.xls';
      path:=SaveDialog1.FileName;
      if fileexists(path) then
      begin
        showmessage('文件已存在,请选择另外的文件名!');
        exit;
      end;  formatPath:=ExtractFileDir(Application.ExeName)+'\format.xls';
      if not fileexists(formatPath) then
      begin
        showmessage('模板文件不存在!');
        exit;
      end;
      try
        copyfile(pchar(formatPath),pchar(path),true);
      except
        Showmessage('创建文件时出错');
        exit;
      end;
      rows:=ListView1.Items.Count;  try
        excel:=createoleobject('excel.application');
        excel.visible:=false;
        excel.DisplayAlerts:=False;
      except
        showmessage('打开Excel出错!');
        exit;
      end;
      try
        excel.workbooks.Open(path);
        book:=excel.workbooks[1];
        sheet:=book.worksheets[1];    rows:=rows-1;
        for i:=0 to rows do
        begin
          item:=ListView1.Items[i];
          sheet.cells.item[i+7,2]:=item.Caption;
          sheet.cells.item[i+7,3]:=item.SubItems[0];
          sheet.cells.item[i+7,4]:=item.SubItems[1];
          sheet.cells.item[i+7,5]:=item.SubItems[2];
          sheet.cells.item[i+7,6]:=item.SubItems[3];
          sheet.cells.item[i+7,7]:=item.SubItems[4];
          sheet.cells.item[i+7,9]:=item.SubItems[5];
        end;
        sheet.Cells.Columns.AutoFit;    book.save;    ListView1.Items.Clear;
      except
        ShowMessage('保存出错!');
      end;
      Showmessage('保存成功!');
      excel.quit;
      

  2.   

    下载一个 dbgrideh控件
    USES DbGridEhImpExp;
    if dbgrideh1 <> nil then
       begin
        if saveDialog1.Execute then
        begin
         SaveDBGridEhToExportFile(TDBGridEhExportAsXLS,dbgrideh1,savedialog1.filename+'.xls',true);
         showmessage('成功导出到'+ savedialog1.filename+'.xls');
        end;
       end
     else
       showmessage('没有数据表格可供导出') ;