Jump to content
Henry Olive

Excel Get Column Number

Recommended Posts

Good Day

 

I'm trying to Import Datas from Excel into a table

When i scan the excel sheet i need to know in which column i am

 

      var

        Sheet, Book : Variant;

     begin

         Book:= ExcelApp.WorkBooks.Open(OD1.FileName);
         Sheet := Book.Worksheets[1];          

 

         for Col:= Low to High do
         begin

           if Sheet.Cell.Column = 2 then //  (i need to get column number here),  ERROR  Method Cell not supported
           doThis;

         end;

     end;

Share this post


Link to post

I remeber somethings about decades ago ... may be the property is Sheet.Cells[x, y] or Sheets.Cells[x].Column ?

 

For your facility, if you import the Excel typelib, you will able to see all properties of all "components" in the "Excel..._tlb.pas" created.

 

These should be near the same as you use with OleObj.

Share this post


Link to post

Start making your own Excel object for your projects..

 

For example:

constructor TExportExcel.Create;
begin
  oXL := CreateOleObject('Excel.Application');
  xlsClosed:=False;
  oXL.Visible := False;

  // Get a new workbook
  oWB := oXL.Workbooks.Add;
  oSheet := oWB.ActiveSheet;
end;

procedure TExportExcel.CloseExcel;
begin
  oWB.Close;
  oXl.Quit;
  xlsClosed:=True;
end;

procedure TExportExcel.Open(aFileName: String);
begin
  oXL.WorkBooks.Open(aFileName);
  oSheet := oXL.Workbooks[ExtractFileName(aFileName)].WorkSheets[1];
end;

function TExportExcel.ColToText(aCol: integer): string;
var
  d,m:integer;
begin
  result:='';
  if aCol<=0 then exit;


  aCol:=aCol-1;
  d:=aCol div 26;
  m:=aCol mod 26;
  result:=Char(ord('A')+m);
  if d>0 then
  begin
    result:=Char(ord('A')+d-1)+result;
  end;
end;

function TExportExcel.GetCell(aRow, aCol: integer): OLEVariant;
begin
  result:=oSheet.Cells[aRow,aCol]
end;

procedure TExportExcel.SetCellBold(aRow, aCol: integer; const Value: OLEVariant);
var
  oRng:OLEVariant;
begin
  oSheet.Cells[aRow,aCol]:=Value;
  oRng:=oSheet.Range[CellToRange(aRow,aCol)];
  oRng.Font.Bold:=True;
end;

procedure TExportExcel.SetCellValue(aRow, aCol: integer; const Value: OLEVariant);
Begin
  oSheet.Cells[aRow,aCol].value := Value;
End;

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×