0

我有一个带有分号分隔符的 CSV,我想将其转换为常规 Excel 工作表。我设法用下面的代码做到了这一点,但我一定犯了一个错误,因为原始文件中不以零开头的带小数的数字在 Excel 中显示为没有小数分隔符的数字。当我在 Excel 中手动打开 CSV 时,结果会很好,因此它必须是使用脚本执行此操作的副作用。

例如:

在 CSV 中有一行:

2013-03-10 17:00:15; 闲置的; 2,272298;; 0,121860

在 Excel 工作表中,这变为:

2013-03-10 17:00 | 闲置 | 2.272.298| | 0,121860

在 excel 中手动打开会给出:

2013-03-10 17:00 | 闲置 | 2,272298| | 0,121860

有人可以告诉我我可以/应该改变什么以将小数保留为 Excel 中的小数吗?可能是一种告诉 Excel 哪个符号代表小数分隔符或强制它使用欧洲格式的参数的方法?

亲切的问候,尼科

这是我目前拥有的脚本,其中csvFile是一个字符串,其中包含原始文件的完整路径,并且excelFile是一个字符串,其中包含我要存储新 Excel 工作表的位置的完整路径。

Set objExcel = CreateObject("Excel.Application")                   'use excel  
objExcel.Visible = true                                            'visible  
objExcel.displayalerts=false                                       'no warnings 

objExcel.Workbooks.Open(csvFile)                                   'open the file  
objExcel.ActiveWorkbook.SaveAs excelFile, -4143, , , False, False  'save as xls  
objExcel.Quit                                                      'close excel
4

4 回答 4

1

在您的 csvFile 所在的文件夹中创建一个schema.ini文件,并根据此处给出的规则对其进行描述。

进一步阅读:导入文本文件

于 2013-03-21T13:02:45.767 回答
0

有几种可能的方法,我将介绍一种我喜欢的方法:

  1. 开始录制宏
  2. 创建新工作簿
  3. 从该工作簿转到数据 > 从文本,然后选择 CSV 文件,然后您可以进行有关值分隔符、小数分隔符、千位分隔符的所有必需设置。也可以为每一列选择特定的数据类型。
  4. 添加 CSV 内容后,转到数据 > 连接并删除连接。数据将保留在工作表中,但不再有活动连接。
  5. 以 xls 名称保存工作簿
  6. 停止录制

现在根据自己的喜好调整脚本。

于 2013-03-21T13:06:33.060 回答
0

一般来说,Excel 尊重系统的区域设置。然而,CSV 导入有时对“正确”格式有自己的想法,特别是当导入的文件具有扩展名.csv.

我会尝试以下。将文件重命名为.txtor.tsv并像这样导入它:

objExcel.Workbooks.OpenText csvFile, , , 1, 1, False, False, True
于 2013-03-21T15:24:38.127 回答
0

我做了一个工作。我现在创建 CSV 文件的副本,在其中替换所有逗号,后跟一个数字。虽然不是很有效,但它确实为 Excel 提供了它想要的东西,而且对于像我这样没有经验的程序员来说,它足够简单。这样做时,一所大学要求我也删除第一列中具有重复值的空格和条目(在这种情况下为时间戳)。

结果是这个脚本

    'csvFile   is a string with the full path to the file. e.g. "C:\\Program Files\\Program\\data.csv"
    'tempFile  is a string with the full path to the file. e.g. "C:\\Temp\\temp.csv"
    'excelfile is a string with the full path to the file. e.g. "D:\\Data\\sheet.xls"

    Set fs=CreateObject("Scripting.FileSystemObject")
    Set writeFile = fs.CreateTextFile(tempFile,True)
    Set readFile = fs.OpenTextFile(csvFile)

    ' regular expression to remove leading whitespaces
    Set regular_expression = New RegExp
        regular_expression.Pattern = "^\s*"
        regular_expression.Multiline = False

    ' regular expression to change the decimal seperator into a point        
    Set regular_expression2 = New RegExp
        regular_expression2.Global = True
        regular_expression2.Pattern = ",(?=\d)"
        regular_expression2.Multiline = False

    'copy the original file to the temp file and apply the changes
    Do Until readFile.AtEndOfStream
        strLine= readFile.ReadLine
        If (StrComp(current_timestamp,Mid(strLine, 1, InStr(strLine,";")),1)<>0) Then
        If (Len(previous_line) > 2) Then
            previous_line = regular_expression2.replace(previous_line,".")
            writeFile.Write regular_expression.Replace(previous_line, "") & vbCrLf
        End if
        End if
        current_timestamp = Mid(strLine, 1, InStr(strLine,";"))
        previous_line = strLine
    Loop
    readFile.Close
    writeFile.Close

    Set objExcel = CreateObject("Excel.Application")    ' use excel
    objExcel.Visible = true                             ' visible
    objExcel.displayalerts=false                        ' no warning pop-ups
    objExcel.Workbooks.Open(tempFile)                   ' open the file
    objExcel.ActiveWorkbook.SaveAs excelfile, -4143, , , False, False   'save as excelfile
    fs.DeleteFile tempFile                              ' clean up the temp file

我希望这对其他人也有用。

于 2013-03-22T09:28:08.873 回答