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

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

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

这工作正常。 但是根据新的要求,货币符号是dynamic的。 因此,一旦我设置货币(例如:马来西亚钻石–MYR),

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

这给出了一个错误“ 无法设置Range类的NumberFormat属性 ”。 可以有很多货币,我尝试使用一个variables设置货币。

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

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

如果我没有弄错,EXCEL数字格式的任何字符都必须用引号括起来,所以:

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

用这个

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

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

说明

你的string

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

也可以写成

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

现在简单地用你的variablesreplace"MYR"

将货币符号包含在引号内:

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

Chr(34) = "