C# 访问 EXCEL,将单元格格式设置为常规
在 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 = "@";
我也会在每个单元格的左上角看到这个“小错误”标志
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要回答第一个问题,您应该阅读这篇文章:http: //blogs.msdn.com/b/eric_carter/archive/2004/09/06/225989.aspx
可选参数部分不适用自从 C# 4.0 有可选参数以来,就不再这样了。
但有一个区别(文章中已说明)
对于第二个问题,您是否尝试过将单元格设置为“常规”并在代码中读出它?
我认为它是
sheet.Cells[row + n, col].NumberFormat = "General"
,其中"@"
是纯文本。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)
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.为了获得常规类型,作为应分配给
NumberFormat
属性的“魔术字符串”,请尝试使用空字符串,例如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.