最近遇到一个问题,如何处理一个表格
这个表格是word或是excel格式的文档
在每一个表格中,都存在一个字段,就是顺序的序号,如果从中间删掉一条记录(就是说,删掉表格中的一行),不知道大家有什么好的想法,还请赐教。如果能够提供源代码参考,愿意加倍给分。

解决方案 »

  1.   

    你查一下ole内嵌的资料,以前用vba控制office系列的实在是很多,word和excel的控制方式也是大同小异的!
    祝你成功!
      

  2.   

    TWordApplication组件
    TExeclApplication组件//Sending Data to Excel
    procedure TForm1.BitBtnToExcelClick(Sender: TObject);
    var 
      WorkBk : _WorkBook; //  Define a WorkBook
      WorkSheet : _WorkSheet; //  Define a WorkSheet
      I, J, R, C : Integer;
      IIndex : OleVariant;
      TabGrid : Variant;
    begin
     if GenericStringGrid.Cells[0,0] <> '' then
        begin
          IIndex := 1;
          R := GenericStringGrid.RowCount;
          C := GenericStringGrid.ColCount;
          // Create the Variant Array
          TabGrid := VarArrayCreate([0,(R - 1),0,(C - 1)],VarOleStr);
          I := 0;
          //  Define the loop for filling in the Variant
          repeat
            for J := 0 to (C - 1) do
              TabGrid[I,J] := GenericStringGrid.Cells[J,I];
            Inc(I,1);
          until
          I > (R - 1);
          // Connect to the server TExcelApplication
          ExcelApp.Connect;
          // Add WorkBooks to the ExcelApplication
          ExcelApp.WorkBooks.Add(xlWBatWorkSheet,0);
          // Select the first WorkBook
          WorkBk := ExcelApp.WorkBooks.Item[IIndex];
          // Define the first WorkSheet
          WorkSheet := WorkBk.WorkSheets.Get_Item(1) as _WorkSheet;
          // Assign the Delphi Variant Matrix to the Variant associated with the WorkSheet
          Worksheet.Range['A1',Worksheet.Cells.Item[R,C]].Value[EmptyParam] := TabGrid;
          // Customise the WorkSheet
          WorkSheet.Name := 'Customers';
          Worksheet.Columns.Font.Bold := True;
          Worksheet.Columns.HorizontalAlignment := xlRight;
          WorkSheet.Columns.ColumnWidth := 14;
          // Customise the first entire Column
          WorkSheet.Range['A' + IntToStr(1),'A' + IntToStr(R)].Font.Color := clBlue;
          WorkSheet.Range['A' + IntToStr(1),'A' + IntToStr(R)].HorizontalAlignment := xlHAlignLeft;
          WorkSheet.Range['A' + IntToStr(1),'A' + IntToStr(R)].ColumnWidth := 31;
          // Show Excel
          ExcelApp.Visible[0] := True;
          // Disconnect the Server
          ExcelApp.Disconnect;
          // Unassign the Delphi Variant Matrix
          TabGrid := Unassigned;
        end; 
    end;//Retrieving Data from Excel
    procedure TForm1.BitBtnFromExcelClick(Sender: TObject);
    var 
      WorkBk : _WorkBook;
      WorkSheet : _WorkSheet;
      K, R, X, Y : Integer;
      IIndex : OleVariant;
      RangeMatrix : Variant;
      XLSFile : WideString;
    begin
      with TOpenDialog.Create(Self) do
        begin
          InitialDir:=ExtractFilePath(ParamStr(0));
          Filter:='Excel document (*.xls)|*.xls';
          Options:=[ofHideReadOnly, ofPathMustExist, ofFileMustExist];
          if Execute then XLSFile:=FileName;
          Free;
        end;
      if XLSFile='' then Exit;
      IIndex := 1;
      ExcelApp.Connect;
      // Open the Excel File
      ExcelApp.WorkBooks.Open(XLSFile,EmptyParam,EmptyParam,EmptyParam,
         EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,
         EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,0);
      WorkBk := ExcelApp.WorkBooks.Item[IIndex];
      WorkSheet := WorkBk.WorkSheets.Get_Item(1) as _WorkSheet;
      // In order to know the dimension of the WorkSheet, i.e the number of rows and the
      // number of columns, we activate the last non-empty cell of it
      WorkSheet.Cells.SpecialCells(xlCellTypeLastCell,EmptyParam).Activate;
      // Get the value of the last row
      X := ExcelApp.ActiveCell.Row;
      // Get the value of the last column
      Y := ExcelApp.ActiveCell.Column;
      // Define the number of the columns in the TStringGrid
      GenericStringGrid.ColCount := Y;
      // Assign the Variant associated with the WorkSheet to the Delphi Variant Matrix
      RangeMatrix := ExcelApp.Range['A1',ExcelApp.Cells.Item[X,Y]].Value[EmptyParam];
      // Quit Excel and Disconnect the Server
      ExcelApp.Quit;
      ExcelApp.Disconnect;
      //  Define the loop for filling in the TStringGrid
      K := 1;
      repeat
        for R := 1 to Y do
          GenericStringGrid.Cells[(R - 1),(K - 1)] := RangeMatrix[K,R];
        Inc(K,1);
        GenericStringGrid.RowCount := K + 1;
      until
      K > X;
      // Unassign the Delphi Variant Matrix
      RangeMatrix := Unassigned;
    end;Word的
    procedure TForm1.Button1Click(Sender: TObject);
    var
      templateName: OleVariant;
      newTemplate: OleVariant;
      ItemIndex: OleVariant;
      vSelection: WordSelection;
      vBookMark: BookMark;
      vTable: Table;
      I : Integer;
    begin
      // get dot file name
      templateName := ExtractFilePath(Application.ExeName) + 'WordApp.dot';  newTemplate := False;  try
        WordApp.Connect;
      except
        MessageDlg('Please Microsoft Office Word!', mtError, [mbOK], 0);
        Abort;
      end;  // create word document by specified dot
      WordApp.Documents.AddOld(templateName, newTemplate);
      WordApp.Caption := 'Word Application Demo Document';  vSelection := WordApp.Selection;  // get the first table
      vTable := WordApp.ActiveDocument.Tables.Item(1);  vTable.Cell(1, 2).Range.Text := 'Liu Yang';
      vTable.Cell(1, 4).Range.Text := 'LYSoft';
      vTable.Cell(2, 2).Range.Text := 'http://lysoft.7u7.net';
      vTable.Cell(3, 2).Range.Text := DateToStr(Now);
      vTable.Cell(3, 4).Range.Text := TimeToStr(Now);  vTable.Cell(5, 1).Range.Text := 'LYSoft powered by Liu Yang';
      vTable.Cell(7, 1).Range.Text := 'LY';
      vTable.Cell(9, 2).Range.Text := 'Demo project';  for I := 1 to WordApp.ActiveDocument.Books.Count do
        begin
          ItemIndex := I;
          // get the Index of specified book
          vBookMark := WordApp.ActiveDocument.Books.Item(ItemIndex);
          //ShowMessage(vBookMark.Name+IntToStr(wordApp.ActiveDocument.Books.Count)+'Now='+IntToStr(i));
          if CompareText(vBookMark.Name, 'bm_ID') = 0 then
             begin
               vBookMark.Select;
               vSelection.InsertAfter(IntToStr(GetTickCount));
             end;
        end;  with WordApp do
        begin
          Visible := CheckBox1.Checked;
          UserName := 'LY';
          // minimize
          WindowState := 2;
          // maxmize
          WindowState := 1;
          // print preview
          PrintPreview := CheckBox2.Checked;      // print now
          if CheckBox3.Checked then PrintOutOld;      Disconnect;
        end;
    end;
      

  3.   

    呵呵
    Word处理表格需要DOT模板的支持的自己慢慢研究了