Other API Samples

Live SpreadsheetGear API Samples

Range Samples Formatting and Other Cell Features Number Formats

Set number formats.

// Create a new workbook and get a reference to the active sheet and its cells.
SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
SpreadsheetGear.IWorksheet worksheet = workbook.ActiveWorksheet;
SpreadsheetGear.IRange cells = worksheet.Cells;

// Get a reference to a range of cells.
SpreadsheetGear.IRange range = cells["A1:A9"];

// Add data to the cells.
range.Formula = "1234.5678";

// Set various number formats.
range[0, 0].NumberFormat = "General";
range[1, 0].NumberFormat = "0";
range[2, 0].NumberFormat = "$#,##0.00_);($#,##0.00)";
range[3, 0].NumberFormat = "m/d/yyyy";
range[4, 0].NumberFormat = "h:mm AM/PM";
range[5, 0].NumberFormat = "0.00%";
range[6, 0].NumberFormat = "# ??/??";
range[7, 0].NumberFormat = "0.00E+00";

// Set number format as text.
// NOTE: Text format must be set before setting the value.
range[8, 0].NumberFormat = "@";
range[8, 0].Formula = "00123456";

// Autofit the column.
Run This Sample
Render Image

Generate an image representation of the results of this sample, which uses the SpreadsheetGear.Drawing.Image class to convert ranges, charts and shapes to images.

Download File

Download an Excel Open XML Workbook (*.xlsx) file with the results of this sample.

Related Samples