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;

+ Recent posts