Forum Discussion

Desktopapptesti's avatar
Desktopapptesti
Occasional Contributor
2 years ago
Solved

reading data from Excel

Marsha_R 

Used below code to read the data from excel file and I am getting file not found error msg.

 

def ExcelExample():

# Get the sheet of the Excel file
excelFile = Excel.Open("C:\\temp\\DataStorageExcel.xlsx")
excelSheet = excelFile.SheetByTitle["Sheet1"]

# Read data from the Excel file
valueA = excelSheet.Cell["A", 3].Value
valueB = excelSheet.Cell[2, 3].Value
valueC = excelSheet.CellByName["C3"].Value

# Write the obtained data into a new row of the file
rowIndex = excelSheet.RowCount + 1
excelSheet.Cell["A", rowIndex].Value = valueA
excelSheet.Cell[2, rowIndex].Value = valueB
excelSheet.Cell("C", rowIndex).Value = valueC

# Save the file to apply the changes
excelFile.Save()

# Save the file with another name
# excelFile.SaveAs("C:\\temp\\DataStorageExcel_new.xlsx")

  • Hi Desktopapptesti!

     

    Did you create and populate a spreadsheet called 'DataStorageExcel.xlsx' in 'c:\\temp'? 

     

    This will be required for this sample to work. I tried the example on my end was able to open the file without issue after I created it. 

     

    One thing to note here, there is a syntax error in the provided example. 

    Line 16 needs brackets [], instead of parens (). 

     

    Once you are past the file error you may alter line 16 to complete the example; 

    #This line has parens but needs brackets.
    #excelSheet.Cell("C", rowIndex).Value = valueC
    
    #Updated line with brackets.
    excelSheet.Cell["C", rowIndex].Value = valueC

     

    I hope this helps!

3 Replies

  • Hi Desktopapptesti!

     

    Did you create and populate a spreadsheet called 'DataStorageExcel.xlsx' in 'c:\\temp'? 

     

    This will be required for this sample to work. I tried the example on my end was able to open the file without issue after I created it. 

     

    One thing to note here, there is a syntax error in the provided example. 

    Line 16 needs brackets [], instead of parens (). 

     

    Once you are past the file error you may alter line 16 to complete the example; 

    #This line has parens but needs brackets.
    #excelSheet.Cell("C", rowIndex).Value = valueC
    
    #Updated line with brackets.
    excelSheet.Cell["C", rowIndex].Value = valueC

     

    I hope this helps!