Q&A

  • D 5 에서 엑셀 셀 병합.. ??
질답란을 참고로 하여 코딩을 해보았지만...

안돼더군여..

델 5에서 TExcelApplication, TExcelWorksheet 를 사용할 경우
셀병합을 어떻게 하는 것인지 궁금합니다.

WS <= TExcelWorksheet Component

WS.Range['A1', 'C1'].MergeArea;
=> 이렇게 하면 실행시 에러가 발생합니다..

조언 부탁합니다.
1  COMMENTS
  • Profile
    강형철 2002.05.08 20:12

    안녕하세요. 강형철입니다.

    일단 제가 D5에서 그 컴퍼넌트로는 해보지는 않았지만.. Ole로 구현을 했었습니다.. 한번 아래와 같이 해보세요... 장담은 못합니다.

    WS <= TExcelWorksheet Component;

    WS.Range['A1', 'C1'].Select;
    WS.Selection.Merge;

    그리고 참고로 제가 전에 HyperGrid를 사용해서 그 그리드의 내용을 엑셀로 보낸 루틴을 한적이 있는데 원하면 아래를 참조하세요 ^^


    HyperGrid가 있는 싸이트

    http://www.pablop.demon.co.uk/marley/thypergrid.htm


    unit UserHgExcel;

    interface

    uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      HgGrid, HgGlobal , HgColumn;

    type

      ThgExcelOption = ( hgexlFont , hgexlAlignment );
      ThgExcelOptions = set of ThgExcelOption;

    function UserHyperGridToExcel( Grid : THyperGrid; Options : ThgExcelOptions ; Title : String ) : Boolean;

    implementation

    uses ComOBJ , Grids , ExtCtrls;

    function UserHyperGridToExcel( Grid : THyperGrid; Options : ThgExcelOptions; Title : String ) : Boolean;
    var
        Excel     : Variant;
        WorkBook  : Variant;
        WorkSheet : Variant;
        Cell      : Variant;

        Col , Row : Longint;
        Column    : ThgHeading;

        GridState   : TGridDrawState;
        CellState   : ThgCellStates;
        Color       : TColor;
        Font        : TFont;
        DrawInfo    : ThgDrawInfo;
        OuterBevel  : TPanelBevel;
        InnerBevel  : TPanelBevel;
        OldCursor   : TCursor;

        szHeading : String;  //HyperGrid의 Heading
        BeginCol  : Integer;  //병합시킬 시작 열
        EndCol    : Integer;    //병합시킬 끝 열
    begin
        szHeading := '';
        BeginCol := 0;
        EndCol := 0;
        Result := False;

        try
             Excel := CreateOLEObject( 'Excel.Application' );
             WorkBook := Excel.Workbooks.Add;

             WorkSheet := WorkBook.Worksheets.Add;
             WorkSheet.Name := Title;
             Font := TFont.Create;
             OldCursor := Screen.Cursor;
             Screen.Cursor := crHourglass;

             try
             //제목 넣는 부분
                  Column := Grid.VisibleColumns[ Col ];
                  Cell := WorkSheet.Cells[1,1];
                  Cell.Font.Name := '굴림체';
                  Cell.Font.Size := 13;
                  Cell.Font.Color := clBlack;
                  Cell.Font.Bold := [fsBold] ;
                  Cell.VerticalAlignment := $FFFFEFF4;
                  Cell.HorizontalAlignment := $FFFFEFF4;
                  Cell.Value := Title;

                  for Col := 0 to Pred( Grid.ColCount ) do
                  begin
                       Column := Grid.VisibleColumns[ Col ];

                       Cell := WorkSheet.Cells[3,Col+1];
                       Cell.Interior.Color := clSilver;

                       if Grid.Columns[Col].HeadingIndex <> -1 then //Heading이 있으면
                       begin
                            //Heading이 변했으면
                            if szHeading <> Grid.Headings[Grid.Columns[Col].HeadingIndex].Caption then
                            begin
                                 Cell.Font.Name  := '굴림체';
                                 Cell.Font.Size  := 9;
                                 Cell.Font.Color := clBlack;
                                 Cell.VerticalAlignment   := $FFFFEFF4;
                                 Cell.HorizontalAlignment := $FFFFEFF4;
                                 Cell.Value := Grid.Headings[Grid.Columns[Col].HeadingIndex].Caption;
                                 szHeading := Grid.Headings[Grid.Columns[Col].HeadingIndex].Caption;
                                 BeginCol := Col + 1 ;
                            end
                            else //Heading이 안 변했으면
                            begin
                                 EndCol := Col + 1;
                            end;
                       end
                       else  //Heading이 없으면
                       begin
                            Cell.Value := '';
                       end;

                       for Row := 0 to Pred( Grid.RowCount ) do
                       begin
                            GridState := [];
                            if ( Col < Grid.FixedCols ) or ( Row < Grid.FixedRows ) then
                                 GridState := [ gdFixed ];

                            Grid.GetCellAttributes( Col , Row , Column , GridState , CellState ,
                                                    Color , Font , DrawInfo , OuterBevel , InnerBevel );
                            Cell := Worksheet.Cells[ Row + 4 , Col + 1  ];

                            if Row = 0 then
                                 Cell.Interior.Color := clSilver
                            else
                                 Cell.Interior.Color := Color;
                                
                            if hgexlFont in Options then
                            begin
                                 Cell.Font.Name := Font.Name;
                                 Cell.Font.Size := Font.Size;
                                 Cell.Font.Color := Font.Color;
                                 Cell.Font.Bold := fsBold in Font.Style;
                                 Cell.Font.Italic := fsItalic in Font.Style;
                                 Cell.Font.Underline := fsUnderline in Font.Style;
                                 Cell.Font.Strikethrough := fsStrikeout in Font.Style;
                            end;

                            if hgexlAlignment in Options then
                            begin
                                 case DrawInfo.VAlign of
                                      hgvaTop     : Cell.VerticalAlignment := $FFFFEFC0;
                                      hgvaCenter  : Cell.VerticalAlignment := $FFFFEFF4;
                                      hgvaBottom  : Cell.VerticalAlignment := $FFFFEFF5;
                                 end;

                                 case DrawInfo.HAlign of
                                      taLeftJustify   : Cell.HorizontalAlignment := $FFFFEFDD;
                                      taCenter        : Cell.HorizontalAlignment := $FFFFEFF4;
                                      taRightJustify  : Cell.HorizontalAlignment := $FFFFEFC8;
                                 end;
                            end;

                            Cell.Value := DrawInfo.Text;
                       end;

                       if Grid.Columns[Col].HeadingIndex = -1 then  //Heading이 없으면 열 병합
                       begin
                            Excel.Range[CHR(64+Col+1)+'3',CHR(64+Col+1)+'4'].Select;
                            Excel.Selection.Merge;
                       end;

                       if EndCol = Col + 1 then //Heading이 있으면 행 병합
                       begin
                            Excel.Range[CHR(64 + BeginCol)+'3', CHR(64+EndCol)+'3'].Select;
                            Excel.Selection.Merge;
                       end;

                       Excel.Range[CHR(64+Col+1)+'1',CHR(64+Col+1)+IntToStr(4 + Pred( Grid.RowCount ))].Select;
                       Excel.Selection.Columns.ColumnWidth := Grid.ColWidths[Col]/9;
                       Excel.Range[CHR(64+Col+1)+'3',CHR(64+Col+1)+'4'].Select;
                       Excel.Selection.Borders.Color := clBlack;
                       Excel.Range[CHR(64+Col+1)+'5',CHR(64+Col+1)+IntToStr(4+Pred( Grid.RowCount ))].Select;
                       Excel.Selection.Borders.Color := clSilver;
                  end;
                  //제목 병합
                  Excel.Range['A1',CHR(64 + Pred(Grid.ColCount) + 1)+'2'].Select;
                  Excel.Selection.Merge;
                  Excel.Range['A5','A5'].Select;
                  Excel.Visible := True;
                  Result := True;
             finally
                  Screen.Cursor := OldCursor;
                  Font.Free;
             end;
        except
             ;
        end;
    end;