Excel cell number format

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.

Excel cell numeric and text values formatted with GemBox.Spreadsheet for Java
Excel cell numeric and text values formatted with GemBox.Spreadsheet for Java
Upload your file (Drag file here)
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

GemBox.Spreadsheet for Java is a component that enables you to read, write, edit and convert spreadsheet files from your applications using one simple API.

Download Buy