Åtkomst till kalkylbladets celler

Åtkomst till celler

Aspose.Cells för Python via Java låter dig komma åt celler i ett kalkylblad genom att använda cellnamnet eller genom att använda radens och kolumnens index. Den här artikeln visar båda dessa tillvägagångssätt som används för att komma åt celler i ett kalkylblad.

Kom åt cellen med hjälp av cellnamnet

Det följande kodexemplet visar åtkomst av cellen med hjälp av cellnamnet. Det här kodexemplet kommer åt cellen “C5” och skriver ut dess värde.

source_directory = "Examples/SampleFiles/SourceDirectory/"
# Instantiating a Workbook object
workbook = Workbook(source_directory + "Book1.xlsx")
# Accessing the worksheet in the Excel file
worksheet = workbook.getWorksheets().get(0)
cells = worksheet.getCells()
# Accessing a cell using its name
cell = cells.get("C5")
# Print Message
print("Cell Value: " + str(cell.getValue()))

Kom åt cellen med hjälp av rad- och kolumnindex

Det följande kodexemplet visar åtkomst av cellen med hjälp av rad- och kolumnindex. Det här kodexemplet kommer åt och skriver ut värdet på cellen “C5” som identifieras av radindex 4 och cellindex 2.

source_directory = "Examples/SampleFiles/SourceDirectory/"
# Instantiating a Workbook object
workbook = Workbook(source_directory + "Book1.xlsx")
# Accessing the worksheet in the Excel file
worksheet = workbook.getWorksheets().get(0)
cells = worksheet.getCells()
# Accessing a cell using the row and column index
cell = cells.get(4, 2)
# Print Message
print("Cell Value: " + str(cell.getValue()))