C#访问EXCEL,格式化一般的单元格

C#,Visual Studio 2010

当在C#中操作Excel单元格(通过COM对象)时,我应该使用.Value还是.Value2? 那是

sheet.Cells[row + n, col].Value = "Hello world" 

要么

  sheet.Cells[row + n, col].Value2 = "Hello world" 

他们之间有什么不同?

另外,如何将单元格格式设置为“常规”?

 sheet.Cells[row + n, col].NumberFormat = "XYZ"; // Not sure what should be here 

现在,当我分配一个数字“0,34”的单元格,即使我这样做

 sheet.Cells[row + n, col].NumberFormat = "@"; 

我在每个单元格的左上angular出现这个“小错误”

要回答第一个问题,您应该阅读这篇文章: http : //blogs.msdn.com/b/eric_carter/archive/2004/09/06/225989.aspx

可选参数部分不再适用,因为C#4.0具有可选参数。

但有一个区别(在文章中指出)

此属性[Value2]和Value属性之间的唯一区别是Value2属性不使用货币和date数据types。 您可以使用Double数据types将使用这些数据types格式化的值作为浮点数返回。

对于第二个问题,你有没有试过把单元格设置为“常规”,并在代码中读出来?

我认为它是sheet.Cells[row + n, col].NumberFormat = "General" ,其中"@"是纯文本。

为了得到Generaltypes,作为应该被分配给NumberFormat属性的“魔术string”尝试使用空string,例如

 sheet.Cells[5, 7].NumberFormat = ""; // sets the cell type to the General type