首页  编辑  

一个操作Excel的例程

Tags: /超级猛料/Office.OA自动化/Excel/   Date Created:

可以自己看看效果:

procedure TForm1 . updatexls ;

var

 xlapp , sheet       : variant ;

 row , i             : integer ;

 filename , fh       : string ;

begin

  try

   xlapp := createoleobject ( 'excel.application' );

  except

   showmessage ( 'not found excel in your system, so can not create file!' );

   exit ;

  end ;

 xlapp . visible := true ;

 xlApp . ScreenUpdating := False ;

  try

    for i := 0 to 10 do

    begin

     fh := '' ;

     filename := 'E:\excel\' + Inttostr ( i ) + '.xls' ;

     xlapp . workbooks . add ;

//    xlapp.workbooks[1].saveas(newfilename);

     sheet := xlapp . workbooks [ 1 ]. worksheets [ 1 ];

     sheet . pagesetup . LeftMargin := xlapp . InchesToPoints ( 0 );

     sheet . pagesetup . rightMargin := xlapp . InchesToPoints ( 0 );

     sheet . pagesetup . topMargin := xlapp . InchesToPoints ( 0 );

     sheet . pagesetup . bottomMargin := xlapp . InchesToPoints ( 0 );

     sheet . pagesetup . headerMargin := xlapp . InchesToPoints ( 0 );

     sheet . pagesetup . footerMargin := xlapp . InchesToPoints ( 0 );

     fh := VarAsType ( sheet . cells [ 1 , 6 ], varstring );

     xlapp . Range [ sheet . cells [ 1 , 1 ], sheet . cells [ 9 , 82 ]]. MergeCells := False ; //Çå ³ý ºÏ ²¢

     xlapp . Range [ sheet . cells [ 3 , 1 ], sheet . cells [ 3 , 1 ]]. entirerow . insert ;

     xlapp . Range [ sheet . cells [ 3 , 1 ], sheet . cells [ 3 , 1 ]]. entirerow . insert ;

     xlapp . Range [ sheet . cells [ 3 , 1 ], sheet . cells [ 3 , 1 ]]. entirerow . insert ;

     xlapp . Range [ sheet . cells [ 3 , 1 ], sheet . cells [ 3 , 1 ]]. entirerow . insert ;

     xlapp . Range [ sheet . cells [ 1 , 1 ], sheet . cells [ 13 , 82 ]]. select ;

     xlapp . selection . ShrinkToFit := False ; //×Ô ¶¯ Ìî ³ä

     xlapp . selection . AddIndent := False ; //Ôö ¼Ó Ëõ ½ø

     xlapp . selection . WrapText := False ; //×Ô ¶¯ »» ÐÐ

     xlapp . selection . ClearContents ; //Çå ³ý ÄÚ ÈÝ

     xlapp . selection . borders [ 5 ]. LineStyle := xlnone ;

     xlapp . selection . borders [ 6 ]. LineStyle := xlnone ;

     xlapp . selection . Orientation := 0 ;

     xlapp . selection . font . name := 'ËÎ Ìå ' ;

     xlapp . selection . font . size := '10' ;

     xlapp . selection . HorizontalAlignment := xlgeneral ;

     xlapp . selection . NumberFormatLocal := 'G/ͨ Óà ¸ñ ʽ ' ;

     sheet . cells [ 1 , 1 ] := '»õ ºÅ :' ;

     xlapp . range [ sheet . cells [ 1 , 1 ], sheet . cells [ 1 , 1 ]]. font . Name := '¿¬ Ìå _GB2312' ;

     xlapp . range [ sheet . cells [ 1 , 1 ], sheet . cells [ 1 , 1 ]]. font . size := 18 ;

     xlapp . range [ sheet . cells [ 1 , 1 ], sheet . cells [ 1 , 1 ]]. font . Bold := true ;

     xlapp . Range [ sheet . cells [ 1 , 1 ], sheet . cells [ 1 , 5 ]]. MergeCells := true ;

     sheet . cells [ 1 , 6 ] := fh ;

     xlapp . range [ sheet . cells [ 1 , 6 ], sheet . cells [ 1 , 6 ]]. font . Name := 'Times New Roman' ;

     xlapp . range [ sheet . cells [ 1 , 6 ], sheet . cells [ 1 , 6 ]]. font . size := 18 ;

     xlapp . Range [ sheet . cells [ 1 , 6 ], sheet . cells [ 1 , 30 ]]. MergeCells := true ;

     sheet . cells [ 3 , 1 ] := '¾-   Ïß ' ;

     xlapp . Range [ sheet . cells [ 3 , 1 ], sheet . cells [ 3 , 31 ]]. select ;

     xlapp . Range [ sheet . cells [ 3 , 1 ], sheet . cells [ 10 , 5 ]]. select ;

     xlapp . selection . MergeCells := true ;

     xlapp . selection . HorizontalAlignment := xlcenter ;

     xlapp . selection . VerticalAlignment := xlcenter ;

     xlapp . selection . Orientation := xlVertical ;

      for row := 3 to 10 do //¾-Ïß Ô-ÁÏ

      begin

       xlapp . Range [ sheet . cells [ row , 6 ], sheet . cells [ row , 7 ]]. select ;

       xlapp . selection . MergeCells := true ;

       xlapp . selection . HorizontalAlignment := xlgeneral ;

       xlapp . selection . VerticalAlignment := xlcenter ;

       xlapp . Range [ sheet . cells [ row , 8 ], sheet . cells [ row , 25 ]]. select ;

       xlapp . selection . MergeCells := true ;

       xlapp . selection . HorizontalAlignment := xlgeneral ;

       xlapp . selection . VerticalAlignment := xlcenter ;

       xlapp . Range [ sheet . cells [ row , 26 ], sheet . cells [ row , 30 ]]. select ;

       xlapp . selection . MergeCells := true ;

       xlapp . selection . HorizontalAlignment := xlcenter ;

       xlapp . selection . VerticalAlignment := xlcenter ;

      end ;

     sheet . cells [ 3 , 31 ] := '»ú À-¿ª ¶È :' ;

      for row := 1 to 3 do

      begin

       xlapp . Range [ sheet . cells [ row * 2 + 1 , 31 ], sheet . cells [ row * 2 + 2 , 35 ]]. select ;

       xlapp . selection . MergeCells := true ;

       xlapp . selection . HorizontalAlignment := xlcenter ;

       xlapp . selection . VerticalAlignment := xlcenter ;

       xlapp . Range [ sheet . cells [ row * 2 + 1 , 36 ], sheet . cells [ row * 2 + 2 , 43 ]]. select ;

       xlapp . selection . MergeCells := true ;

       xlapp . selection . HorizontalAlignment := xlcenter ;

       xlapp . selection . VerticalAlignment := xlcenter ;

      end ;

      for row := 12 to 13 do

      begin

       xlapp . Range [ sheet . cells [ row , 73 ], sheet . cells [ row , 82 ]]. select ;

       xlapp . selection . MergeCells := true ;

       xlapp . selection . HorizontalAlignment := xlcenter ;

       xlapp . selection . VerticalAlignment := xlcenter ;

      end ;

     row := 11 ;

     xlapp . Range [ sheet . cells [ row , 31 ], sheet . cells [ row + 2 , 66 ]]. select ;

     xlapp . selection . MergeCells := true ;

     xlapp . selection . HorizontalAlignment := xlgeneral ;

     xlapp . selection . VerticalAlignment := xltop ;

     sheet . rows . item [ '1' ]. RowHeight := 22 ;

     xlapp . Range [ sheet . cells [ 2 , 1 ], sheet . cells [ 13 , 82 ]]. select ;

     xlapp . selection . borders [ 7 ]. LineStyle := 1 ;

     xlapp . range [ sheet . cells [ 16 , 1 ], sheet . cells [ 16 , 1 ]]. select ;

     xlapp . displayalerts := false ;

     sheet . saveas ( FileName );

     xlapp . workbooks [ 1 ]. close ;

     VarClear ( Sheet );

    end ;

   xlapp . quit ;

   Application . ProcessMessages ;

  finally

   xlapp . ScreenUpdating := True ;

  end ;

end ;