通过格式化策略获取单元格字符串值
Contents
[
Hide
]
Aspose.Cells for Python via .NET 提供了一个方法 Cell.get_string_value(format_strategy),可用于获取带有或没有任何格式的单元格的字符串值。假设您有一个值为 0.012345 的单元格,并且已将其格式化为只显示两位小数。在 Excel 中,它将显示为 0.01。您可以使用 Cell.get_string_value(format_strategy) 方法检索字符串值,值可以是 0.01 也可以是 0.012345。它接受 CellValueFormatStrategy 枚举作为参数,该枚举具有以下值:
- CellValueFormatStrategy.CELL_STYLE
- CellValueFormatStrategy.DISPLAY_STYLE
- CellValueFormatStrategy.DISPLAY_STRING
- CellValueFormatStrategy.NONE
以下示例代码解释了使用Cell.get_string_value(format_strategy)方法的用法。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |