델파이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
델파이2007. 11. 23. 20:47
델파이 5에서 잘 되던 VarArrayOf와 VarArrayCreate가 안되면..
uses 절에 Variants 를 추가해줘야 쓸수 있다.

var
  data :Variant;
begin
  data:=VarArrayOf(배열에 들어갈 내용);
  //ex
  data:=VarArrayOf('번호','이름','점수');

  data:=VarArrayCreate([시작지점,생성할갯수],자료형); // 이거 되긴 하는데 시작지점과 생성갯수가 확실한지 모르겠다!! 저는 비기너니까*-_-* 누가 좀 알려주세요!! 도움말에는 The Bounds_size parameter is the index of the last value in the Bounds array. 이렇게 나와있습니다..(..) 번역 바랍니다... 쿨럭..
  //ex
  data:=VarArrayCreate([1,3],varVariant);
  //위와 같이 선언하고 data[1]:='번호'; data[2]:='이름'; data[3]:='점수'; 와 같이 사용할 수 있다.

다음엔 이 Variant 타입을 엑셀로 내보내는걸 복습하자!!
Posted by Mons
델파이2007. 11. 19. 23:52

다음 소스는 info2.txt 라는 파일을 불러와서 , 로 구분되어있는 문자열들을 StringGrid로 옮기는 작업을 하는것입니다.
보시다 시피 폼이 만들어질때 파일을 가져와서(파일 내용은 수검번호,이름,주민번호,학년,반으로 , 로 구분되어져 있습니다.) 일단 파일이 있는지 확인하고 ,를 찾아서 각 변수에 복사, 삭제를 반복하는 형식으로 되어있습니다. 아 힘들다.ㅜ.ㅡ

procedure TForm1.FormCreate(Sender: TObject);
var
  temp, st, files, bun, name, jumin, school, hak, ban:string;
  n, p:Integer;
  fh:TextFile;
begin
  n:=1;
  files:='info2.txt';

  StringGrid1.Cells[0,0]:='수검번호';
  StringGrid1.Cells[1,0]:='이름';
  StringGrid1.Cells[2,0]:='주민번호';
  StringGrid1.Cells[3,0]:='학년';
  StringGrid1.Cells[4,0]:='반';

  if not FileExists(files) then
  begin
    ShowMessage(files+'파일이 없습니다.');
    Exit;
  end;

  AssignFile(fh,files);

  Reset(fh);

  while not Eof(fh) do
  begin
    ReadLn(fh,temp);

    p:=Pos(',',temp);
    bun:=Copy(temp,1,p-1);
    Delete(temp,1,p);

    p:=Pos(',',temp);
    name:=Copy(temp,1,p-1);
    Delete(temp,1,p);

    p:=Pos(',',temp);
    jumin:=Copy(temp,1,p-1);
    Delete(temp,1,p);

    p:=Pos(',',temp);
    school:=Copy(temp,1,p-1);
    Delete(temp,1,p);

    if ComboBox1.Items.IndexOf(school)= -1 then
      ComboBox1.Items.Add(school);

    p:=Pos(',',temp);
    hak:=Copy(temp,1,p-1);
    Delete(temp,1,p);

    ban:=temp;

    StringGrid1.Cells[0,n]:=bun;
    StringGrid1.Cells[1,n]:=name;
    StringGrid1.Cells[2,n]:=jumin;
    StringGrid1.Cells[3,n]:=hak;
    StringGrid1.Cells[4,n]:=ban;

    StringGrid1.RowCount:= n+1;

    Inc(n);
  end;

  Caption:='건수 : '+IntToStr(n-1);
end;

Posted by Mons