在 MS Excel 中打开一个分号分隔的文件,在我通过 C# Addin 调用 Workbook.Save() 后,分号将被替换为逗号

Open a semicolon seperated file in MS Excel, after I call Workbook.Save() via C# Addin the semicolons will be replaced with comma

我在 MS Excel 2013 中开发 C# 插件时出现奇怪的行为。 我的客户打开一个带有分号分隔符的 CSV 文件。

现在他可以单击我的插件按钮之一。其中一个按钮将调用 "Application.ActiveWorkbook.Save()" 以保存对文件的更改。

此调用后 MS Excel 将用逗号替换所有分号! 如果在 Excel API 中找不到解决此错误的解决方案。

当我用MSExcels蓝盘保存文件时,不会进行替换

为什么?

谁能帮帮我?

提前致谢!

-- 马库斯

如果您的 CSV 文件标准格式是带分号的,您可以尝试

ActiveWorkbook.SaveAs Filename:="text.csv", FileFormat:=xlCSV, CreateBackup:=False, local:=True

local:=True 将使用您环境中通常使用的任何定界符。

如果这不起作用,您可以尝试使用 vba 直接创建 csv 文件

根据您的需要调整以下代码(可在 http://www.mrexcel.com/forum/excel-questions/134185-xlcsv-%3D-comma-save-csv-%3D-semicolon-delimited.html#post1724209 找到)

Sub CreateCSV()

    Dim rCell As Range
    Dim rRow As Range
    Dim sOutput As String
    Dim sFname As String, lFnum As Long

    'Open a text file to write
    sFname = "C:\MyCsv.csv"
    lFnum = FreeFile

    Open sFname For Output As lFnum
    'Loop through the rows'
        For Each rRow In ActiveSheet.UsedRange.Rows
        'Loop through the cells in the rows'
        For Each rCell In rRow.Cells
            sOutput = sOutput & rCell.Value & ";"
        Next rCell
         'remove the last comma'
        sOutput = Left(sOutput, Len(sOutput) - 1)

        'write to the file and reinitialize the variables'
        Print #lFnum, sOutput
        sOutput = ""
     Next rRow

    'Close the file'
    Close lFnum

End Sub