Get Cell String Value with Format Strategy
Aspose.Cells for Python via .NET provides a method Cell.get_string_value(format_strategy) which can be used to get the string value of the cell with or without any formatting. Suppose, you have a cell with value 0.012345 and you have formatted it to display two decimal places only. It will then display as 0.01 in Excel. You can retrieve string values both as 0.01 and as 0.012345 using the Cell.get_string_value(format_strategy) method. It takes CellValueFormatStrategy enum as a parameter which has the following values
- CellValueFormatStrategy.CELL_STYLE
- CellValueFormatStrategy.DISPLAY_STYLE
- CellValueFormatStrategy.DISPLAY_STRING
- CellValueFormatStrategy.NONE
The following sample code explains the use of Cell.get_string_value(format_strategy) method.
from aspose.cells import CellValueFormatStrategy, Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Create workbook | |
workbook = Workbook() | |
# Access first worksheet | |
worksheet = workbook.worksheets[0] | |
# Access cell A1 | |
cell = worksheet.cells.get("A1") | |
# Put value inside the cell | |
cell.put_value(0.012345) | |
# Format the cell that it should display 0.01 instead of 0.012345 | |
style = cell.get_style() | |
style.number = 2 | |
cell.set_style(style) | |
# Get string value as Cell Style | |
value = cell.get_string_value(CellValueFormatStrategy.CELL_STYLE) | |
print(value) | |
# Get string value without any formatting | |
value = cell.get_string_value(CellValueFormatStrategy.NONE) | |
print(value) |