获取带格式和不带格式的单元格字符串值
Contents
[
Hide
]
Aspose.Cells 提供一个 Cell.getStringValue() 方法,可以用来获取单元格的字符串值,无论是否带有格式。假设你有一个值为 0.012345 的单元格,你对其应用了格式,仅显示两位小数,那么在 Excel 中会显示为 0.01。你可以用 Cell.getStringValue() 方法同时获取为 0.01 和 0.012345两种值。该方法接受一个 CellValueFormatStrategy 枚举参数,具有以下值。
- CellValueFormatStrategy.CellStyle
- CellValueFormatStrategy.DisplayStyle
- CellValueFormatStrategy.DisplayString
- CellValueFormatStrategy.None
以下示例代码说明了Cell.getStringValue()方法的使用。
This file contains hidden or 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
const AsposeCells = require("aspose.cells.node"); | |
const path = require("path"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Create workbook | |
const workbook = new AsposeCells.Workbook(); | |
// Access first worksheet | |
const worksheet = workbook.getWorksheets().get(0); | |
// Access cell A1 | |
const cell = worksheet.getCells().get("A1"); | |
// Put value inside the cell | |
cell.putValue(0.012345); | |
// Format the cell that it should display 0.01 instead of 0.012345 | |
const style = cell.getStyle(); | |
style.setNumber(2); | |
cell.setStyle(style); | |
// Get string value as Cell Style | |
let value = cell.getStringValue(AsposeCells.CellValueFormatStrategy.CellStyle); | |
console.log(value); | |
// Get string value without any formatting | |
value = cell.getStringValue(AsposeCells.CellValueFormatStrategy.None); | |
console.log(value); |