When having issues like these, it is always a good idea to create a test project and check what is happening.
Excel Interop - Microsoft Learn[
^] especially.
So, I have created a spreadsheet manually and put a numeric value in a cell. Then I turned macro recording on, applied formatting and resized the row. Then turned off recording and viewed the VBA code generated. It looked like this:
Sub Macro1()
Selection.NumberFormat = "#,##0"
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").RowHeight = 84
End Sub
Okay, now I have a reference, I can now write my code. Here is the C# version of the code above:
using System;
using System.Diagnostics;
using System.IO;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;
using _Excel = Microsoft.Office.Interop.Excel;
Application excel = new _Excel.Application();
excel.Visible = true;
Workbook workbook = excel.Workbooks.Add();
Worksheet worksheet = workbook.Worksheets.Item["Sheet1"];
var range = worksheet.Cells.Range["A1"];
var cell = range.Cells[1,1];
cell.Value = 1234;
cell.NumberFormat = "#,##0";
cell.HorizontalAlignment = _Excel.XlHAlign.xlHAlignCenter;
cell.VerticalAlignment = _Excel.XlVAlign.xlVAlignCenter;
cell.WrapText = false;
cell.Orientation = 0;
cell.AddIndent = false;
cell.IndentLevel = 0;
cell.ShrinkToFit = false;
cell.ReadingOrder = XlReadingOrder.xlContext;
cell.MergeCells = false;
worksheet.Rows["1:1"].RowHeight = 84;
Debugger.Break();
string file = Path.Combine(Environment.CurrentDirectory, "test.xlsx");
if (File.Exists(file))
File.Delete(file);
workbook.Close(SaveChanges: true, Filename: file);
excel.Quit();
NOTES:
* For development and debugging, I have turned visibility on so I can see what is happening in the spreadsheet
* I have a breakpoint set so that I can pause execution and check
* Lastly, I release resources to avoid memory leaks
After the app finishes and the spreadsheet is closed, I open it manually and check the results.
Everything looks formatted correctly. Now it is time to update the code in the app. I recommend that you add the noted points to your own code so that you too can check your code.