Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何检索给定字符串文字的枚举值_C#_Excel - Fatal编程技术网

C# 如何检索给定字符串文字的枚举值

C# 如何检索给定字符串文字的枚举值,c#,excel,C#,Excel,需要获取给定字符串文字的枚举值,如“xlCenter”(这些值是从Excel宏剪切和粘贴的)。 我想通过com封送处理检索实际常量值(int)-4108=“xLCenter”这可能吗?如果是,怎么做 理想情况下,我正在寻找这样的功能 public int ExcelConstant(string constantName) { ...} 谢谢在Visual Studio中,您应该在项目中添加对Excel的引用。您将在添加参考对话框的COM选项卡中找到Excel。名称为Microsoft Ex

需要获取给定字符串文字的枚举值,如“xlCenter”(这些值是从Excel宏剪切和粘贴的)。 我想通过com封送处理检索实际常量值(int)-4108=“xLCenter”这可能吗?如果是,怎么做

理想情况下,我正在寻找这样的功能

public int ExcelConstant(string constantName)
{ ...} 

谢谢

在Visual Studio中,您应该在项目中添加对Excel的引用。您将在添加参考对话框的COM选项卡中找到Excel。名称为Microsoft Excel 12.0对象库(如果您使用的是其他版本的Excel,则名称稍有不同)

在项目中,您可以使用
Microsoft.Office
名称空间中的类型。例如,您可能希望使用
Microsoft.Office.Interop.Excel.Constants.xlCenter
枚举常量。如果需要数值,只需将其转换为整数:

Int32 xlCenterValue = (Int32) Microsoft.Office.Interop.Excel.Constants.xlCenter;
或者,您可以创建一个问题中概述的函数:

public Int32 ExcelConstant(String constantName) {
  return Enum.Parse(
    typeof(Microsoft.Office.Interop.Excel.Constants),
    constantName
  );
}
如果希望从托管代码调用Excel自动化API,则可能不关心枚举的数值。相反,您应该在代码中直接使用枚举:

var worksheet = (Worksheet) excel.ActiveWorkbook.ActiveSheet;
var cell = (Range) worksheet.Cells[1, 1];
cell.HorizontalAlignment = Constants.xlCenter;

在VisualStudio中,您应该在项目中添加对Excel的引用。您将在添加参考对话框的COM选项卡中找到Excel。名称为Microsoft Excel 12.0对象库(如果您使用的是其他版本的Excel,则名称稍有不同)

在项目中,您可以使用
Microsoft.Office
名称空间中的类型。例如,您可能希望使用
Microsoft.Office.Interop.Excel.Constants.xlCenter
枚举常量。如果需要数值,只需将其转换为整数:

Int32 xlCenterValue = (Int32) Microsoft.Office.Interop.Excel.Constants.xlCenter;
或者,您可以创建一个问题中概述的函数:

public Int32 ExcelConstant(String constantName) {
  return Enum.Parse(
    typeof(Microsoft.Office.Interop.Excel.Constants),
    constantName
  );
}
如果希望从托管代码调用Excel自动化API,则可能不关心枚举的数值。相反,您应该在代码中直接使用枚举:

var worksheet = (Worksheet) excel.ActiveWorkbook.ActiveSheet;
var cell = (Range) worksheet.Cells[1, 1];
cell.HorizontalAlignment = Constants.xlCenter;

我不确定这是否是您想要的确切答案,但我建议您看看
Microsoft.Office.Interop.Excel.Constants


我不确定这是否是您想要的确切答案,但我建议您看看
Microsoft.Office.Interop.Excel.Constants

您可以使用