Q&A

  • 엑셀저장시 에러가 납니다ㅡ.ㅜ..
조회해서 엑셀로 저장을 하는데요...
<!--CodeS-->
procedure TMISDBGrid.DataSetToExcel;
const MAX_ROW = 65536;
var
  XL, XLBook, XLSheet, XArr, XTitle: Variant;
  i, j, k, sNum: Integer;
  sp : TBookMark;
  cidx, cmod: integer;
  cidxChar, cmodChar: Char;

begin
if not DataSource.DataSet.Active then Exit;
   sp := DataSource.DataSet.GetBookMark;
   DataSource.DataSet.DisableControls;
   //타이틀 처리변수
   XTitle := VarArrayCreate([1, FieldCount], VarVariant);
   //데이타 처리변수
   XArr := VarArrayCreate([1, FieldCount], VarVariant);

   try
      //엑셀을 실행
      XL := CreateOLEObject('Excel.Application');
   except
      MessageDlg('Excel이 설치되어 있지 않습니다.',
                 MtWarning, [mbok], 0);
      Exit;
   end;
   XL.SheetsInNewWorkbook := 1;
   XL.UserControl := False;
   XLBook := XL.WorkBooks.Add; //새로운 페이지 생성
   sNum := 1;
   XLSheet := XLBook.WorkSheets[1];

   i := 1;//column index
   j := 1;//index for visible columns

   k := 1;//row index
   //column 타이틀처리
   while i <= Self.FieldCount do begin
      if FsaveAllColumn and (Columns.Items[i-1].FieldName <> '') then
         XTitle[i] := Columns.Items[i-1].Title.Caption
      else if Columns.Items[i-1].Visible and ((Columns.Items[i-1].FieldName <> '')) then begin
         XTitle[j] := Columns.Items[i-1].Title.Caption;
         Inc(j);
      end;
      Inc(i);
   end;

   //column.Count > 26인 경우 -> excel column = 'AA1'
   //cell에 값을 넣는다.
   cidx := Self.FieldCount div 26;
   cmod := Self.FieldCount mod 26;

   if cidx > 0 then
   begin
           if cmod = 0 then begin
             if cidx = 1 then cidxChar := CHR(32) else cidxChar := CHR(64 + cidx - 1);
        cmodChar := CHR(64 + 26);
               end
           else begin
             cidxChar := CHR(64 + cidx);
        cmodChar := CHR(64 + cmod);
     end;
           XLSheet.Range['A1', trim(cidxChar) + trim(cmodChar) + '1'].Value := XTitle;
           XLSheet.Range['A1', trim(cidxChar) + trim(cmodChar) + '1'].Font.Bold := True;
              end
   else begin
           XLSheet.Range['A1', CHR(64 + Self.FieldCount) + '1'].Value := XTitle;
           XLSheet.Range['A1', CHR(64 + Self.FieldCount) + '1'].Font.Bold := True;
   end;

   DataSource.DataSet.First;
   while Not DataSource.DataSet.EOF do begin
      i := 1;
      j := 1;
      while j <= Self.FieldCount do begin
         if FsaveAllColumn then
            if (Columns.Items[j-1].FieldName <> '') then
               if (Columns.Items[j-1].Field is TStringField) then XArr[j] := '''' + VarToStr(Self.Fields[j-1].Value)
               else XArr[j] := Self.Fields[j-1].Value
            else XArr[j] := ''
         else if Columns.Items[j-1].visible then begin
               if (Columns.Items[j-1].FieldName <> '') then
                  if (Columns.Items[j-1].Field is TStringField) then XArr[i] := '''' + VarToStr(Self.Fields[j-1].Value)
                  else XArr[i] := Self.Fields[j-1].Value
               else XArr[i] := '';
            inc(i);
         end;
         Inc(j);
      end;//while

      //셀에 값을 넣는다.
           if cidx > 0 then XLSheet.Range['A' + IntToStr(k+1), trim(cidxChar) + trim(cmodChar) + IntToStr(k+1)].Value := XArr
           else XLSheet.Range['A' + IntToStr(k+1), CHR(64 + Self.FieldCount) + IntToStr(k+1)].Value := XArr;

     DataSource.DataSet.Next;
     Inc(k);

     //data 건수가 타이틀 포함 65536건이상이면 sheet 추가
     if k = MAX_ROW then
     begin
        //추가하기전 현재 sheet의 셀크기 조정
                  if cidx > 0 then XLSheet.Range['A1', trim(cidxChar) + trim(cmodChar) + '1'].EntireColumn.AutoFit
                   else XLSheet.Range['A1', CHR(64 + Self.FieldCount) + '1'].EntireColumn.AutoFit;

        XLSheet := Xl.WorkSheets.Add;
        inc(sNum);
        k := 1;
           if cidx > 0 then
           begin
              XLSheet.Range['A1', trim(cidxChar) + trim(cmodChar) + '1'].Value := XTitle;
            XLSheet.Range['A1', trim(cidxChar) + trim(cmodChar) + '1'].Font.Bold := True;
           end
           else begin
           XLSheet.Range['A1', CHR(64 + Self.FieldCount) + '1'].Value := XTitle;
           XLSheet.Range['A1', CHR(64 + Self.FieldCount) + '1'].Font.Bold := True;
           end;
     end;
   end;
   //마지막 sheet의 셀 크기 조정
   if cidx > 0 then XLSheet.Range['A1', trim(cidxChar) + trim(cmodChar) + '1'].EntireColumn.AutoFit
   else XLSheet.Range['A1', CHR(64 + Self.FieldCount) + '1'].EntireColumn.AutoFit;

   try
      DataSource.DataSet.GotoBookMark(sp);
      DataSource.DataSet.FreeBookMark(sp);
   except
      DataSource.DataSet.First;
   end;

   XL.DisplayAlerts := FShowExcel;
   if not FShowExcel then XL.ActiveWorkBook.SaveAs(getFileName);

   XL.Visible := FShowExcel;
   XL.UserControl := True;
   XLSheet := XLBook.WorkSheets[1];
   if FShowExcel then XLSheet.Range['A1', 'A1'].Select;
   XL.Quit;

   DataSource.DataSet.EnableControls;
end;
<!--CodeE-->


이렇게 하는데..OLE error 800A03EC 에러가 납니다..

어떻게 해야하죠?


0  COMMENTS