1. 필요 unit


엑셀로 인한 추가 unit 2개 : ComObj, OleCtrls


2. 변수 선언


var

     XL : variant ;

begin

    XL := CreateOLEObject('Excel.Application');     // OLE컨트롤 생성
    XL.DisplayAlerts := False;          //경고창 숨기기

    XL.WorkBooks.Add;                   //새화일 열기

    XL.SaveAs('C:\test.xls');         //다른이름 저장

    XL.quit;            //엑셀 언로드 ---> exception 구간에서 사용하기 ! 안사용하면 메모리누수


3. 생각해야 될것 기타 값 입력 모양 변경 등등은


엑셀에 보면 도구 - 메크로 - 메크로 기록이라는 항목이 있는데


거기서 기록 을 누른 후 행동을 하고 ㅁ 버튼을 눌러 메크로 기록을 중지하면


기록된 메크로를 볼 수 있는데 (alt + F11)  여기서 만들어진 메크로를 적용 시키면 된다.



3번이 가장 중요 ! 즉 메크로 기록을 한후 역추적을 통해 코딩 ㄱㄱ



===================================================================================

이하 코드를 통하여 참조하여 주시기 바랍니다. ㄳ

===================================================================================

unit Unit1;

interface

uses
//    엑셀로 인한 추가 unit 2개 : ComObj, OleCtrls
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComObj, OleCtrls, Grids, StdCtrls;
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  Function  ReadExcelFile(sFileName:String):integer;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
    ReadExcelFile('12.xls');
end;

function TForm1.ReadExcelFile(sFileName: String): integer;
var
  XL, XArr, XTitle : Variant;
  sheetNumer,x,y : Integer;
  temp : String;
begin
  sheetNumer:=1;
  temp := 'a1:a1';
  try
    XL := CreateOLEObject('Excel.Application');
    XL.DisplayAlerts := False;          //경고창 보이기
    XL.workbooks.add;                   //새화일 열기
    XL.ActiveCell.FormulaR1C1 := '=3*3';
    XL.ActiveCell.CurrentRegion.Select;
    XL.selection.style:='Currency';
    XL.Cells[1,3].value := '3';

    XL.Range['D22'].Select;
    XL.ActiveCell.FormulaR1C1 := 'wonsama';
    XL.Selection.Font.ColorIndex := 3;


//   참조항목
//    XL.workbooks.Add('C:\test.xls');    //특정 이름의 화일 열기
//    XL.workbooks.Open('C:\'+sFileName); //특정 이름의 화일 열기
//    XL.ActiveWorkbook.saveas('C:\123.xls'); //활성화된 엑셀 다른 이름으로 저장
//    XL.ActiveCell.FormulaR1C1 := '=3*3';    //값입력
//    XL.ActiveCell.Font.Bold := True      //글자 환경 변경
//    XL.ActiveCell.CurrentRegion.Select;   //활성화 된 셀의 영역을 선택
//    XL.selection.style:='Currency';       //선택영역 통화 형태로

  except
    result:= 0;
    MessageDlg('Excel이 설치되어 있지 않습니다.'+#13+'이 기능을 이용하시려면 반드시 MS오피스 엑셀이 설치되어 있어야 합니다.- ' , MtWarning, [mbok], 0);
    XL.quit;            //엑셀 언로드
    Exit;
  end;
  XL.Visible := true;
  XL:= Unassigned;
 end;
end.

Posted by 나비:D
:
BLOG main image
by 나비:D

공지사항

카테고리

분류 전체보기 (278)
Programming? (0)
---------------------------.. (0)
나비의삽질 (5)
Application (177)
SQL (51)
Web (27)
etc. (14)
Omnia (0)
---------------------------.. (0)

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함

달력

«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Total :
Today : Yesterday :