Tuesday 6 April 2010

Setting Attributes of an Excel File

In the previous tutorial ( Creating an Excel File using Delphi ), you have learned how to create an MS Excel file. Now we need to set some attributes of the MS Excel file. In an MS Excel file, a cell has some attributes ie : font, color, border etc. 

To set cell border, use this code :

ExcelApplication.WorkBooks[1].WorkSheets['my data']. Range['A1: D7'].Borders.LineStyle := 7;

Or

Sheet.Range['A1: D7'].Borders.LineStyle := 7;

We can replace ExcelApplication.WorkBooks[1].WorkSheets['my data'] with Sheet because in the previous tutorial we have set our working worksheet into Sheet variable. The LineStyle value can be 0, 1, 2 ……13. You can try each value and see what happen to the worksheet.

To set cell border color :

Sheet.Range['A1:D7'].Borders.color := clblue;

To set cell font attributes :

Sheet.Range['A1: D7'].Font.color := clred;

Sheet.Range['A1: D7'].Font.Bold := true;

To set columns width :

Sheet.Columns[1].ColumnWidth := 4;

Sheet.Columns[2].ColumnWidth := 30;

Sheet.Columns[3].ColumnWidth := 8;

Sheet.Columns[4].ColumnWidth := 20;

To set rows height :

Sheet.Rows[1].RowHeight := 25;

Sheet.Rows[2].RowHeight:= 40;

Entering a value to a cell

In MS Excel worksheet, every cell has a name like A1, B12, AA7, BC8 etc. But we can not access cells with these names. Imagine that a worksheet is a two dimension array of cells. A1 equals to Cells[1,1], B12 equals to Cells[12,2], AA7 equals to Cells[7,27] etc. To enter a value to A1, B1, C1 and D1 cells, use these codes :

Sheet.Cells[1,1] := 'ID';

Sheet.Cells[1,2] := 'NAME';

Sheet.Cells[1,3] := 'AGE';

Sheet.Cells[1,4] := 'PHONE NUMBER';

Now add some codes to our previous project in the previous tutorial. See the codes below, the new added codes are bold.

unit Unit1;

interface

uses

  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

  StdCtrls, ComObj;

type

  TForm1 = class(TForm)

    CreateExcel: TButton;

    Exit: TButton;

    procedure ExitClick(Sender: TObject);

    procedure CreateExcelClick(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;

var

  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.CreateExcelClick(Sender: TObject);

var

 ExcelApplication : variant;

 Sheet : variant;

begin

 try

  begin

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

   ExcelApplication.Visible := true;

  end;

 except

  Showmessage('Cannot create an Excel file, make sure that MS Excel is installed on your system');

  Application.Terminate;

 end;

 ExcelApplication.WorkBooks.Add(-4167);

 ExcelApplication.WorkBooks[1].WorkSheets[1].Name := 'my data';

 Sheet := ExcelApplication.WorkBooks[1].WorkSheets['my data'];

 Sheet.Range['A1:D7'].Borders.LineStyle := 7;

 Sheet.Range['A1:D7'].Borders.color := clblue;

 Sheet.Columns[1].ColumnWidth := 4;

 Sheet.Columns[2].ColumnWidth := 30;

 Sheet.Columns[3].ColumnWidth := 8;

 Sheet.Columns[4].ColumnWidth := 20;

 Sheet.Cells[1,1] := 'ID';

 Sheet.Cells[1,2] := 'NAME';

 Sheet.Cells[1,3] := 'AGE';

 Sheet.Cells[1,4] := 'PHONE NUMBER';

end;

procedure TForm1.ExitClick(Sender: TObject);

begin

 Application.Terminate;

end;

end.


Go to Previous Tutorial ( Creating an Excel File using Delphi )

or Next Tutorial ( Export an Interbase Table into an MS Excel File )

4 comments:

  1. Great, but what about if no M$EXCEL is installed at all?

    How to do such with a Freeware solution?

    ReplyDelete
  2. supplier of 3D printing, rapid prototyping, rapid manufacturing, plastic fabrication, tooling, CNC and injection molding services.

    ReplyDelete
    Replies
    1. s The best and a lot of secret drop-shipping suppliers in the world which will provide your products globally available for you directly to your customers doors. Also get the best drop-shipping manufacturer on your organization.

      Delete
  3. Great...! i worked on this. I was so fedup with the problem.
    thanks a lot.
    One more thing is there any way to merge cells .
    i can't give headings to the excel out put.
    if you can please post the answer
    alex mathew Kottayam,kerala,india

    ReplyDelete

These links are part of a pay per click advertising program called Infolinks. Infolinks is an In Text advertising service; they take my text and create links within it. If you hover your mouse over these double underlined links, you will see a small dialog box containing an advertisement related to the text. You can choose to move the mouse away and go on with your browsing, or to click on the box and visit the relevant ad. Click here to learn more about Infolinks Double Underline Link Ads.