We have seen here in a previous post how to set some values in cell. We'll see now how to format the cells.
The examples shown here are quite simple but as all - or so - properties methods take only objects and that the MSDN gives only sparingly the real type to use.
Note that for all the following examples and to focus on the different types we can use, I have defined two aliases :
using TExcel = Microsoft.Office.Tools.Excel;
using IExcel = Microsoft.Office.Interop.Excel;
How to set some basic properties :
cell.Font.Bold = true;
cell.HorizontalAlignment = IExcel.XlHAlign.xlHAlignCenter;
cell.VerticalAlignment = IExcel.XlVAlign.xlVAlignCenter;
cell.WrapText = true;
How to set the width on a column :
Note that here, the width is given in points.
How to set the formatting display on a cell :
How to find the correct formatting cell ? Well you can see in Excel directly in the cell format dialog editor. In the "custom" tab you will see some examples of the string you can give.
cell.NumberFormat = "0.00";
cell.NumberFormat = "#.##0.00";
cell.NumberFormat = "dd/MM/yyyy";