2011년 3월 15일 화요일

MS Excel 문서 읽기

uses Windows, SysUtils, Forms, Variants, ComObj, OleServer;


...
...
...


functionGetExcelText(fName: String): String;
var
Excel: OleVariant;
i, j: Integer;
tmpString: String;

begin
Result:='';

// 엑셀 어플리케이션 생성
try
Excel:=CreateOleObject('Excel.application');
except MessageBox(0,'Excel이 설치되어 있지 않습니다.','',MB_OK);
Exit;
end;

// 파일이 있는지 확인
if notFileExists(fName)then begin
MessageBox(0,'파일이 없습니다','',MB_OK);
Exit;
end;

// WorkBooks Open
Excel.Workbooks.Open(fName);

// Cell 의 내용을 받아옴
fori:=1 toExcel.ActiveSheet.UsedRange.Rows.Count do begin forj:=1toExcel.ActiveSheet.UsedRange.Columns.Countdo begin
tmpString:=Excel.Cells[i, j];
ifLength(Trim(tmpString))>0thenResult:=Result+#13#10+tmpString;
Application.ProcessMessages;
end;
end;

// 해제과정
Excel.Workbooks.Close;
Excel.Quit;
Excel:=Unassigned;
end;

댓글 없음:

댓글 쓰기