Skip to content

Set cell value

Pierlam-dev edited this page Jan 24, 2026 · 1 revision

Set a value, principle

When a value in a certain type is set to a cell, If the cell does not exists, it will be created before setting the value.

If the cell exists and has a style: fill: background/foreforeground colors, border, ... The style will stay the same.

If the cell contains a formula, it will be revoved.

Same as Get value, you can use 3 ways to address the cell.

Example, set the value 12. into a cell.

//1- by reference
proc.SetCellValue(excelSheet, "C10", 12.5);

//2- by column and row indexes
proc.SetCellValue(excelSheet, 3, 10, 12.5);

//3- by object
ExcelCell excelCell= proc.GetCellAt("C10"); 
proc.SetCellValue(excelSheet, excelCell, 12.5);

Set a value and a number format

When setting a value, it's possible to define the format (display format). You can format the display of the value for number, date and currency.

For date and currency the format is mandatory.

Example, format the display of a number:

// set a double value and format it with 2 decimals, e.g.: 12,30
proc.SetCellValue(excelSheet, "B9", 12.5, "0.00");

// set a date with a standard format, will display: 12/10/2025
proc.SetCellValue(excelSheet, "D12", new DateOnly(2025,10,12), "d/m/yyyy");

You can use one of some predefined format declared in the class Definitions.cs:

// set a double value and format it with 2 decimals, e.g.: 12,30
// Definitions.NumFmtNumberTwoDec2= "0.00"
proc.SetCellValue(excelSheet, "B5", 12.3, Definitions.NumFmtNumberTwoDec2);

// set a formated date
// Definitions.NumFmtDayMonthYear14= "d/m/yyyy"
proc.SetCellValue(excelSheet, "C4", new DateOnly(2025,12,28), Definitions.NumFmtDayMonthYear14);

If you set a value (string, int or double) without format in a existing cell, the defined format of the cell is used as much as possible.

Set a value, available types

You can set a value with these types:

//-string
proc.SetCellValue(excelSheet, "C10", "hello");

//-integer
proc.SetCellValue(excelSheet, "C10", 12);

//-double
proc.SetCellValue(excelSheet, "C10", 23.45);

//-dateOnly
proc.SetCellValue(excelSheet, "C10", new DateOnly(2025, 30,12));

//-dateTime
proc.SetCellValue(excelSheet, "C10", new DateTime(2025, 30,12, 10,23,45));

//-timeOnly
proc.SetCellValue(excelSheet, "C10", new TimeOnly(10,23,45));

Clone this wiki locally