老板给的题目:
如何把数据表里的某些数据项用Word导出来?;用途:要打印检测报告,有些地方像送检单位、仪器名称、编号之类的在数据表里就有的就直接导出到Word模板里,然后打印谁能指点一下啊?
谢谢:)

解决方案 »

  1.   

    为什么要用Word,可以用Excel吗?
    如果你要用Excel 的话,就相对简单了,
    你可以用OWC组件,那里操作电子表格很简单,当然把数据库中的数据
    导入到TSpreadSheet组件中,也同样简单,生成报表导入到电子表格打印
    就容易了;
      

  2.   

    搜索一下这里,关键字'ole',能找到你要的东西
      

  3.   

    建议使用EXECL,看的出你在检验机构做事,我们单位现在就在用一套检验报告处理系统,用EXECL,在EXECL里作一个模板,模板上用数据中不可能出现的东东作标记,找到这个标记后把相应数据填进去,,这样只要改变标记的位置就能自定义模板。
      

  4.   

    呢, 可要给我  90 分哟!!!!,  刚刚好我也在做delphi+word+web报表问题,看看我们用的例程,应该可以了!
    procedure TForm1.Button1Click(Sender: TObject);
    var
      templateName: OleVariant;
      newTemplate: OleVariant;
      ItemIndex: OleVariant;
      vSelection: Selection;
      vBookMark: BookMark;
      vTable: Table;
      I : Integer;
    begin
      //构造打印模板文件名全路径
      case ListBox1.ItemIndex of
       0:
         begin
          templateName := folderName + 'cld.dot';
         end;
       1:
         begin
          templateName := folderName + 'cljg.dot';
         end;
      end;  newTemplate := False;  try
        wordApp.Connect();
      except
        MessageDlg('您的计算机上还未安装Microsoft Office Word97或更高的版本!', mtError, [mbOK], 0);
        Abort;
      end;  //以指定的模板文件创建新Word文档
      wordApp.Documents.AddOld(templateName, newTemplate);
      wordApp.Caption := ListBox1.Items.Strings[ListBox1.ItemIndex];  vSelection := wordApp.Selection;  case ListBox1.ItemIndex of
       0:
         begin
           //取文档中的第1张表
           vTable := wordApp.ActiveDocument.Tables.Item(1);       vTable.Cell(1, 2).Range.Text := '李鑫 于 北京供电公司';
           vTable.Cell(1, 4).Range.Text := '昆明锐祺软件';
           vTable.Cell(2, 2).Range.Text := '昆明市白塔路延长线387号星耀大厦1102B座 李鑫(收) 650051 (0871)3126628';
           vTable.Cell(3, 2).Range.Text := DateToStr(Now);
           vTable.Cell(3, 4).Range.Text := DateToStr(Now);       vTable.Cell(5, 1).Range.Text := '哈哈哈哈哈哈,我也不知道了。';
           vTable.Cell(7, 1).Range.Text := '不知道了';
           vTable.Cell(9, 2).Range.Text := '不知道';
         end;
       1:
         begin
           ItemIndex := 1;
         end;
      end;  for I := 1 to wordApp.ActiveDocument.Books.Count do
        begin
          ItemIndex := I;
          //取指定Index的书签
          vBookMark := wordApp.ActiveDocument.Books.Item(ItemIndex);
          //ShowMessage(vBookMark.Name+IntToStr(wordApp.ActiveDocument.Books.Count)+'Now='+IntToStr(i));
          if LowerCase(vBookMark.Name) = 'nd' then
             begin
               vBookMark.Select();
               vSelection.InsertAfter('2003');
             end;      if LowerCase(vBookMark.Name) = 'bh' then
            begin
              vBookMark.Select();
              vSelection.InsertAfter('1');
            end;
        end;
        
      wordApp.Visible := CheckBox1.Checked;
      wordApp.UserName := '李鑫';
      //先最小化
      wordApp.WindowState := 2;
      //再最大化
      wordApp.WindowState := 1;
      //打印预览
      wordApp.PrintPreview := CheckBox2.Checked;  //立即打印
      if CheckBox3.Checked then
         wordApp.PrintOutOld;  wordApp.Disconnect();
    end;procedure TForm1.FormCreate(Sender: TObject);
    var
      templateName: String;
      fileStream: TResourceStream;
    begin
      //获得应用程序所在的目录
      folderName := ExtractFilePath(Application.ExeName);  if not FileExists(folderName+'template.res') then
         begin
           MessageDlg('错误:找不到模板资源文件template.res!', mtError, [mbOK], 0);
           Close;
         end;  //构造WORD打印模板的文件名全路径
      templateName := folderName + 'cld.dot';
      //获得资源文件里的“处理单”打印模板
      fileStream := TResourceStream.Create(hInstance, 'cld', 'WORD');  if not FileExists(templateName) then
        begin
          //拆离文件
          fileStream.SaveToFile(templateName);
          //释放
          fileStream.Free;
        end;  //构造WORD打印模板的文件名全路径
      templateName := folderName + 'cljg.dot';
      //获得资源文件里的“处理结果”打印模板
      fileStream := TResourceStream.Create(hInstance, 'cljg', 'WORD');
      if not FileExists(templateName) then
        begin
          //拆离文件
          fileStream.SaveToFile(templateName);
          //释放
          fileStream.Free;
        end;  ListBox1.Items.Add('处理单');
      ListBox1.Items.Add('处理结果');
      ListBox1.Selected[0] := True;
    end;end.
      

  5.   


    if you want use table in word,u must declare a table in word.
    just like this.vTable: Table;//declare the table u want use in Word;WordApplication.activedocument.tables.add(wordappliction.activedocument.rang,row_count,Col_count,,)// you can get the rest two parameters when u type the left quote  in Delphi Unit during ur promgraming!then u use the table to add the data from database to word document;just like this:
    vTable:=wordApplication.activedocument.tables.items(0) ;
    vTable.cells(row,col).range.text:='string';可要多给我分哟,这是我昨天晚上的心血呀, 一直到晚上12点哟。