폼에 ExcelApplication, ExcelWorkbook, ExcelWorksheet를 추가하고 버튼이벤트에 다음과 같이 기록했습니다...
procedure Tilbo1F.SpeedButton10Click(Sender: TObject);
const
xlleft = -4131;
xlright = -4152;
xltop = -4160;
xlbottom = -4107;
xlline = 1;
xlbline = 9;
xlcenter = -4108;
var i, j, m_Ex_Row, m_Ex_Col: integer;
m_ExTitle, m_ExFile : string;
begin
if ZQuery1.RecordCount <= 0 then Exit;
try
ExcelApplication1.Connect;
except
MessageDlg('Excel과 연결되지 않습니다.', mtError, [mbOk], 0);
Abort;
end;
m_ExFile := '파일명_' + DateToStr(DateTimePicker1.Date);
m_ExTitle := '타이틀';
try
ExcelApplication1.Caption := m_ExTitle;
ExcelApplication1.Workbooks.Add(xlWBATWorksheet,0);
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks.Item[1]);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Sheets[1] as _Worksheet);
m_Ex_Row := ZQuery1.RecordCount + 20;
m_Ex_Col := DBGrid1.Columns.Count;
with ExcelWorksheet1 do
begin
Range[Cells.Item[1,1], Cells.Item[m_Ex_Row,m_Ex_Col]].Font.Name := '굴림체';
Range[Cells.Item[1,1], Cells.Item[m_Ex_Row,m_Ex_Col]].Font.Bold := False;
Range[Cells.Item[1,1], Cells.Item[m_Ex_Row,m_Ex_Col]].Font.Size := 10;
Range[Cells.Item[1,1], Cells.Item[m_Ex_Row,m_Ex_Col]].VerticalAlignment := xlcenter;
Range[Cells.Item[1,1],Cells.Item[1,m_Ex_Col]].MergeCells := True;
Cells.Item[1,1] := m_ExTitle;
Cells.Item[1,1].HorizontalAlignment := xlcenter;
Range[Cells.Item[2,1],Cells.Item[2,m_Ex_Col]].MergeCells := True;
Cells.Item[2,1] := '조회일자 : ' + DateToStr(DateTimePicker1.Date);
Cells.Item[2,1].HorizontalAlignment := xlleft;
for i := 1 to m_Ex_Col do
begin
Cells.Item[3,i] := DBGrid1.Columns[i-1].Title.Caption;
Range[Cells.Item[3,1], Cells.Item[3,m_Ex_Col]].HorizontalAlignment := xlcenter;
if DBGrid1.Columns[i-1].Alignment = taRightJustify then
Range[Cells.Item[4,i], Cells.Item[m_Ex_Row,i]].HorizontalAlignment := xlright;
if DBGrid1.Columns[i-1].Alignment = taLeftJustify then
Range[Cells.Item[4,i], Cells.Item[m_Ex_Row,i]].HorizontalAlignment := xlleft;
if DBGrid1.Columns[i-1].Alignment = taCenter then
Range[Cells.Item[4,i], Cells.Item[m_Ex_Row,i]].HorizontalAlignment := xlcenter;
end;
ZQuery1.First;
i := 4;
while not ZQuery1.Eof do
begin
for j := 1 to m_Ex_Col do
begin
Cells.Item[i,j] := DBGrid1.Fields[j-1].Value;
end;
i := i + 1;
ZQuery1.Next;
end;
m_ExFile := jesmain.m_imsipath + m_ExFile + '.xls';
if fileexists(m_ExFile) then deletefile(pchar(m_ExFile)) ;
SaveAs(m_ExFile);
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
MessageDlg(m_ExFile+'로 저장되었습니다.', mtConfirmation, [mbOk], 0) ;
end;
except
on E: Exception do
begin
ShowMessage(E.Message);
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
end;
end;
DBGrid1.SetFocus;
end;
이러고 나면 엑셀파일로 내용이 잘 만들어집니다... 그런데 문제는 현 프로그램이 띄워진 상태에서 탐색기를 이용해 만들어진
파일을 더블클릭하면 엑셀창만 표시되고 내용이 나타나질 않습니다... 먹통이 된다고 해야하나요...
그런데 프로그램 종료하고 더블클릭하거나 프로그램이 그대로 있는 상태에서 그냥 엑셀을 실행해서 해당화일을 불러오기해서
해당파일을 불러오면 잘 읽어들여서 화면에 보여줍니다...
아무래도 저 위 코딩상에 엑셀파일을 만들고 나서 메모리상에 문제가 발생하는 요지가 있는듯 싶은데 뭔지를 모르겠습니다...
알려주십시요...
|