我看了几个这方面的东西,没有太明白
怎么样把查询到的数据导入到execl模板里
我现在有几个execl模板,表格和标头都已经设定好了,现在就是要把查询到的数据导入到这个模板中
1、我怎么样读取execl模板
2、怎么样把数据放入模板中
如果有这样的例子能给我发一个吗麻烦各位了
[email protected]

解决方案 »

  1.   

    procedure TfrmSearch.bbtnExportClick(Sender: TObject);
    var
      xlsFilename :string;
      ExcelApp,WorkBook : variant ;
      FieldCount : Integer;
      i,j : Integer;
    begin
      bbtnExport.Enabled := False;
      FieldCount := dgridSearchList.Columns.Count;
      try
        ExcelApp := CreateOleObject('Excel.Application');
        WorkBook :=CreateOleObject('Excel.Sheet');
      except
        Application.MessageBox('您的系统没有安装MicroSoft EXCEL软件,数据不能导出!','错误提示',MB_OK+MB_SystemModal+MB_IconStop);
        bbtnExport.Enabled := True;
        exit;
      end;  if SaveDialog.Execute then
      begin
        xlsFilename := SaveDialog.FileName;
        if  FileExists(xlsFilename) then
        begin
          if Application.MessageBox('该文件已经存在,是否覆盖?','确认',MB_ICONQUESTION+MB_YESNO)=IDYES then
            DeleteFile(xlsFilename)
          else
          begin
            bbtnExport.Enabled := True;
            exit;
          end;
        end;
      end
      else
      begin
        bbtnExport.Enabled := True;
        exit;
      end;  try
        try
          WorkBook :=ExcelApp.WorkBooks.Add ;
          for i:=1 to  FieldCount do      //转化字段名;
            ExcelApp.Cells.Item(1,i) := dgridSearchList.Columns[i-1].Title.Caption ;      qrySearch.First;
          for i:=1 to qrySearch.RecordCount do
          begin
            for j:=1 to  FieldCount do  //转化一个记录
              begin
                ExcelApp.Cells.Item(i+1,j) :=dgridSearchList.Fields[j-1].AsString ;
              end;
            qrySearch.Next ;
          end;      try
            WorkBook.saveas(xlsFilename);
            Application.MessageBox('保存完毕!','提示',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);
        end;
      finally
        WorkBook.close;
        ExcelApp.quit;
        ExcelApp := Unassigned;
        bbtnExport.Enabled := True;
      end;
    end;
      

  2.   

    按照楼上的办法,出错
    [Error] Unit1.pas(39): Undeclared identifier: 'CreateOleObject'
      

  3.   

    最佳的方法是用quickreport去預覽並導入excel
      

  4.   

    我按照上面的办法作了,也出现
    [Error] Unit1.pas(39): Undeclared identifier: 'CreateOleObject'
    这个情况,是怎么回事
      

  5.   

    谢谢,各位的帮助,再次问一下,怎么样打开一个我做好的execl模板,然后往里面依次放入查询出的数据
      

  6.   

    try
            ExcelApp:=CreateOleObject('Excel.Application');
        except
            application.Messagebox('无法打开Xls文件,请确认已 经安装EXCEL.','',mb_OK+mb_IconStop);
        end;
        try
            ExcelApp.WorkBooks.Open('E:\EMSToExcel\example2\test.xls');
            ExcelApp.Visible:=true;
            ExcelApp.WorkSheets['原始'].Activate;
      

  7.   

    try
            ExcelApp:=CreateOleObject('Excel.Application');
        except
            application.Messagebox('无法打开Xls文件,请确认已 经安装EXCEL.','',mb_OK+mb_IconStop);
        end;
        try
            ExcelApp.WorkBooks.Open('E:\EMSToExcel\example2\test.xls');
            ExcelApp.Visible:=true;
            ExcelApp.WorkSheets['原始'].Activate;