Q&A

  • DB=>Excel저장시 엑셀의 Cell의 Width변경 가능한지...
DB그리드의 데이타를 엑셀로 저장하는데 엑셀을 각 셀의 폭을 변경하여

저장할려고 합니다.

가능한지요?



3  COMMENTS
  • Profile
    박장용 2001.08.08 04:57
    cjw wrote:

    > DB그리드의 데이타를 엑셀로 저장하는데 엑셀을 각 셀의 폭을 변경하여

    > 저장할려고 합니다.

    > 가능한지요?

    > 가능합니다.. autofit을 사용하시면요..

    XL.Selection.Columns.AutoFit;

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    샘플을 올립니다.. 적당히 수정하셔서 사용하세요..



    function TFormDbri6301.fExcelSimpleConvThree(sSheetName:string;

    iCurrency :string):boolean;

    var XL : Variant;

    ir, j : Integer;

    iCount : integer;

    sSumformula : string;

    begin

    result := True;



    //엑셀

    try

    XL := CreateOLEObject('Excel.Application');

    except

    Application.MessageBox('Excel이 설치되어 있지 않습니다.',

    '정보', MB_ICONINFORMATION);

    result := False;

    Exit;

    end;



    with txsBT_DBRI6300 do begin

    DisableControls;



    try

    XL.WorkBooks.Add; //페이지 생성

    XL.ActiveSheet.Name := sSheetName; //쉬트명 지정

    ir := 1; //현재 엑셀의 Row수



    //검색조건 출력

    XL.Cells[ ir, 1].Value := '청구년월 : ' + mpickerSalesDate3.GetFormatText;

    inc(ir);

    XL.Cells[ ir, 1].Value := '통화구분 : ' + cboxCurrency3.Text;

    inc(ir);

    if rbtn31.Checked then XL.Cells[ir,1] := '구 분 : 청구분'

    else XL.Cells[ir,1] := '구 분 : 확정분';

    inc(ir);



    //첫번째 서비스를 연다.

    if not fServiceCall(3) then begin

    result := False;

    XL := unassigned;

    exit;

    end;

    //Header color

    XL.Range[ XL.Cells[ir, 1], XL.Cells[ir, 9] ].Select;

    XL.Selection.Interior.ColorIndex := xlColorGray;

    XL.Selection.Interior.Pattern := xlSolid;

    //첫번째 표 헤더 작성

    XL.Cells[ir, 1].Value := '조정구분';

    XL.Cells[ir, 2].Value := '매출환급구분';

    XL.Cells[ir, 3].Value := '공급가액';

    XL.Cells[ir, 4].Value := '부가세';

    XL.Cells[ir, 5].Value := '합계';

    XL.Cells[ir, 6].Value := '자동할인';

    XL.Cells[ir, 7].Value := '연체료';

    XL.Cells[ir, 8].Value := 'myBill할인';

    XL.Cells[ir, 9].Value := '총합계';



    inc(ir);



    iCount := 0;

    First;

    while not EOF do begin

    XL.Cells[ir, 1].Value := Fieldbyname('STR21').asstring; //조정구분

    XL.Cells[ir, 2].Value := '매출'; //Fieldbyname('STR22').asstring; //매출환급구분

    //현재는 매출밖에 없슴.. 추후 추가

    if iCurrency = '달러' then begin

    XL.Cells[ir, 3].Value := (Fieldbyname('DOUBLE11').asfloat/100); //공급가액

    XL.Cells[ir, 4].Value := (Fieldbyname('DOUBLE12').asfloat/100); //부가세

    XL.Cells[ir, 5].Value := (Fieldbyname('DOUBLE13').asfloat/100); //합계

    XL.Cells[ir, 6].Value := (Fieldbyname('DOUBLE14').asfloat/100); //자동할인

    XL.Cells[ir, 7].Value := (Fieldbyname('DOUBLE15').asfloat/100); //연체료

    XL.Cells[ir, 8].Value := (Fieldbyname('DOUBLE16').asfloat/100); //Mybill할인

    XL.Cells[ir, 9].Value := (Fieldbyname('DOUBLE17').asfloat/100); //총합계

    end

    else begin

    XL.Cells[ir, 3].Value := Fieldbyname('DOUBLE11').asfloat; //공급가액

    XL.Cells[ir, 4].Value := Fieldbyname('DOUBLE12').asfloat; //부가세

    XL.Cells[ir, 5].Value := Fieldbyname('DOUBLE13').asfloat; //합계

    XL.Cells[ir, 6].Value := Fieldbyname('DOUBLE14').asfloat; //자동할인

    XL.Cells[ir, 7].Value := Fieldbyname('DOUBLE15').asfloat; //연체료

    XL.Cells[ir, 8].Value := Fieldbyname('DOUBLE16').asfloat; //Mybill할인

    XL.Cells[ir, 9].Value := Fieldbyname('DOUBLE17').asfloat; //총합계

    end;

    next;

    inc(ir);

    inc(iCount);

    end; // end of while



    //~~~~합계 처리

    XL.Range[ XL.Cells[ir,1], XL.Cells[ir,1]].Value := '합계 ';



    sSumformula := '=SUM(R[-'+ inttostr(iCount)+']C:R[-1]C)'; //SUM 공식

    XL.Range[ XL.Cells[ir,3], XL.Cells[ir,9]].FormulaR1C1 := sSumformula;



    inc(ir);

    inc(ir);



    //두번째 서비스를 연다.

    if not fServiceCall(31) then begin

    result := False;

    XL := unassigned;

    exit;

    end;



    //Header color

    XL.Range[ XL.Cells[ir, 1], XL.Cells[ir, 6] ].Select;

    XL.Selection.Interior.ColorIndex := xlColorGray;

    XL.Selection.Interior.Pattern := xlSolid;

    //두번째 표 헤더 작성

    XL.Cells[ir, 1].Value := '구분';

    XL.Cells[ir, 2].Value := '건수';

    XL.Cells[ir, 3].Value := '공급가액';

    XL.Cells[ir, 4].Value := '부가세포함';

    XL.Cells[ir, 5].Value := '원단위절사';

    XL.Cells[ir, 6].Value := '선납대체';



    inc(ir);

    iCount := 0;

    First;

    while not EOF do begin

    XL.Cells[ir, 1].Value := Fieldbyname('STR21').asstring; //구분

    XL.Cells[ir, 2].Value := Fieldbyname('LONG11').asinteger; //건수

    if iCurrency = '달러' then begin

    XL.Cells[ir, 3].Value := (Fieldbyname('DOUBLE11').asfloat/100); //공급가액

    XL.Cells[ir, 4].Value := (Fieldbyname('DOUBLE12').asfloat/100); //부가세포함

    XL.Cells[ir, 5].Value := (Fieldbyname('DOUBLE13').asfloat/100); //원단위절사

    XL.Cells[ir, 6].Value := (Fieldbyname('DOUBLE14').asfloat/100); //선납대체

    end

    else begin

    XL.Cells[ir, 3].Value := Fieldbyname('DOUBLE11').asfloat; //공급가액

    XL.Cells[ir, 4].Value := Fieldbyname('DOUBLE12').asfloat; //부가세포함

    XL.Cells[ir, 5].Value := Fieldbyname('DOUBLE13').asfloat; //원단위절사

    XL.Cells[ir, 6].Value := Fieldbyname('DOUBLE14').asfloat; //선납대체

    end;

    next;

    inc(ir);

    inc(iCount);

    end; // end of while



    //~~~~합계 처리

    XL.Range[ XL.Cells[ir,1], XL.Cells[ir,1]].Value := '합계 ';



    sSumformula := '=SUM(R[-'+ inttostr(iCount)+']C:R[-1]C)'; //SUM 공식

    XL.Range[ XL.Cells[ir,2], XL.Cells[ir,6]].FormulaR1C1 := sSumformula;



    inc(ir);

    inc(ir);



    //세번째 서비스를 연다.

    if not fServiceCall(32) then begin

    result := False;

    XL := unassigned;

    exit;

    end;



    //Header color

    XL.Range[ XL.Cells[ir, 1], XL.Cells[ir, 9] ].Select;

    XL.Selection.Interior.ColorIndex := xlColorGray;

    XL.Selection.Interior.Pattern := xlSolid;

    //세번째 표 헤더 작성

    XL.Cells[ir, 1].Value := '청구종류';

    XL.Cells[ir, 2].Value := '납입자수';

    XL.Cells[ir, 3].Value := '매출액';

    XL.Cells[ir, 4].Value := '부가세';

    XL.Cells[ir, 5].Value := '청구금액';

    XL.Cells[ir, 6].Value := '환급금액';

    XL.Cells[ir, 7].Value := '절사액';

    XL.Cells[ir, 8].Value := '선납대체';

    XL.Cells[ir, 9].Value := '청구서금액';



    inc(ir);

    First;

    while not EOF do begin

    XL.Cells[ir, 1].Value := Fieldbyname('STR21').asstring; //청구종류

    XL.Cells[ir, 2].Value := Fieldbyname('LONG11').asinteger; //납입자수

    if iCurrency = '달러' then begin

    XL.Cells[ir, 3].Value := (Fieldbyname('DOUBLE11').asfloat/100); //매출액

    XL.Cells[ir, 4].Value := (Fieldbyname('DOUBLE12').asfloat/100); //부가세

    XL.Cells[ir, 5].Value := (Fieldbyname('DOUBLE13').asfloat/100); //청구금액

    XL.Cells[ir, 6].Value := (Fieldbyname('DOUBLE14').asfloat/100); //환급금액

    XL.Cells[ir, 7].Value := (Fieldbyname('DOUBLE15').asfloat/100); //절사액

    XL.Cells[ir, 8].Value := (Fieldbyname('DOUBLE16').asfloat/100); //선납대체

    XL.Cells[ir, 9].Value := (Fieldbyname('DOUBLE17').asfloat/100); //청구서금액

    end

    else begin

    XL.Cells[ir, 3].Value := Fieldbyname('DOUBLE11').asfloat; //매출액

    XL.Cells[ir, 4].Value := Fieldbyname('DOUBLE12').asfloat; //부가세

    XL.Cells[ir, 5].Value := Fieldbyname('DOUBLE13').asfloat; //청구금액

    XL.Cells[ir, 6].Value := Fieldbyname('DOUBLE14').asfloat; //환급금액

    XL.Cells[ir, 7].Value := Fieldbyname('DOUBLE15').asfloat; //절사액

    XL.Cells[ir, 8].Value := Fieldbyname('DOUBLE16').asfloat; //선납대체

    XL.Cells[ir, 9].Value := Fieldbyname('DOUBLE17').asfloat; //청구서금액

    end;

    next;

    inc(ir);

    end; // end of while



    //엑셀 서식지정

    for j:=0 to 0 do begin

    XL.Columns[j+1].Select;

    XL.Selection.NumberFormatLocal := '@';

    end;

    for j:=1 to 8 do begin

    XL.Columns[j+1].Select;

    if iCurrency = '달러' then

    XL.Selection.NumberFormatLocal := '#,##0.00_ ;[빨강]-#,##0.00 '

    else XL.Selection.NumberFormatLocal := '#,##0_ ;[빨강]-#,##0 ';

    end;



    //셀 크기 조정

    XL.Range[ XL.Cells[1, 1], XL.Cells[ir, 9]].Select;

    XL.Selection.Columns.AutoFit;



    //마무리

    EnableControls;

    XL.ActiveSheet.Range['A1'].Select;

    XL.Visible := True;



    XL := unassigned;



    except

    Application.MessageBox('Excel처리중 오류가 발생 했습니다. ',

    '오류', MB_ICONERROR);

    result := False;

    XL := unassigned;

    exit;

    end; // end of try

    end; // end of with

    end;







  • Profile
    cjw 2001.08.13 19:31
    박장용 wrote:

    > cjw wrote:

    > > DB그리드의 데이타를 엑셀로 저장하는데 엑셀을 각 셀의 폭을 변경하여

    > > 저장할려고 합니다.

    > > 가능한지요?

    > > 가능합니다.. autofit을 사용하시면요..

    > XL.Selection.Columns.AutoFit;

    > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    > 샘플을 올립니다.. 적당히 수정하셔서 사용하세요..

    >

    > function TFormDbri6301.fExcelSimpleConvThree(sSheetName:string;

    > iCurrency :string):boolean;

    > var XL : Variant;

    > ir, j : Integer;

    > iCount : integer;

    > sSumformula : string;

    > begin

    > result := True;

    >

    > //엑셀

    > try

    > XL := CreateOLEObject('Excel.Application');

    > except

    > Application.MessageBox('Excel이 설치되어 있지 않습니다.',

    > '정보', MB_ICONINFORMATION);

    > result := False;

    > Exit;

    > end;

    >

    > with txsBT_DBRI6300 do begin

    > DisableControls;

    >

    > try

    > XL.WorkBooks.Add; //페이지 생성

    > XL.ActiveSheet.Name := sSheetName; //쉬트명 지정

    > ir := 1; //현재 엑셀의 Row수

    >

    > //검색조건 출력

    > XL.Cells[ ir, 1].Value := '청구년월 : ' + mpickerSalesDate3.GetFormatText;

    > inc(ir);

    > XL.Cells[ ir, 1].Value := '통화구분 : ' + cboxCurrency3.Text;

    > inc(ir);

    > if rbtn31.Checked then XL.Cells[ir,1] := '구 분 : 청구분'

    > else XL.Cells[ir,1] := '구 분 : 확정분';

    > inc(ir);

    >

    > //첫번째 서비스를 연다.

    > if not fServiceCall(3) then begin

    > result := False;

    > XL := unassigned;

    > exit;

    > end;

    > //Header color

    > XL.Range[ XL.Cells[ir, 1], XL.Cells[ir, 9] ].Select;

    > XL.Selection.Interior.ColorIndex := xlColorGray;

    > XL.Selection.Interior.Pattern := xlSolid;

    > //첫번째 표 헤더 작성

    > XL.Cells[ir, 1].Value := '조정구분';

    > XL.Cells[ir, 2].Value := '매출환급구분';

    > XL.Cells[ir, 3].Value := '공급가액';

    > XL.Cells[ir, 4].Value := '부가세';

    > XL.Cells[ir, 5].Value := '합계';

    > XL.Cells[ir, 6].Value := '자동할인';

    > XL.Cells[ir, 7].Value := '연체료';

    > XL.Cells[ir, 8].Value := 'myBill할인';

    > XL.Cells[ir, 9].Value := '총합계';

    >

    > inc(ir);

    >

    > iCount := 0;

    > First;

    > while not EOF do begin

    > XL.Cells[ir, 1].Value := Fieldbyname('STR21').asstring; //조정구분

    > XL.Cells[ir, 2].Value := '매출'; //Fieldbyname('STR22').asstring; //매출환급구분

    > //현재는 매출밖에 없슴.. 추후 추가

    > if iCurrency = '달러' then begin

    > XL.Cells[ir, 3].Value := (Fieldbyname('DOUBLE11').asfloat/100); //공급가액

    > XL.Cells[ir, 4].Value := (Fieldbyname('DOUBLE12').asfloat/100); //부가세

    > XL.Cells[ir, 5].Value := (Fieldbyname('DOUBLE13').asfloat/100); //합계

    > XL.Cells[ir, 6].Value := (Fieldbyname('DOUBLE14').asfloat/100); //자동할인

    > XL.Cells[ir, 7].Value := (Fieldbyname('DOUBLE15').asfloat/100); //연체료

    > XL.Cells[ir, 8].Value := (Fieldbyname('DOUBLE16').asfloat/100); //Mybill할인

    > XL.Cells[ir, 9].Value := (Fieldbyname('DOUBLE17').asfloat/100); //총합계

    > end

    > else begin

    > XL.Cells[ir, 3].Value := Fieldbyname('DOUBLE11').asfloat; //공급가액

    > XL.Cells[ir, 4].Value := Fieldbyname('DOUBLE12').asfloat; //부가세

    > XL.Cells[ir, 5].Value := Fieldbyname('DOUBLE13').asfloat; //합계

    > XL.Cells[ir, 6].Value := Fieldbyname('DOUBLE14').asfloat; //자동할인

    > XL.Cells[ir, 7].Value := Fieldbyname('DOUBLE15').asfloat; //연체료

    > XL.Cells[ir, 8].Value := Fieldbyname('DOUBLE16').asfloat; //Mybill할인

    > XL.Cells[ir, 9].Value := Fieldbyname('DOUBLE17').asfloat; //총합계

    > end;

    > next;

    > inc(ir);

    > inc(iCount);

    > end; // end of while

    >

    > //~~~~합계 처리

    > XL.Range[ XL.Cells[ir,1], XL.Cells[ir,1]].Value := '합계 ';

    >

    > sSumformula := '=SUM(R[-'+ inttostr(iCount)+']C:R[-1]C)'; //SUM 공식

    > XL.Range[ XL.Cells[ir,3], XL.Cells[ir,9]].FormulaR1C1 := sSumformula;

    >

    > inc(ir);

    > inc(ir);

    >

    > //두번째 서비스를 연다.

    > if not fServiceCall(31) then begin

    > result := False;

    > XL := unassigned;

    > exit;

    > end;

    >

    > //Header color

    > XL.Range[ XL.Cells[ir, 1], XL.Cells[ir, 6] ].Select;

    > XL.Selection.Interior.ColorIndex := xlColorGray;

    > XL.Selection.Interior.Pattern := xlSolid;

    > //두번째 표 헤더 작성

    > XL.Cells[ir, 1].Value := '구분';

    > XL.Cells[ir, 2].Value := '건수';

    > XL.Cells[ir, 3].Value := '공급가액';

    > XL.Cells[ir, 4].Value := '부가세포함';

    > XL.Cells[ir, 5].Value := '원단위절사';

    > XL.Cells[ir, 6].Value := '선납대체';

    >

    > inc(ir);

    > iCount := 0;

    > First;

    > while not EOF do begin

    > XL.Cells[ir, 1].Value := Fieldbyname('STR21').asstring; //구분

    > XL.Cells[ir, 2].Value := Fieldbyname('LONG11').asinteger; //건수

    > if iCurrency = '달러' then begin

    > XL.Cells[ir, 3].Value := (Fieldbyname('DOUBLE11').asfloat/100); //공급가액

    > XL.Cells[ir, 4].Value := (Fieldbyname('DOUBLE12').asfloat/100); //부가세포함

    > XL.Cells[ir, 5].Value := (Fieldbyname('DOUBLE13').asfloat/100); //원단위절사

    > XL.Cells[ir, 6].Value := (Fieldbyname('DOUBLE14').asfloat/100); //선납대체

    > end

    > else begin

    > XL.Cells[ir, 3].Value := Fieldbyname('DOUBLE11').asfloat; //공급가액

    > XL.Cells[ir, 4].Value := Fieldbyname('DOUBLE12').asfloat; //부가세포함

    > XL.Cells[ir, 5].Value := Fieldbyname('DOUBLE13').asfloat; //원단위절사

    > XL.Cells[ir, 6].Value := Fieldbyname('DOUBLE14').asfloat; //선납대체

    > end;

    > next;

    > inc(ir);

    > inc(iCount);

    > end; // end of while

    >

    > //~~~~합계 처리

    > XL.Range[ XL.Cells[ir,1], XL.Cells[ir,1]].Value := '합계 ';

    >

    > sSumformula := '=SUM(R[-'+ inttostr(iCount)+']C:R[-1]C)'; //SUM 공식

    > XL.Range[ XL.Cells[ir,2], XL.Cells[ir,6]].FormulaR1C1 := sSumformula;

    >

    > inc(ir);

    > inc(ir);

    >

    > //세번째 서비스를 연다.

    > if not fServiceCall(32) then begin

    > result := False;

    > XL := unassigned;

    > exit;

    > end;

    >

    > //Header color

    > XL.Range[ XL.Cells[ir, 1], XL.Cells[ir, 9] ].Select;

    > XL.Selection.Interior.ColorIndex := xlColorGray;

    > XL.Selection.Interior.Pattern := xlSolid;

    > //세번째 표 헤더 작성

    > XL.Cells[ir, 1].Value := '청구종류';

    > XL.Cells[ir, 2].Value := '납입자수';

    > XL.Cells[ir, 3].Value := '매출액';

    > XL.Cells[ir, 4].Value := '부가세';

    > XL.Cells[ir, 5].Value := '청구금액';

    > XL.Cells[ir, 6].Value := '환급금액';

    > XL.Cells[ir, 7].Value := '절사액';

    > XL.Cells[ir, 8].Value := '선납대체';

    > XL.Cells[ir, 9].Value := '청구서금액';

    >

    > inc(ir);

    > First;

    > while not EOF do begin

    > XL.Cells[ir, 1].Value := Fieldbyname('STR21').asstring; //청구종류

    > XL.Cells[ir, 2].Value := Fieldbyname('LONG11').asinteger; //납입자수

    > if iCurrency = '달러' then begin

    > XL.Cells[ir, 3].Value := (Fieldbyname('DOUBLE11').asfloat/100); //매출액

    > XL.Cells[ir, 4].Value := (Fieldbyname('DOUBLE12').asfloat/100); //부가세

    > XL.Cells[ir, 5].Value := (Fieldbyname('DOUBLE13').asfloat/100); //청구금액

    > XL.Cells[ir, 6].Value := (Fieldbyname('DOUBLE14').asfloat/100); //환급금액

    > XL.Cells[ir, 7].Value := (Fieldbyname('DOUBLE15').asfloat/100); //절사액

    > XL.Cells[ir, 8].Value := (Fieldbyname('DOUBLE16').asfloat/100); //선납대체

    > XL.Cells[ir, 9].Value := (Fieldbyname('DOUBLE17').asfloat/100); //청구서금액

    > end

    > else begin

    > XL.Cells[ir, 3].Value := Fieldbyname('DOUBLE11').asfloat; //매출액

    > XL.Cells[ir, 4].Value := Fieldbyname('DOUBLE12').asfloat; //부가세

    > XL.Cells[ir, 5].Value := Fieldbyname('DOUBLE13').asfloat; //청구금액

    > XL.Cells[ir, 6].Value := Fieldbyname('DOUBLE14').asfloat; //환급금액

    > XL.Cells[ir, 7].Value := Fieldbyname('DOUBLE15').asfloat; //절사액

    > XL.Cells[ir, 8].Value := Fieldbyname('DOUBLE16').asfloat; //선납대체

    > XL.Cells[ir, 9].Value := Fieldbyname('DOUBLE17').asfloat; //청구서금액

    > end;

    > next;

    > inc(ir);

    > end; // end of while

    >

    > //엑셀 서식지정

    > for j:=0 to 0 do begin

    > XL.Columns[j+1].Select;

    > XL.Selection.NumberFormatLocal := '@';

    > end;

    > for j:=1 to 8 do begin

    > XL.Columns[j+1].Select;

    > if iCurrency = '달러' then

    > XL.Selection.NumberFormatLocal := '#,##0.00_ ;[빨강]-#,##0.00 '

    > else XL.Selection.NumberFormatLocal := '#,##0_ ;[빨강]-#,##0 ';

    > end;

    >

    > //셀 크기 조정

    > XL.Range[ XL.Cells[1, 1], XL.Cells[ir, 9]].Select;

    > XL.Selection.Columns.AutoFit;

    >

    > //마무리

    > EnableControls;

    > XL.ActiveSheet.Range['A1'].Select;

    > XL.Visible := True;

    >

    > XL := unassigned;

    >

    > except

    > Application.MessageBox('Excel처리중 오류가 발생 했습니다. ',

    > '오류', MB_ICONERROR);

    > result := False;

    > XL := unassigned;

    > exit;

    > end; // end of try

    > end; // end of with

    > end;

    >



    장용님 답변 정말 감사합니다.

    답변 주신대로 'AutoFit'을 사용하니 셀폭이 자동적으로 조절됩니다.

    그런데요. 엑셀상에서는 제대로 표시가 됩니다만, 인쇄할려고 미리보기를 하면

    문자열이 제대로 표시가 안되고 '#######'표시되는 셀들이 있어 다시 조정을 해야

    됩니다. 죄송합니다만 다음 2가지만 더 질문 드립니다.



    1. 각 열의 폭을 각각 지정할수는 없는지요?

    XL.Cells[ir, 1].Width := 50;

    XL.Cells[ir, 2].Width := 100;

    XL.Cells[ir, 3].Width := 150; 식으로요.



    2. 헤더부에 정렬을 하고 싶은데 어떻게 하면 되는지요?

    이상 답변 부탁드립니다.

    그럼, 수고하세요.

  • Profile
    Superdev 2001.11.02 01:42
    cjw wrote:

    > 박장용 wrote:

    > > cjw wrote:

    > > > DB그리드의 데이타를 엑셀로 저장하는데 엑셀을 각 셀의 폭을 변경하여

    > > > 저장할려고 합니다.

    > > > 가능한지요?

    > > > 가능합니다.. autofit을 사용하시면요..

    > > XL.Selection.Columns.AutoFit;

    > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    > > 샘플을 올립니다.. 적당히 수정하셔서 사용하세요..

    > >

    > > function TFormDbri6301.fExcelSimpleConvThree(sSheetName:string;

    > > iCurrency :string):boolean;

    > > var XL : Variant;

    > > ir, j : Integer;

    > > iCount : integer;

    > > sSumformula : string;

    > > begin

    > > result := True;

    > >

    > > //엑셀

    > > try

    > > XL := CreateOLEObject('Excel.Application');

    > > except

    > > Application.MessageBox('Excel이 설치되어 있지 않습니다.',

    > > '정보', MB_ICONINFORMATION);

    > > result := False;

    > > Exit;

    > > end;

    > >

    > > with txsBT_DBRI6300 do begin

    > > DisableControls;

    > >

    > > try

    > > XL.WorkBooks.Add; //페이지 생성

    > > XL.ActiveSheet.Name := sSheetName; //쉬트명 지정

    > > ir := 1; //현재 엑셀의 Row수

    > >

    > > //검색조건 출력

    > > XL.Cells[ ir, 1].Value := '청구년월 : ' + mpickerSalesDate3.GetFormatText;

    > > inc(ir);

    > > XL.Cells[ ir, 1].Value := '통화구분 : ' + cboxCurrency3.Text;

    > > inc(ir);

    > > if rbtn31.Checked then XL.Cells[ir,1] := '구 분 : 청구분'

    > > else XL.Cells[ir,1] := '구 분 : 확정분';

    > > inc(ir);

    > >

    > > //첫번째 서비스를 연다.

    > > if not fServiceCall(3) then begin

    > > result := False;

    > > XL := unassigned;

    > > exit;

    > > end;

    > > //Header color

    > > XL.Range[ XL.Cells[ir, 1], XL.Cells[ir, 9] ].Select;

    > > XL.Selection.Interior.ColorIndex := xlColorGray;

    > > XL.Selection.Interior.Pattern := xlSolid;

    > > //첫번째 표 헤더 작성

    > > XL.Cells[ir, 1].Value := '조정구분';

    > > XL.Cells[ir, 2].Value := '매출환급구분';

    > > XL.Cells[ir, 3].Value := '공급가액';

    > > XL.Cells[ir, 4].Value := '부가세';

    > > XL.Cells[ir, 5].Value := '합계';

    > > XL.Cells[ir, 6].Value := '자동할인';

    > > XL.Cells[ir, 7].Value := '연체료';

    > > XL.Cells[ir, 8].Value := 'myBill할인';

    > > XL.Cells[ir, 9].Value := '총합계';

    > >

    > > inc(ir);

    > >

    > > iCount := 0;

    > > First;

    > > while not EOF do begin

    > > XL.Cells[ir, 1].Value := Fieldbyname('STR21').asstring; //조정구분

    > > XL.Cells[ir, 2].Value := '매출'; //Fieldbyname('STR22').asstring; //매출환급구분

    > > //현재는 매출밖에 없슴.. 추후 추가

    > > if iCurrency = '달러' then begin

    > > XL.Cells[ir, 3].Value := (Fieldbyname('DOUBLE11').asfloat/100); //공급가액

    > > XL.Cells[ir, 4].Value := (Fieldbyname('DOUBLE12').asfloat/100); //부가세

    > > XL.Cells[ir, 5].Value := (Fieldbyname('DOUBLE13').asfloat/100); //합계

    > > XL.Cells[ir, 6].Value := (Fieldbyname('DOUBLE14').asfloat/100); //자동할인

    > > XL.Cells[ir, 7].Value := (Fieldbyname('DOUBLE15').asfloat/100); //연체료

    > > XL.Cells[ir, 8].Value := (Fieldbyname('DOUBLE16').asfloat/100); //Mybill할인

    > > XL.Cells[ir, 9].Value := (Fieldbyname('DOUBLE17').asfloat/100); //총합계

    > > end

    > > else begin

    > > XL.Cells[ir, 3].Value := Fieldbyname('DOUBLE11').asfloat; //공급가액

    > > XL.Cells[ir, 4].Value := Fieldbyname('DOUBLE12').asfloat; //부가세

    > > XL.Cells[ir, 5].Value := Fieldbyname('DOUBLE13').asfloat; //합계

    > > XL.Cells[ir, 6].Value := Fieldbyname('DOUBLE14').asfloat; //자동할인

    > > XL.Cells[ir, 7].Value := Fieldbyname('DOUBLE15').asfloat; //연체료

    > > XL.Cells[ir, 8].Value := Fieldbyname('DOUBLE16').asfloat; //Mybill할인

    > > XL.Cells[ir, 9].Value := Fieldbyname('DOUBLE17').asfloat; //총합계

    > > end;

    > > next;

    > > inc(ir);

    > > inc(iCount);

    > > end; // end of while

    > >

    > > //~~~~합계 처리

    > > XL.Range[ XL.Cells[ir,1], XL.Cells[ir,1]].Value := '합계 ';

    > >

    > > sSumformula := '=SUM(R[-'+ inttostr(iCount)+']C:R[-1]C)'; //SUM 공식

    > > XL.Range[ XL.Cells[ir,3], XL.Cells[ir,9]].FormulaR1C1 := sSumformula;

    > >

    > > inc(ir);

    > > inc(ir);

    > >

    > > //두번째 서비스를 연다.

    > > if not fServiceCall(31) then begin

    > > result := False;

    > > XL := unassigned;

    > > exit;

    > > end;

    > >

    > > //Header color

    > > XL.Range[ XL.Cells[ir, 1], XL.Cells[ir, 6] ].Select;

    > > XL.Selection.Interior.ColorIndex := xlColorGray;

    > > XL.Selection.Interior.Pattern := xlSolid;

    > > //두번째 표 헤더 작성

    > > XL.Cells[ir, 1].Value := '구분';

    > > XL.Cells[ir, 2].Value := '건수';

    > > XL.Cells[ir, 3].Value := '공급가액';

    > > XL.Cells[ir, 4].Value := '부가세포함';

    > > XL.Cells[ir, 5].Value := '원단위절사';

    > > XL.Cells[ir, 6].Value := '선납대체';

    > >

    > > inc(ir);

    > > iCount := 0;

    > > First;

    > > while not EOF do begin

    > > XL.Cells[ir, 1].Value := Fieldbyname('STR21').asstring; //구분

    > > XL.Cells[ir, 2].Value := Fieldbyname('LONG11').asinteger; //건수

    > > if iCurrency = '달러' then begin

    > > XL.Cells[ir, 3].Value := (Fieldbyname('DOUBLE11').asfloat/100); //공급가액

    > > XL.Cells[ir, 4].Value := (Fieldbyname('DOUBLE12').asfloat/100); //부가세포함

    > > XL.Cells[ir, 5].Value := (Fieldbyname('DOUBLE13').asfloat/100); //원단위절사

    > > XL.Cells[ir, 6].Value := (Fieldbyname('DOUBLE14').asfloat/100); //선납대체

    > > end

    > > else begin

    > > XL.Cells[ir, 3].Value := Fieldbyname('DOUBLE11').asfloat; //공급가액

    > > XL.Cells[ir, 4].Value := Fieldbyname('DOUBLE12').asfloat; //부가세포함

    > > XL.Cells[ir, 5].Value := Fieldbyname('DOUBLE13').asfloat; //원단위절사

    > > XL.Cells[ir, 6].Value := Fieldbyname('DOUBLE14').asfloat; //선납대체

    > > end;

    > > next;

    > > inc(ir);

    > > inc(iCount);

    > > end; // end of while

    > >

    > > //~~~~합계 처리

    > > XL.Range[ XL.Cells[ir,1], XL.Cells[ir,1]].Value := '합계 ';

    > >

    > > sSumformula := '=SUM(R[-'+ inttostr(iCount)+']C:R[-1]C)'; //SUM 공식

    > > XL.Range[ XL.Cells[ir,2], XL.Cells[ir,6]].FormulaR1C1 := sSumformula;

    > >

    > > inc(ir);

    > > inc(ir);

    > >

    > > //세번째 서비스를 연다.

    > > if not fServiceCall(32) then begin

    > > result := False;

    > > XL := unassigned;

    > > exit;

    > > end;

    > >

    > > //Header color

    > > XL.Range[ XL.Cells[ir, 1], XL.Cells[ir, 9] ].Select;

    > > XL.Selection.Interior.ColorIndex := xlColorGray;

    > > XL.Selection.Interior.Pattern := xlSolid;

    > > //세번째 표 헤더 작성

    > > XL.Cells[ir, 1].Value := '청구종류';

    > > XL.Cells[ir, 2].Value := '납입자수';

    > > XL.Cells[ir, 3].Value := '매출액';

    > > XL.Cells[ir, 4].Value := '부가세';

    > > XL.Cells[ir, 5].Value := '청구금액';

    > > XL.Cells[ir, 6].Value := '환급금액';

    > > XL.Cells[ir, 7].Value := '절사액';

    > > XL.Cells[ir, 8].Value := '선납대체';

    > > XL.Cells[ir, 9].Value := '청구서금액';

    > >

    > > inc(ir);

    > > First;

    > > while not EOF do begin

    > > XL.Cells[ir, 1].Value := Fieldbyname('STR21').asstring; //청구종류

    > > XL.Cells[ir, 2].Value := Fieldbyname('LONG11').asinteger; //납입자수

    > > if iCurrency = '달러' then begin

    > > XL.Cells[ir, 3].Value := (Fieldbyname('DOUBLE11').asfloat/100); //매출액

    > > XL.Cells[ir, 4].Value := (Fieldbyname('DOUBLE12').asfloat/100); //부가세

    > > XL.Cells[ir, 5].Value := (Fieldbyname('DOUBLE13').asfloat/100); //청구금액

    > > XL.Cells[ir, 6].Value := (Fieldbyname('DOUBLE14').asfloat/100); //환급금액

    > > XL.Cells[ir, 7].Value := (Fieldbyname('DOUBLE15').asfloat/100); //절사액

    > > XL.Cells[ir, 8].Value := (Fieldbyname('DOUBLE16').asfloat/100); //선납대체

    > > XL.Cells[ir, 9].Value := (Fieldbyname('DOUBLE17').asfloat/100); //청구서금액

    > > end

    > > else begin

    > > XL.Cells[ir, 3].Value := Fieldbyname('DOUBLE11').asfloat; //매출액

    > > XL.Cells[ir, 4].Value := Fieldbyname('DOUBLE12').asfloat; //부가세

    > > XL.Cells[ir, 5].Value := Fieldbyname('DOUBLE13').asfloat; //청구금액

    > > XL.Cells[ir, 6].Value := Fieldbyname('DOUBLE14').asfloat; //환급금액

    > > XL.Cells[ir, 7].Value := Fieldbyname('DOUBLE15').asfloat; //절사액

    > > XL.Cells[ir, 8].Value := Fieldbyname('DOUBLE16').asfloat; //선납대체

    > > XL.Cells[ir, 9].Value := Fieldbyname('DOUBLE17').asfloat; //청구서금액

    > > end;

    > > next;

    > > inc(ir);

    > > end; // end of while

    > >

    > > //엑셀 서식지정

    > > for j:=0 to 0 do begin

    > > XL.Columns[j+1].Select;

    > > XL.Selection.NumberFormatLocal := '@';

    > > end;

    > > for j:=1 to 8 do begin

    > > XL.Columns[j+1].Select;

    > > if iCurrency = '달러' then

    > > XL.Selection.NumberFormatLocal := '#,##0.00_ ;[빨강]-#,##0.00 '

    > > else XL.Selection.NumberFormatLocal := '#,##0_ ;[빨강]-#,##0 ';

    > > end;

    > >

    > > //셀 크기 조정

    > > XL.Range[ XL.Cells[1, 1], XL.Cells[ir, 9]].Select;

    > > XL.Selection.Columns.AutoFit;

    > >

    > > //마무리

    > > EnableControls;

    > > XL.ActiveSheet.Range['A1'].Select;

    > > XL.Visible := True;

    > >

    > > XL := unassigned;

    > >

    > > except

    > > Application.MessageBox('Excel처리중 오류가 발생 했습니다. ',

    > > '오류', MB_ICONERROR);

    > > result := False;

    > > XL := unassigned;

    > > exit;

    > > end; // end of try

    > > end; // end of with

    > > end;

    > >

    >

    > 장용님 답변 정말 감사합니다.

    > 답변 주신대로 'AutoFit'을 사용하니 셀폭이 자동적으로 조절됩니다.

    > 그런데요. 엑셀상에서는 제대로 표시가 됩니다만, 인쇄할려고 미리보기를 하면

    > 문자열이 제대로 표시가 안되고 '#######'표시되는 셀들이 있어 다시 조정을 해야

    > 됩니다. 죄송합니다만 다음 2가지만 더 질문 드립니다.

    >

    > 1. 각 열의 폭을 각각 지정할수는 없는지요?

    > XL.Cells[ir, 1].Width := 50;

    > XL.Cells[ir, 2].Width := 100;

    > XL.Cells[ir, 3].Width := 150; 식으로요.

    >

    > 2. 헤더부에 정렬을 하고 싶은데 어떻게 하면 되는지요?

    > 이상 답변 부탁드립니다.

    > 그럼, 수고하세요.



    WS.Range['E2','E2'].ColumnWidth := 40;

    E2 <- 암꺼나 같은 숫자로 하면 되네여.. Column은 숫자가 변해도 같은 Column이니깐.

    쩝!