C#访问EXCEL,将单元格格式化为常规 [英] C# Accessing EXCEL, formatting cell as General

查看:321
本文介绍了C#访问EXCEL,将单元格格式化为常规的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#(通过COM对象)中处理excel单元格时,应该使用.Value或.Value2吗?Visual Studio 2010




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

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

他们之间的区别?



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

  sheet.Cells [row + n,col] .NumberFormat =XYZ; //不知道这里应该是什么

现在,当我分配一个数字0 34,即使我做了

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

我得到这个小错误注册在每个单元格的左上角

解决方案

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



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



但是有差异在文章中)


此属性[Value2]和Value
属性之间的唯一区别是Value2属性不t使用货币和日期
数据类型。您可以使用Double数据类型将这些数据类型格式化为
浮点数。


对于第二个问题,你是否尝试将单元格设置为General并以代码读出?



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


C#, Visual studio 2010

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

 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

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

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

解决方案

To answer the first question you should read this article: 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.

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.

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.

这篇关于C#访问EXCEL,将单元格格式化为常规的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆