In Visual Studio you should add a reference to Excel in your project. You will find Excel in the COM tab of the Add Reference dialog. The name is Microsoft Excel 12.0 Object Library (or a slightly different name if you are using another version of Excel).
In your project you are then able to use types in the Microsoft.Office
namespaces. For instance you would probably want to use the Microsoft.Office.Interop.Excel.Constants.xlCenter
enumeration constant. If you need the numerical value you simply cast it to an integer:
Int32 xlCenterValue = (Int32) Microsoft.Office.Interop.Excel.Constants.xlCenter;
Or you could create a function as you have outlined in your question:
public Int32 ExcelConstant(String constantName) {
return Enum.Parse(
typeof(Microsoft.Office.Interop.Excel.Constants),
constantName
);
}
If you want to call the Excel automation API from managed code you probably don't care about the numerical values of the enumerations. Instead you should use the enumeration directly in your code:
var worksheet = (Worksheet) excel.ActiveWorkbook.ActiveSheet;
var cell = (Range) worksheet.Cells[1, 1];
cell.HorizontalAlignment = Constants.xlCenter;