유돌이

calendar

1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

Notice

2009. 6. 26. 13:59 델파이

function fnStringGridToExcel(StringGrid1: TStringGrid) : Boolean;
var
XL, XArr: Variant;
i, j: Integer;
XLastPosion: String;
Sp : Integer;
begin

//데이타 처리변수
XArr := VarArrayCreate([1, StringGrid1.ColCount], VarVariant);

try
//엑셀을 실행
XL := CreateOLEObject('Excel.Application');
except
MessageDlg('Excel이 설치되어 있지 않습니다.', MtWarning, [mbok], 0);
Exit;
end;

XL.WorkBooks.Add; //새로운 페이지 생성
// XL.Visible := True;

for i := 0 to StringGrid1.RowCount - 1 do begin
for j := 0 to StringGrid1.ColCount - 1 do begin
XArr[j + 1] := StringGrid1.Cells[j, i];
end;


Sp := StringGrid1.ColCount Div 26;
if Sp <> 0 then
XLastPosion := CHR(64 + Sp) + CHR(64 + StringGrid1.ColCount Mod 26)
else
XLastPosion := CHR(64 + StringGrid1.ColCount);

//엑셀에 값을 넣는다.
XL.Range['A' + IntToStr(i + 1), XLastPosion + IntToStr(i + 1)].Value :=
XArr;

end;
//셀 크기 조정
XL.Range['A1', XLastPosion + IntToStr(i + 1)].Select;
XL.Selection.Columns.AutoFit;
XL.Range['A1', 'A1'].Select;
XL.Visible := True;
Result := True;
end;


posted by 유돌이