Change the format of a cell

Possible Usage Scenarios

When you want to highlight certain data, you can change the style of the cells.

How to change the format of a cell in Excel

To change the format of a single cell in Excel, follow these steps:

  1. Open Excel and open the workbook that contains the cell you want to format.

  2. Locate the cell you want to format.

  3. Right-click on the cell and select “Format Cells” from the context menu. Alternatively, you can select the cell and go to the Home tab in the Excel ribbon, click on the “Format” dropdown in the “Cells” group, and select “Format Cells”.

  4. The “Format Cells” dialog box will appear. Here, you can choose various formatting options to apply to the selected cell. For example, you can change the font style, font size, font color, number format, borders, background color, etc. Explore the different tabs in the dialog box to access various formatting options.

  5. After making the desired formatting changes, click the “OK” button to apply the formatting to the selected cell.

How to change the format of a cell Using C#

To change the format of a cell using Aspose.Cells, you can use You can use the following methods:

  1. Cell.SetStyle(Style style)
  2. Cell.SetStyle(Style style, bool explicitFlag)
  3. Cell.SetStyle(Style style, StyleFlag flag)

Sample Code

In this example, we create an Excel workbook, add some sample data, access the first worksheet, and get two cells(“A2” and “B3”). Then, we get the style of the cell, set various formatting options (e.g., font color, bold), and change the format to the cell. Finally, we save the workbook to a new file. todo:image_alt_text

// Create the workbook
Workbook workbook = new Workbook();
//Get the first worksheet
Worksheet ws = workbook.Worksheets[0];
Aspose.Cells.Cells cells = ws.Cells;
//Setting the value to the cells
Aspose.Cells.Cell cell = cells["A1"];
cell.PutValue("Fruit");
cell = cells["B1"];
cell.PutValue("Count");
cell = cells["C1"];
cell.PutValue("Price");
cell = cells["A2"];
cell.PutValue("Apple");
cell = cells["A3"];
cell.PutValue("Mango");
cell = cells["A4"];
cell.PutValue("Blackberry");
cell = cells["A5"];
cell.PutValue("Cherry");
cell = cells["B2"];
cell.PutValue(5);
cell = cells["B3"];
cell.PutValue(3);
cell = cells["B4"];
cell.PutValue(6);
cell = cells["B5"];
cell.PutValue(4);
cell = cells["C2"];
cell.PutValue(5);
cell = cells["C3"];
cell.PutValue(20);
cell = cells["C4"];
cell.PutValue(30);
cell = cells["C5"];
cell.PutValue(60);
// Access the worksheet
Worksheet worksheet = workbook.Worksheets[0];
Cell a2 = worksheet.Cells["A2"];
// Get style of A2
Style style = a2.GetStyle();
// Change the format
style.Font.Color = Color.Red;
style.Font.IsBold = true;
StyleFlag flag = new StyleFlag();
flag.FontColor = true;
a2.SetStyle(style, flag);
Cell b3 = worksheet.Cells["B3"];
// Get style of B3
Style style2 = b3.GetStyle();
// Change the format
style2.Font.Color = Color.Blue;
style2.Font.IsItalic = true;
b3.SetStyle(style2);
// Save the modified workbook
workbook.Save("output.xlsx");