Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I read a cell in excel file? 1

Status
Not open for further replies.

alfanew

Programmer
Oct 13, 2005
7
I can to read the information from an excel file.
Help me !!!!!
 
I'd suggest you first read the following:
[ul]
[li]S Vanels' excellent FAQ (faq102-1562)in this forum[/li]
[li]thread102-771386[/li]
[li]thread102-738955[/li]
[/ul]

Cheers,
Chris [pc2]
 
Thank Chris!!
But I have to read from an Excel file ,not write.
 
You can read and write an Excel file with the same basic method - viz:
Code:
// Read the cell value in
   MyVar := ExcelApplication1.Range[cCell,cCell].Value2;
// Set the cell value to you variable
   ExcelApplication1.Range[cCell,cCell].Value2 := MyVar;

However, to illustrate the point in a working example (using D7),
create a new application with the following components:
[ul]
[li]ExcelApplication[/li]
[li]Button[/li]
[li]Combo box[/li]
[li]Edit masks (two of them) - one for row number (mask #####;1; ) and one for the column label (mask >lll;1;_ [/li]
[li]Label[/li]
[/ul]

You'll also need some events for the ButtonClick (onClick) and the Form (onActivate).

The following code reads the cell you specify, from the tab sheet you choose from the combo box
Code:
unit ExcelTest;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExcelXP, OleServer, Mask;

type
  TForm1 = class(TForm)
    ExcelApplication1: TExcelApplication;
    Button1: TButton;
    Label1: TLabel;
    ComboBox1: TComboBox;
    Label2: TLabel;
    E_Row: TMaskEdit;
    E_Column: TMaskEdit;
    procedure Button1Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
   var
      wSheet : _WorkSheet;
      LCID : INTEGER;
      fExcelFile,  // name of your file
      cCol,        // string to check the column value
      cCell : string; // cell to read

  begin
      fExcelFile := 'c:\mypath\myfile.xls'; // File to open
      LCID := GetUserDefaultLCID;           // not sure what this is - me thinks its a handle
      with ExcelApplication1 do begin
        Connect;                            // Connect the server
        try
          visible[LCID] := false;           // make Excel invisible when it works

          if FileExists(fExcelFile) then
            begin                              // Open the workbook to the existing file

          // Open(FileName, UpdateLinks, ReadOnly, Format, Password, WriteResPassword,
          //      IgnoreReadOnlyRecommended, Origin, Delimiter, Editable, Notify,
          //      Converter, AddToMru, Local, CorruptLoad, LCID);
              WorkBooks.Open(fExcelFile,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,
                              EmptyParam, EmptyParam,EmptyParam,EmptyParam,EmptyParam,
                              EmptyParam,EmptyParam,EmptyParam,EmptyParam,LCID);
          // Set the worksheet - must be its numeric value!                              
              wSheet := ExcelApplication1.Sheets.Item[ComboBox1.ItemIndex + 1] as _WorkSheet;
              WSheet.Activate(LCID);
            end;  // if FileExists

            // Check the Row  Column are valid
            if (length(trim(E_row.Text)) = 0) then
              E_Row.Text := '1';
            if (StrToInt(trim(E_Row.Text)) < 1) then
              E_Row.Text := '1';   
            cCol := trim(E_Column.Text);
            if (length(cCol) = 0) or not (cCol[1] in ['A'..'Z']) then
              E_Column.Text := 'A';

            cCell :=  trim(E_Column.Text) + trim(E_Row.Text); // make the cell string

            // ******* Display the results in the caption - this is the actual reading bit! ********

            [b]Label1.Caption := ExcelApplication1.Range[cCell,cCell].Value2;[/b]

            // *********************************************************
        finally
          disconnect;  // break the connection
          ExcelApplication1.Workbooks.Close(LCID); // close it, or you'll lock the file!
        end;  //try
      end;  //with ExcelApplication1
    end;  // Button1Click

procedure TForm1.FormActivate(Sender: TObject);
// used this only to demonstrate getting the sheet names
  var
    I,            // Counter for loop
    LCID : INTEGER;
    fExcelFile,  // name of your file
    LineString : string;
  begin
    fExcelFile := 'c:\mypath\myfile.xls';
    LCID := GetUserDefaultLCID;           // not sure what this is - me thinks its a handle
    with ExcelApplication1 do begin
      Connect;                            // Connect the server
      try
        visible[LCID] := false;           // make Excel invisible when it works

        if FileExists(fExcelFile) then
          begin                              // Open the workbook to the existing file

        // Open(FileName, UpdateLinks, ReadOnly, Format, Password, WriteResPassword,
        //      IgnoreReadOnlyRecommended, Origin, Delimiter, Editable, Notify,
        //      Converter, AddToMru, Local, CorruptLoad, LCID);
            WorkBooks.Open(fExcelFile,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,
                            EmptyParam, EmptyParam,EmptyParam,EmptyParam,EmptyParam,
                            EmptyParam,EmptyParam,EmptyParam,EmptyParam,LCID);

        // Get each worksheets name and add it to the combo box
            for I := 1 to Worksheets.Count do
              ComboBox1.additem((Worksheets[I] as _WorkSheet).Name,nil);
            ComboBox1.ItemIndex := 0; // set item index
          end;  // if FileExists
      finally
        disconnect;
      end; //try
    end; //with Excelapplication1
  end; //FormActive
end.

Hope thats a bit more instructive.
BTW, the parameters (eg for [tt]ExcelApplication1.WorkBooks.Open[/tt])may vary between versions of Office - the ones I've used are for OfficeXP, but seem to work with the most recent edition.

Cheers,
Chris [pc2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top