alfanew
Programmer
- Oct 13, 2005
- 7
I can to read the information from an excel file.
Help me !!!!!
Help me !!!!!
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
// Read the cell value in
MyVar := ExcelApplication1.Range[cCell,cCell].Value2;
// Set the cell value to you variable
ExcelApplication1.Range[cCell,cCell].Value2 := MyVar;
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.