如何用delphi把sqlserver的数据导出到word或excel中,并要求直接定制字体、字号、位置等!请把基本的语句格式写一下好吗?现在我手头资料有限,请大家把基本的语句写给我好吗?谢谢

解决方案 »

  1.   

    use comobjprocedure TSCZNPC_Form.Excel_ButtonClick(Sender: TObject);
    var
        xlsFilename :string;
        eclApp,WorkBook :variant ;
        a_filedNo,i,j :integer;
        fs : string;
        p1    : array[0..300] of Char;
    begin
        a_filedNo := DBGrid1.FieldCount ;    try
          eclApp :=CreateOleObject('Excel.Application');
          WorkBook :=CreateOleObject('Excel.Sheet');
        except
          Application.MessageBox('您的系统没有安装MicroSoft EXCEL软件,数据不能导出!','错误提示',MB_OK+MB_SystemModal+MB_IconStop);
          exit;
        end;    if SaveDialog1.Execute then
          xlsFileName := SaveDialog1.FileName
        else
          exit;    try
          WorkBook :=eclApp.workBooks.add ;
          for i :=1 to  a_FiledNo do      //转化字段名;
          begin
            eclApp.cells.item(1,i) := DBGrid1.Fields[i-1].FieldName ;
          end;      DBGrid1.DataSource.DataSet.First ;
          for i :=1 to DBGrid1.DataSource.DataSet.RecordCount do
          begin
            for j :=1 to  a_filedNo do  //转化一个记录
              begin
                eclApp.cells.item(i+1,j) :=DbGrid1.Fields[j-1].AsString ;
              end;
            DBGrid1.DataSource.DataSet.Next ;
          end;
          try
            fs := xlsFileName;
            StrPCopy(p1,'文件"' + ExtractFilePath(Application.ExeName )+xlsFileName + '"已存在,'+#13+'是否覆盖?');
            if  FileExists(fs) then
            begin
              if Application.MessageBox(p1,'确认',MB_ICONQUESTION+MB_YESNO)=IDYES then
                DeleteFile(fs)
              else
                exit;
            end;
            WorkBook.saveas(fs);
            WorkBook.close;
            StrPCopy(p1,'EXECL文件已经保存,路径为:' + fs);
            Application.MessageBox(p1,'提示',MB_OK+MB_SystemModal+MB_IconInformation);
          except
            Application.MessageBox('保存文件出现异常!','错误提示',MB_OK+MB_SystemModal+MB_IconStop);
          end;
        except
          Application.MessageBox('不能正确操作EXECL文件,可能该文件已经被其他程序占用或系统错误!','错误提示',MB_OK+MB_SystemModal+MB_IconStop);
          WorkBook.close;
          eclApp.quit;
          eclApp := Unassigned;
        end;end;
      

  2.   

    你看看这里,最好的,快呀,。。
    http://www.csdn.net/develop/article/18/18623.shtm
      

  3.   

    eclApp :=CreateOleObject('Excel.Application');
          WorkBook :=CreateOleObject('Excel.Sheet');
    为什么CreateOleObject这个出错
      

  4.   

    因为 你在uses 单元中没有声明:
    uses :comobj;