1

I'm writing a VB.NET application that generates an Excel file.

My intention here is to write a particular formula that uses CONCATENATE in a cell.

Now, the following line of code fires the above exception:

0) tSheet.Cells(tIncRow + ItemIndex * PixelIndex + PixelIndex, 2).Formula = 
   "=CONCATENATE(" & Pixels(PixelIndex) & ";Batches!J3)"

The following row does NOT raise the exception. (It's simply the row above without the = at the beginning. The fact that it doesn't raise the exception means that the indexes are used properly; I'll get rid of them in the following passages to ease the notation). Also, if I manually put in Excel an = in front of the very same formula, then the formula gives a correct result (it correctly grabs Batches!J3)

1) tSheet.Cells(tIncRow + ItemIndex * PixelIndex + PixelIndex, 2).Formula = 
   "CONCATENATE(" & Pixels(PixelIndex) & ";Batches!J3)"

The following line also runs without problem:

2) tSheet.Cells(indexes).Formula = "=CONCATENATE(" & Pixels(PixelIndex) & ")"

This line works as well:

3) tSheet.Cells(indexes).Formula = "=CONCATENATE(Batches!J3)"

It seems that only the combination of 2) and 3) raises the exception.

I'm using Visual Studio 2012, Excel 2007, and I'm including Microsoft Excel 12.0 Object Library

Thanks for any help!

4

1 回答 1

1

您还没有提到您的语言环境,所以以防万一 Excel 期望参数分隔符是逗号(如在 en-UK 语言环境中)而不是分号(如在 de-DE 语言环境中),然后尝试:

tSheet.Cells(tIncRow + ItemIndex * PixelIndex + PixelIndex, 2).Formula = 
    "=CONCATENATE(" & Pixels(PixelIndex) & ",Batches!J3)"

(编辑)如果确实有效,那么您可以使用以下内容使其独立于语言环境:

Dim ci As CultureInfo = System.Globalization.CultureInfo.CurrentCulture
Dim listSep As String = ci.TextInfo.ListSeparator
tSheet.Cells(tIncRow + ItemIndex * PixelIndex + PixelIndex, 2).Formula =
    "=CONCATENATE(" & Pixels(PixelIndex) & listSep & "Batches!J3)"

这只是从 Windows 文化中获取数字列表分隔符。它确实假定 Excel 使用 运行System.Globalization.CultureInfo.CurrentCulture,请参阅以下两个 MSDN 参考以获取有关此内容的更多详细信息以及您可能遇到的特殊情况:

(结束编辑)

如果这不起作用,那么我们可能需要更多信息 - 请参阅我对您原始问题的评论。

于 2013-05-27T17:12:59.753 回答