Q&A

  • 엑셀 WorkSheet 추가하기...
procedure TfDataSearch_Repair.FlatButton4Click(Sender: TObject);

var

I, J : Integer;

Range, sfile : String;

begin

sFile := '조회결과';

If VarIsEmpty(MyExcel) Then Begin

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

MyExcel.WorkBooks.Add;

end;



MyExcel.WorkBooks[1].WorkSheets[Sheet].Name := sFile + IntTostr(sheet);



MyExcel.WorkSheets[Sheet].Range['A1:C1'].Font.Size := 14;

MyExcel.WorkSheets[Sheet].Range['A1:C1'].Font.Bold := True;

MyExcel.WorkSheets[sFile + IntTostr(sheet)].Cells[1, 1] := fc_Search.Text;

for i := 0 to Sg_List.RowCount-1 do begin

for j := 1 to Sg_List.ColCount-1 do begin

MyExcel.WorkSheets[sFile + IntTostr(sheet)].Cells.Item[i+4,j+1]

:= Sg_List.Cells[j,i];

end;

end;

Inc(Sheet);

MyExcel.Visible := True;

end;



보통 Excel이 기동되면 3개의 WorkSheets가 생성되어 있습니다.

위와 같이 코딩을 하면 3개까지는 하나의 WorkBook에 제대로 Write가 되는데

4번째 부터는 WorkSheet가 없으니까 에러가 발생하게 됩니다.

WorkSheet를 추가해주는 방법은 없나요.

1  COMMENTS
  • Profile
    아세만 2000.07.14 22:19
    cell wrote:

    > procedure TfDataSearch_Repair.FlatButton4Click(Sender: TObject);

    > var

    > I, J : Integer;

    > Range, sfile : String;

    > begin

    > sFile := '조회결과';

    > If VarIsEmpty(MyExcel) Then Begin

    > MyExcel := CreateOleObject('Excel.Application');

    > MyExcel.WorkBooks.Add;

    > end;

    >

    > MyExcel.WorkBooks[1].WorkSheets[Sheet].Name := sFile + IntTostr(sheet);

    >

    > MyExcel.WorkSheets[Sheet].Range['A1:C1'].Font.Size := 14;

    > MyExcel.WorkSheets[Sheet].Range['A1:C1'].Font.Bold := True;

    > MyExcel.WorkSheets[sFile + IntTostr(sheet)].Cells[1, 1] := fc_Search.Text;

    > for i := 0 to Sg_List.RowCount-1 do begin

    > for j := 1 to Sg_List.ColCount-1 do begin

    > MyExcel.WorkSheets[sFile + IntTostr(sheet)].Cells.Item[i+4,j+1]

    > := Sg_List.Cells[j,i];

    > end;

    > end;

    > Inc(Sheet);

    > MyExcel.Visible := True;

    > end;

    >

    > 보통 Excel이 기동되면 3개의 WorkSheets가 생성되어 있습니다.

    > 위와 같이 코딩을 하면 3개까지는 하나의 WorkBook에 제대로 Write가 되는데

    > 4번째 부터는 WorkSheet가 없으니까 에러가 발생하게 됩니다.

    > WorkSheet를 추가해주는 방법은 없나요.



    안녕하십니까

    아세만입니다.



    엑셀에서 WorkSheets를 생성하는 방법은 다음과 같습니다.



    for I := 1 to 5 do

    begin

    MyExcel.WorkSheets.Add;

    end;



    WorkSheets가 필요한 시점에서 위와 같이 코딩해 주면 WorkSheets가 5개 생성되죠!

    그럼...