Excel cell number format
GemBox.Spreadsheet for Java has been retired
The following example shows how you can use GemBox.Spreadsheet for Java to get formatted cell value. Formatted cell value is a textual representation of a cell value which is formatted with a number format applied on that cell.
Using this example you can also find out how specific number format from MS Excel should be used in GemBox.Spreadsheet for Java API. Create new Excel file with MS Excel and populate its A2 cell in the first worksheet with a value and number format. Use that file as input to this example ('Upload your file' below) and run the example. Use value from output file's D column in GemBox.Spreadsheet API.

import com.gembox.spreadsheet.*;
class Program {
public static void main(String[] args) throws java.io.IOException {
// If using the Professional version, put your serial key below.
SpreadsheetInfo.setLicense("FREE-LIMITED-KEY");
ExcelFile workbook = ExcelFile.load("%InputFileName%");
ExcelWorksheet worksheet = workbook.getWorksheet(0);
worksheet.getCell(0, 2).setValue("ExcelCell.Value");
worksheet.getColumn(2).getStyle().setNumberFormat("@");
worksheet.getCell(0, 3).setValue("CellStyle.NumberFormat");
worksheet.getColumn(3).getStyle().setNumberFormat("@");
worksheet.getCell(0, 4).setValue("ExcelCell.GetFormattedValue()");
worksheet.getColumn(4).getStyle().setNumberFormat("@");
for (int i = 1; i < worksheet.getRows().size(); i++) {
ExcelCell sourceCell = worksheet.getCell(i, 0);
worksheet.getCell(i, 2).setValue(sourceCell.getValue() == null ? null : sourceCell.getValue().toString());
worksheet.getCell(i, 3).setValue(sourceCell.getStyle().getNumberFormat());
worksheet.getCell(i, 4).setValue(sourceCell.getFormattedValue());
}
// Set column widths.
double[] columnWidths = new double[] { 192, Double.NaN, 122, 236, 200 };
for (int i = 0; i < columnWidths.length; i++)
if (!Double.isNaN(columnWidths[i]))
worksheet.getColumn(i).setWidth(columnWidths[i], LengthUnit.PIXEL);
workbook.save("Number Format.%OutputFileType%");
}
}
See also
Next steps
Published: December 13, 2018 | Modified: December 19, 2022 | Author: Marek Turis