書式設定ありおよびなしでセル文字列の値を取得
Contents
[
Hide
]
Aspose.Cellsは、Cell.getStringValue()メソッドを提供しており、セルの文字列値をフォーマットの有無に関係なく取得できます。例えば、値が0.012345のセルがあり、小数点以下2桁だけ表示するようにフォーマットされている場合、Excelでは0.01と表示されます。 Cell.getStringValue()メソッドを使えば、0.01としても0.012345としても文字列値を取得可能です。このメソッドは以下の値を持つ CellValueFormatStrategy 列挙型をパラメータとして受け取ります。
書式設定ありおよびなしでセル文字列の値を取得
以下のサンプルコードは、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
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(GetCellStringValue.class); | |
// Create workbook | |
Workbook workbook = new Workbook(); | |
// Access first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Access cell A1 | |
Cell 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 | |
Style style = cell.getStyle(); | |
style.setNumber(2); | |
cell.setStyle(style); | |
// Get string value as Cell Style | |
String value = cell.getStringValue(CellValueFormatStrategy.CELL_STYLE); | |
System.out.println(value); | |
// Get string value without any formatting | |
value = cell.getStringValue(CellValueFormatStrategy.NONE); | |
System.out.println(value); |
コンソール出力
上記のサンプルコードのコンソール出力は以下の通りです。
0.01
0.012345