블로그 이미지
자료에 문제가 있을 경우, 확인하는대로 삭제처리 하겠습니다. 즐거운 하루 되시길...
05-04 06:48
Total
Today
Yesterday

카테고리

분류 전체보기 (199)
이야기방 (20)
공부방 (173)
개발노트&관련잡다구니 (6)




uses

  ComObj;

var

  ExcelApp: OleVariant;

implementation


procedure TForm1.Button1Click(Sender: TObject);

const

  // SheetType

  xlChart = -4109;

  xlWorksheet = -4167;

  // WBATemplate

  xlWBATWorksheet = -4167;

  xlWBATChart = -4109;

  // Page Setup

  xlPortrait = 1;

  xlLandscape = 2;

  xlPaperA4 = 9;

  // Format Cells

  xlBottom = -4107;

  xlLeft = -4131;

  xlRight = -4152;

  xlTop = -4160;

  // Text Alignment

  xlHAlignCenter = -4108;

  xlVAlignCenter = -4108;

  // Cell Borders

  xlThick = 4;

  xlThin = 2;

var

  ColumnRange: OleVariant;

  // Function to get the number of Rows in a Certain column

  function GetLastLine(AColumn: Integer): Integer;

  const

    xlUp = 3;

  begin

    Result := ExcelApp.Range[Char(96 + AColumn) + IntToStr(65536)].end[xlUp].Rows.Row;

  end;

begin

  { Start Excel }

  // By using GetActiveOleObject, you use an instance of Word that's already running,

  // if there is one.

  try

    ExcelApp := GetActiveOleObject('Excel.Application');

  except

    try

      ExcelApp := GetActiveOleObject('scalc.Application');

    except  

      try

        // If no instance of Word is running, try to Create a new Excel Object

        ExcelApp := CreateOleObject('Excel.Application');  

      except

        try

          // If no instance of Word is running, try to Create a new scalc Object

          ExcelApp := CreateOleObject('scalc.Application');  

        except

          ShowMessage('Cannot start Excel/Excel not installed ?');

          Exit;

        end;                

      end;    

    end;

  end;

  // Add a new Workbook, Neue Arbeitsmappe offnen

  ExcelApp.Workbooks.Add(xlWBatWorkSheet);

  // Open a Workbook, Arbeitsmappe offnen

  ExcelApp.Workbooks.Open('c:\YourFileName.xls');


  // Rename the active Sheet

  ExcelApp.ActiveSheet.Name := 'This is Sheet 1';

  // Rename

  ExcelApp.Workbooks[1].WorkSheets[1].Name := 'This is Sheet 1';

  // Insert some Text in some Cells[Row,Col]

  ExcelApp.Cells[1, 1].Value := 'SwissDelphiCenter.ch';

  ExcelApp.Cells[2, 1].Value := 'http://www.swissdelphicenter.ch';

  ExcelApp.Cells[3, 1].Value := FormatDateTime('dd-mmm-yyyy', Now);

  // Setting a row of data with one call

  ExcelApp.Range['A2', 'D2'].Value := VarArrayOf([1, 10, 100, 1000]);

  // Setting a formula

  ExcelApp.Range['A11', 'A11'].Formula := '=Sum(A1:A10)';

  // Change Cell Alignement

  ExcelApp.Cells[2, 1].HorizontalAlignment := xlright;

  // Change the Column Width.

  ColumnRange := ExcelApp.Workbooks[1].WorkSheets[1].Columns;

  ColumnRange.Columns[1].ColumnWidth := 20;

  ColumnRange.Columns[2].ColumnWidth := 40;

  // Change Rowheight / Zeilenhohe andern:

  ExcelApp.Rows[1].RowHeight := 15.75;

  // Merge cells, Zellen verbinden:

  ExcelApp.Range['B3:D3'].Mergecells := True;

  // Apply borders to cells, Zellen umrahmen:

  ExcelApp.Range['A14:M14'].Borders.Weight := xlThick; // Think line/ Dicke Linie

  ExcelApp.Range['A14:M14'].Borders.Weight := xlThin;  // Thin line Dunne Linie

  // Set Bold Font in cells, Fettdruck in den Zellen

  ExcelApp.Range['B16:M26'].Font.Bold := True;

  // Set Font Size, Schriftgroße setzen

  ExcelApp.Range['B16:M26'].Font.Size := 12;

  //right-aligned Text, rechtsbundige Textausrichtung

  ExcelApp.Cells[9, 6].HorizontalAlignment := xlright;

  // horizontal-aligned text, horizontale Zentrierung

  ExcelApp.Range['B14:M26'].HorizontalAlignment := xlHAlignCenter;

  // left-aligned Text, vertikale Zentrierung

  ExcelApp.Range['B14:M26'].VerticallyAlignment := xlVAlignCenter;


  { Page Setup }

  ExcelApp.ActiveSheet.PageSetup.Orientation := xlLandscape;

  // Left, Right Margin (Seitenrander)

  ExcelApp.ActiveSheet.PageSetup.LeftMargin  := 35;

  ExcelApp.ActiveSheet.PageSetup.RightMargin := -15;

  // Set Footer Margin

  ExcelApp.ActiveSheet.PageSetup.FooterMargin := ExcelApp.InchesToPoints(0);

  // Fit to X page(s) wide by Y tall

  ExcelApp.ActiveSheet.PageSetup.FitToPagesWide := 1;  // Y

  ExcelApp.ActiveSheet.PageSetup.FitToPagesTall := 3; // Y

  // Zoom

  ExcelApp.ActiveSheet.PageSetup.Zoom := 95;

  // Set Paper Size:

  ExcelApp.PageSetup.PaperSize := xlPaperA4;

  // Show/Hide Gridlines:

  ExcelApp.ActiveWindow.DisplayGridlines := False;

  // Set Black & White

  ExcelApp.ActiveSheet.PageSetup.BlackAndWhite := False;

  // footers

  ExcelApp.ActiveSheet.PageSetup.RightFooter := 'Right Footer / Rechte Fußzeile';

  ExcelApp.ActiveSheet.PageSetup.LeftFooter  := 'Left Footer / Linke Fußzeile';

  // Show Excel Version:

  ShowMessage(Format('Excel Version %s: ', [ExcelApp.Version]));

  // Show Excel:

  ExcelApp.Visible := True;

  // Save the Workbook

  ExcelApp.SaveAs('c:\filename.xls');

  // Save the active Workbook:

  ExcelApp.ActiveWorkBook.SaveAs('c:\filename.xls');

end;

procedure TForm1.FormDestroy(Sender: TObject);

begin

  // Quit Excel

  if not VarIsEmpty(ExcelApp) then

  begin

    ExcelApp.DisplayAlerts := False;  // Discard unsaved files....

    ExcelApp.Quit;

  end;

end;


원본 링크 : http://seniorkr.tistory.com/41


선그리기 참고 


procedure TForm1.Button1Click(Sender: TObject);
var
  Excel: OleVariant;
  WorkBook: OleVariant;
  WorkSheet: OleVariant;

  i : integer;

Const
  xlNone = -4142;
  xlDiagonalDown = 5;
  xlDiagonalUp = 6;
  xlEdgeLeft = 7;
  xlEdgeTop = 8;
  xlEdgeBottom = 9;
  xlEdgeRight = 10;
  xlContinuous = 1;

  xlThin = 2;
  xlThick = 4;

  xlAutomatic = -4105;

begin

  Excel := CreateOleObject('Excel.Application');
  Excel.Visible := True;

  // 워크북 추가
  Excel.WorkBooks.Add;
  WorkBook := Excel.ActiveWorkBook;

  // 워크시트 추가
  Workbook.sheets.add;

  // 작업할  워크시트 선택
  WorkSheet := WorkBook.WorkSheets[1];

  // 선그리기
  WorkSheet.Range['B5:E10'].Borders[xlDiagonalDown].LineStyle := xlNone;
  WorkSheet.Range['B5:E10'].Borders[xlDiagonalUp].LineStyle := xlNone;
  WorkSheet.Range['B5:E10'].Borders[xlEdgeLeft].LineStyle := xlContinuous;
  WorkSheet.Range['B5:E10'].Borders[xlEdgeTop].LineStyle := xlContinuous;
  WorkSheet.Range['B5:E10'].Borders[xlEdgeBottom].LineStyle := xlContinuous;
  WorkSheet.Range['B5:E10'].Borders[xlEdgeBottom].Weight := xlThick;
  WorkSheet.Range['B5:E10'].Borders[xlEdgeBottom].ColorIndex := xlAutomatic;
  WorkSheet.Range['B5:E10'].Borders[xlEdgeRight].LineStyle := xlContinuous;

end;


원본 : http://wwwi.tistory.com/174


참고

http://skql.tistory.com/11

http://www.nika-soft.com/nativeexcel2/doc/brd_pls.htm



실제 적용 소스


//엑셀파일로 저장하기

procedure TfN040070_1.Btn_SaveClick(Sender: TObject);

var

  vHandle : THandle;

  vExcel  : Variant;

  vSheet  : Variant;

  Range   : Variant;

  i       : integer;

Const

  xlCenter = -4108;

  xlRight = -4152;

  xlNone = -4142;

  xlDiagonalDown = 5;

  xlDiagonalUp = 6;

  xlEdgeLeft = 7;

  xlEdgeTop = 8;

  xlEdgeBottom = 9;

  xlEdgeRight = 10;

  xlContinuous = 1;


  xlThin = 2;

  xlThick = 4;


  xlAutomatic = -4105;


begin

  vHandle := FindWindow('XLMAIN',nil);

  if vHandle <> 0 then vExcel := GetActiveOLEObject('Excel.Application')

  else

  begin

    try

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

    except

      Application.MessageBox('Excel을 열 수 없습니다.'+ char(13) +

                             'Excel이 설치되어 있는지 확인하세요!',

                             '정보', MB_OK);

    end;

  end;


  //엑셀객체 정의

  vExcel.Visible := True;                                      


  SetForegroundWindow(vHandle);

  vExcel.WorkBooks.Add;

  vExcel.WorkBooks[vExcel.WorkBooks.Count].WorkSheets[1].Name := Caption;

  vSheet := vExcel.WorkBooks[vExcel.WorkBooks.Count].WorkSheets[Caption];


  //컬럼, 로우 사이즈 변경

  vSheet.Columns[1].ColumnWidth := 35;

  vSheet.Columns[2].ColumnWidth := 20;

  vSheet.Columns[3].ColumnWidth := 10;

  vSheet.Columns[4].ColumnWidth := 25;


  vExcel.Rows[1].RowHeight := 8;

  vExcel.Rows[4].RowHeight := 8;


  //해더 설정

  vSheet.Cells[2,1].Value := 'TEST1'; vSheet.Cells[2,2].Value := Trim(B_1.Caption);

  vSheet.Cells[2,3].Value := 'TEST2'; vSheet.Cells[2,4].Value := Trim(B_2.Caption);

  vSheet.Range['A2:D2'].Borders.LineStyle := xlContinuous;


  vSheet.Cells[3,1].Value := 'TEST3'; vSheet.Cells[3,2].Value := Trim(B_3.Caption);

  vSheet.Cells[3,3].Value := 'TEST4;  vSheet.Cells[3,4].Value := Trim(B_4.Caption);

  vSheet.Range['A3:D3'].Borders.LineStyle := xlContinuous;


  //데이터 설정

  for i:= 0 to ed_contents.Lines.Count -1 do begin

    //데이터 치환_ 메모장에서 한줄씩 치환

    vSheet.Cells[5+i,1].Value := ed_contents.Lines.Strings[i];


    //테두리 라인 그리기

    vSheet.Range['A'+IntToStr(5+i)].Borders[xlEdgeLeft].LineStyle := xlContinuous; //왼쪽 라인 그리기

    vSheet.Range['D'+IntToStr(5+i)].Borders[xlEdgeRight].LineStyle:= xlContinuous; //오른쪽 라인 그리기


    if i = 0 then //첫번째줄

      vSheet.Range['A'+IntToStr(5+i), 'D'+IntToStr(5+i)].Borders[xlEdgeTop].LineStyle := xlContinuous //윗쪽 라인 그리기

    else if i = ed_contents.Lines.Count -1 then //마지막줄

      vSheet.Range['A'+IntToStr(5+i), 'D'+IntToStr(5+i)].Borders[xlEdgeBottom].LineStyle := xlContinuous; //아랫쪽 라인 그리기

  end;


  //눈금자 안보이게 설정

  vExcel.ActiveWindow.DisplayGridlines := False;

end;

Posted by 래채
, |

unit Unit2; 

interface 

uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs; 

type 
  TForm2 = class(TForm) 
    procedure FormClose(Sender: TObject; var Action: TCloseAction); 
  private 
    { Private declarations } 
  public 
    { Public declarations } 
    procedure VisibleChanging; override; // <<-- 요기
  end; 

var 
  Form2: TForm2; 

implementation 

{$R *.dfm} 

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction); 
begin 
  Action:=caHide; 
end; 

procedure TForm2.VisibleChanging;  //  <<-- 요기 
begin 
 // inherited; 
end; 

end.




원본 링크 :  http://cafe.naver.com/bcbmaster.cafe?iframe_url=/ArticleRead.nhn%3Farticleid=1437&

'공부방 > Delphi' 카테고리의 다른 글

[델파이] 엑셀 제어  (0) 2013.12.31
[델파이] 변수에 대해서  (0) 2013.11.13
[델파이] 델파이7에서 퀵레포트를 PDF로 변환하기  (0) 2010.11.26
[델파이] 클립보드 사용  (0) 2010.11.09
[델파이] 구성 파일  (0) 2010.10.27
Posted by 래채
, |

퀵레포트에서 PDF로 변환하기 위해서는 컴포넌트가 필요한데 그 이름은!!

ExportReport


압축을 풀어서 확인해보면 델파이6, 7버전의 패키지 파일이 있는데 

ExpackD7.dpk를 설치 한다 (TPDF.dpk는 설치가 안된다.. 흠냥 멀 잘못한건지 ;;)

이 컴포넌트를 설치 후에.. 아래와 같은 소스를 작성하면 된다


[퀵레포트 폼이름] := [퀵레포트 폼이름].Create(Self); //퀵레포트 폼 생성

[퀵레포트 폼이름].Prepare;  //준비

[ExportReport 개체명].Report :=  
[퀵레포트 폼이름]
 
; // 퀵레포트와 컴포넌트 개체와 연결

[ExportReport 개체명]
 
.ExportQRPDF([생성될 파일명], True); //변환형식 설정

[퀵레포트 폼이름]
 
.QRPrinter.Free;   // 해제(?)

[퀵레포트 폼이름]
 
.QRPrinter := nil;  //


ExportReport는 아래와 같은 포멧을 지원한다.

EXQR.ExportQRPDF(
[생성될 파일명]
.Text, True);     >> PDF(작은용량) 
EXQR.ExportQRPDF(
[생성될 파일명]
.Text, False);  
 
>> PDF(큰용량)  압축 차이인듯..........
EXQR.ExportQRJPG(
[생성될 파일명]
.Text);             >> JPG 변환
EXQR.ExportQRBMP(
[생성될 파일명]
.Text);            >> BMP 변환
EXQR.ExportQRWMF(
[생성될 파일명]
.Text);            
>> WMF(?) 변환
      
EXQR.ExportQREMF(
[생성될 파일명]
.Text);            
>> EMF(?) 변환

[생성될 파일명]은 확장자를 뺀 파일명으로 설정한다

Ex ) 설정 값 : [test] >>> 생성 파일명 : [test.PDF]



ExportReport는 단독으로 PDF를 생성하는 형식이 아니라 퀵레포트의 확장되는 기능으로 생각하면 된다

미리 작성된 퀵레포트를 추출해서 PDF나 JPG로 변환되는 형식이다... 


'공부방 > Delphi' 카테고리의 다른 글

[델파이] 변수에 대해서  (0) 2013.11.13
[델파이] Cannot hide and MDI Child Form  (0) 2012.02.27
[델파이] 클립보드 사용  (0) 2010.11.09
[델파이] 구성 파일  (0) 2010.10.27
[델파이] Frame의 속성  (0) 2010.10.26
Posted by 래채
, |