Delphi XE10.2打开新的MS Word文档并插入表格


 procedure TForm1.CreateTable(Rows, Columns : Integer);
var
  MSWord,
  Document,
  Table,
  Range,
  Cell: OleVariant;
  ARow,
  AColumn: Integer;
  RowIndex,
  ColIndex : Integer;
  S : String;
begin
  MsWord := CreateOleObject('Word.Application');
  MsWord.Visible := True;

  Document := MSWord.Documents.Add;
  MSWord.Selection.Font.Size := 22;
  MSWord.Selection.Font.Bold := true;
  MSWord.Selection.TypeText(#13#10);
  MSWord.Selection.TypeText('This should be center-aligned');
  MSWord.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter;
  MSWord.Selection.TypeText(#13#10#13#10);
  MSWord.Selection.Font.Size := 12;
  MSWord.Selection.Font.Bold := False;

  Table := MSWord.ActiveDocument.Tables.Add( Range:= MSWord.Selection.Range, NumRows:= Rows, NumColumns:= Columns, DefaultTableBehavior:= wdWord9TableBehavior, AutoFitBehavior:= wdAutoFitFixed);

  MSWord.Selection.ParagraphFormat.Alignment := wdAlignParagraphJustify;
  for ARow := 1 to Rows do begin
    for AColumn := 1 to Columns do begin
      Cell := Table.Cell(ARow, AColumn);
      RowIndex := Cell.RowIndex;
      ColIndex := Cell.ColumnIndex;
      Caption := IntToStr(RowIndex) + '/' + IntToStr(ColIndex);
      Range := Cell.Range;
      Range.Select;
      MSWord.Selection.ParagraphFormat.Alignment := wdAlignParagraphJustify;
      if Odd(AColumn) then
        Range.Font.Bold := True
      else
        Range.Font.Bold := False;
      S := Format('Row: %d, col: %d', [RowIndex, ColIndex]);
      MSWord.Selection.Range := Range;
      MSWord.Selection.TypeText(Text := S);
    end;
  end;
end;


联系电话:
020-00000000
联系电话:
020-00000000
联系电话:
020-12345678