C#访问EXCEL,将单元格格式化为常规

时间:2022-08-14 20:22:36

C#, Visual studio 2010

C#,Visual Studio 2010

When manipulating excel cells in C# (via an COM object), should I use the .Value or .Value2 ? that is

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

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

or

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

What is the difference between them to ?

他们之间有什么区别?

Also, how do I set a cell format to "General" ?

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

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

Right now when I assign a cell with the number "0,34" and even if I do

现在当我指定一个数字为“0,34”的单元格时,即使我这样做了

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

I get this "little error" sign up in the left corner in each cell

我在每个单元格的左角收到这个“小错误”注册

2 个解决方案

#1


4  

To answer the first question you should read this article: http://blogs.msdn.com/b/eric_carter/archive/2004/09/06/225989.aspx

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

The optional parameters part doesn't apply anymore since C# 4.0 has optional parameters.

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

But there IS a difference (stated in the article)

但是有一点不同(在文章中说明)

The only difference between this property [Value2] and the Value property is that the Value2 property doesn’t use the Currency and Date data types. You can return values formatted with these data types as floating-point numbers by using the Double data type.

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

For the second question, have you tried setting a cell to 'General' and reading it out in code?

对于第二个问题,您是否尝试将单元格设置为“常规”并在代码中读取它?

I think it's sheet.Cells[row + n, col].NumberFormat = "General", where "@" is pure text.

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

#2


1  

In order to get the General type, as "magic string" that should to be assigned to the NumberFormat property try to use the empty string, e.g.

为了获得General类型,作为应该分配给NumberFormat属性的“magic string”,尝试使用空字符串,例如

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

#1


4  

To answer the first question you should read this article: http://blogs.msdn.com/b/eric_carter/archive/2004/09/06/225989.aspx

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

The optional parameters part doesn't apply anymore since C# 4.0 has optional parameters.

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

But there IS a difference (stated in the article)

但是有一点不同(在文章中说明)

The only difference between this property [Value2] and the Value property is that the Value2 property doesn’t use the Currency and Date data types. You can return values formatted with these data types as floating-point numbers by using the Double data type.

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

For the second question, have you tried setting a cell to 'General' and reading it out in code?

对于第二个问题,您是否尝试将单元格设置为“常规”并在代码中读取它?

I think it's sheet.Cells[row + n, col].NumberFormat = "General", where "@" is pure text.

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

#2


1  

In order to get the General type, as "magic string" that should to be assigned to the NumberFormat property try to use the empty string, e.g.

为了获得General类型,作为应该分配给NumberFormat属性的“magic string”,尝试使用空字符串,例如

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