Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

reading excel cell 29 Nov 2021 13:50 #20585

  • vzeljko
  • vzeljko's Avatar
  • Topic Author


  • Posts: 30
  • I have problem reading excel cell. Here is my code:

    LOCAL cExelFile AS STRING
    LOCAL oExcel AS Microsoft.Office.Interop.Excel.Application
    LOCAL oWorkbook AS Workbook
    LOCAL oWorksheet AS Worksheet


    oExcel := Microsoft.Office.Interop.Excel.Application{}
    oExcel:Visible := FALSE
    oWorkbook := oExcel:Workbooks:Open(Path.Combine( cDataPath, cExelFile ))
    oWorksheet := oWorkbook:ActiveSheet // error XS0266

    TRY
    SELF:oPrTextBox:Text := oWorksheet:Cells[3, 5]:Value:ToString() // error XS1061

    CATCH oEx AS Exception
    MessageBox:Show(oEx:ToString())
    FINALLY
    oWorkbook:Close()
    END TRY



    error XS0266: Cannot implicitly convert type 'object' to 'Microsoft.Office.Interop.Excel.Worksheet'. An explicit conversion exists (are you missing a cast?)
    error XS1061: 'object' does not contain a definition for 'Value' and no accessible extension method 'Value' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
    Compilation failed (2 errors)

    Does anybody have any idea?

    Zeljko

    Please Log in or Create an account to join the conversation.

    reading excel cell 29 Nov 2021 13:58 #20586

    • wriedmann
    • wriedmann's Avatar


  • Posts: 2878
  • Hi Zeljko,
    in .NET you have to cast:
    oWorksheet := (  WorkSheet (oWorkbook:ActiveSheet
    if the instance variable WorkBook:WorkSheet is defined as generic object.
    Wolfgang
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    reading excel cell 29 Nov 2021 14:14 #20589

    • vzeljko
    • vzeljko's Avatar
    • Topic Author


  • Posts: 30
  • Thanks Wolfgang. Your suggestion solved first problem, but after compiling I got another one:

    error XS1061: 'object' does not contain a definition for 'Cells' and no accessible extension method 'Cells' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

    for this line:
    SELF:oPrTextBox:Text := oWorksheet:Cells[3, 5]:Value:ToString()

    Zeljko

    Please Log in or Create an account to join the conversation.

    reading excel cell 29 Nov 2021 14:28 #20593

    • ic2


  • Posts: 1221
  • Hello Zeljko,

    See this discussion: www.xsharp.eu/forum/private-product/1111...-speed-question#8173

    My X# Excel code now looks as follows:

    oWorkbooks:Open(cExcelFile)
    oWorkSheet:=(Worksheet)oExcel:Worksheets[1] // Changed 5-8-2020 from //oWorkSheet:=oExcel:ActiveSheet this gives error Cannot implicitly convert type 'object' to 'Microsoft.Office.Interop.Excel.Worksheet'. An explicit conversion exists (are you missing a cast?) // To activate the first sheet
    oRange:=oWorkSheet:UsedRange


    and further:

    For nRow:= 1 Upto nRowCount
    dr:=dt:NewRow()
    For nCol:=1 Upto nColCount
    oCell := (Range) oRange:Cells[nRow, nCol]
    cContent := AsString(oCell:Value2)

    (etc)
    This should work

    Dick

    Please Log in or Create an account to join the conversation.

    Last edit: by ic2.

    reading excel cell 29 Nov 2021 16:07 #20604

    • vzeljko
    • vzeljko's Avatar
    • Topic Author


  • Posts: 30
  • Thanks Dick. Works fine.

    Zeljko

    Please Log in or Create an account to join the conversation.

    • Page:
    • 1