vb.net 如何在vb.net中设置excel单元格格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30297272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:08:11  来源:igfitidea点击:

how to set excel cell format in vb.net

vb.netexcel

提问by user1220497

I'm modifying an existing application written in vb.net. There is an export option which exports information to an excel sheet. Before exporting the cell format is defined as follows.

我正在修改用 vb.net 编写的现有应用程序。有一个导出选项可将信息导出到 Excel 工作表。导出之前的单元格格式定义如下。

.Cells(cRow, 17).NumberFormat = "$#,##0.00_);($#,##0.00)"

this works fine. But according to the new requirement, currency symbol is dynamic. Therefore once I set the currency (eg : Malaysian riggit -MYR),

这工作正常。但根据新要求,货币符号是动态的。因此,一旦我设置了货币(例如:Malaysian riggit -MYR),

.Cells(cRow, 17).NumberFormat = "MYR#,##0.00_);(MYR#,##0.00)"

this gives an error "Unable to set the NumberFormat property of the Range class". there can be many currencies and I tried setting the currency using a variable.

这会产生错误“无法设置 Range 类的 NumberFormat 属性”。可以有多种货币,我尝试使用变量设置货币。

Dim strCurrency As String = ""
strCurrency = "SGD"
.Cells(cRow, 17).NumberFormat = ""+strCurrency +"#,##0.00_);("+strCurrency +"#,##0.00)"

All are giving me the same error. Someone please let me know how to set the custom currency symbol. thanks

所有人都给我同样的错误。有人请让我知道如何设置自定义货币符号。谢谢

采纳答案by Keith Mifsud

If I'm not mistaken any characters in an EXCEL numeric format must be enclosed in quotation marks so:

如果我没记错的话,EXCEL 数字格式中的任何字符都必须用引号引起来,这样:

Dim strCurrency As String = ""
strCurrency = "SGD"
.Cells(cRow, 17).NumberFormat = """"+strCurrency +"""#,##0.00_);("""+strCurrency +"""#,##0.00)"

回答by Siddharth Rout

Use this

用这个

.Cells(cRow, 17).NumberFormat = strCurrency & "#,##0.00_);(" & strCurrency & "#,##0.00)"

There is no need to use so many quotes as shown in the answer that is accepted.

没有必要使用被接受的答案中显示的那么多引号。

Explanation

解释

Your string

你的字符串

"MYR#,##0.00_);(MYR#,##0.00)"

"MYR#,##0.00_);(MYR#,##0.00)"

can also be written as

也可以写成

"MYR" & "#,##0.00_);(" & "MYR" & "#,##0.00)"

"MYR" & "#,##0.00_);(" & "MYR" & "#,##0.00)"

Now simply replace "MYR"with your variable

现在只需替换"MYR"为您的变量

回答by Top Systems

Enclose the currency symbol inside quotation :

将货币符号括在引号内:

Dim strCurrency = Chr(34) & "SGD" & Chr(34)
.Cells(cRow, 17).NumberFormat = String.Format("{0}#,##0.00_);({0}#,##0.00)", strCurrency)

The Chr(34)= "

Chr(34)="