델파이2007. 11. 24. 18:52

보시다시피 버튼을 클릭하면 Variant 형의 data형과 xlApp라는 Variant형을(응? 이건 어디서 지정해놨지;;) 선언하고

엑셀을 실행하고 data에 WorkBooks를 추가하고 WorkSheets의 이름을 인명부로 바꿔준다.
엑셀을 보이게끔 해준다음에 해당 범위를 병합해주고 값 바꿔주고
StringGrid의 cell 값을 Variant 배열에다 집어넣고
A3:E3부터 점차적으로 값을 늘려 가며 data를 집어넣는다.
그리고 c가 2로 나눠서 1이 남으면 배경색을 바꿔준다.
마지막으로 AutoFit 해주고..
그런데 컬러값을 대입할때 RGB 값이 아닌가?
왜 무려 8자나 되지.ㅜ..ㅡ 전 초보니까 좀 알려주실분!!

procedure TForm1.Button1Click(Sender: TObject);

var

  data: Variant;

  st : String;

  c : Integer;

begin

  try

    xlApp:=CreateOleObject('Excel.Application');

  except

    ShowMessage('엑셀실행오류');

    Exit;

  end;

 

  data:=VarArrayCreate([1,5],varVariant);

  xlApp.WorkBooks.Add;

  xlApp.WorkBooks[1].WorkSheets[1].Name:='인명부';

  xlApp.Visible:=true;

  xlApp.Range['A1:E1'].Merge;

  xlApp.Range['A1'].Value:='인명부';

  xlApp.Range['A1:E1'].Interior.Color := clBlue;

  xlApp.Range['A1:E1'].Font.Color := clWhite;

  xlApp.Range['A1:E1'].HorizontalAlignment := -4108;

  xlApp.Range['A2:E2'].Value:=VarArrayOf(['수검번호','이름','주민등록번호','','번호']);

 

  for c:=1 to StringGrid1.RowCount-1 do

  begin

    data[1]:=StringGrid1.Cells[0,c];

    data[2]:=StringGrid1.Cells[1,c];

    data[3]:=StringGrid1.Cells[2,c];

    data[4]:=StringGrid1.Cells[3,c];

    data[5]:=StringGrid1.Cells[4,c];

 

    st:='A'+IntToStr(c+2)+':E'+IntToStr(c+2);

    xlApp.Range[ st ].Value:=data;

    if c mod 2=1 then

      xlApp.Range[st].Interior.Color:=$00FFBBBB;

  end;

  st:='A3:E3';

  xlApp.Range[ st ].Columns.AutoFit;

 

end;

Posted by Mons