将数据从* .csv复制到excel

我有一个csv文件,充满了来自温度传感器的输出,它具有date,温度控制器地址,温度传感器1值,温度传感器2值,热功率,冷却功率以及限制和报警状态,这里是一个空格在记事本中打开时添加可读性:

2/10/2016 14:52:26.2, 1, 73.9039, 74.89208, 14.63515, 0, F, None, None, None, 2/10/2016 14:52:36.594, 1, 73.75067, 74.86765, 25.21247, 0, F, None, None, None, 2/10/2016 14:52:47.165, 1, 73.66284, 74.83871, 35.95927, 0, F, None, None, None, 2/10/2016 14:52:57.788, 1, 73.59991, 74.79031, 47.17537, 0, F, None, None, None, 2/10/2016 14:53:8.381, 1, 73.54018, 74.75883, 58.62064, 0, F, None, None, None, 

但是,如果我打开它在Excel中,我得到这种格式:

 52:26.2, 1, 73.9039, 74.89208, 14.63515, 0, F, None, None, None 52:36.6, 1, 73.75067, 74.86765, 25.21247, 0, F, None, None, None 52:47.2, 1, 73.66284, 74.83871, 35.95927, 0, F, None, None, None 52:57.8, 1, 73.59991, 74.79031, 47.17537, 0, F, None, None, None 53:08.4, 1, 73.54018, 74.75883, 58.62064, 0, F, None, None, None 

我想在Excel中使用VBA从这个CSV文件或多个CSV文件的数据,基于用户select通过文件select器对话框到工作簿的graphics。 我遇到的问题是date和时间列不能正确显示。 我认为这与我用于数据的格式有关,但我不确定。 我使用的格式是

 m/d/yyyy h:mm:ss.000 

出于某种原因,这是在Excel文件中显示的数据:

 1/0/1900 0:52:26.200, 1, 73.90390, 74.89208, 14.64, 0, F, None, None, None 1/0/1900 0:52:36.600, 1, 73.75067, 74.86765, 25.21, 0, F, None, None, None 1/0/1900 0:52:47.200, 1, 73.66284, 74.83871, 35.96, 0, F, None, None, None 1/0/1900 0:52:57.800, 1, 73.59991, 74.79031, 47.18, 0, F, None, None, None 1/0/1900 0:53:08.400, 1, 73.54018, 74.75883, 58.62, 0, F, None, None, None 

我尝试格式化第一列作为格式的数字

 0.0000000000 

然后移动数据,但列只是空白,然后因为我不得不使用范围数据types才能够改变格式。 这里是我用来显示文件select器和移动数据的代码:

  Sub fileDialogStart() Dim fd As FileDialog Dim vrtSelectedItem As Variant Set fd = Application.FileDialog(msoFileDialogFilePicker) With fd If .Show = -1 Then For Each vrtSelectedItem In .SelectedItems ' Finds the last row in the current workbook With ThisWorkbook.Sheets(1) lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row End With ' Imports the data from the selected file Call importData(vrtSelectedItem) Next vrtSelectedItem Else End If End With Set fd = Nothing End Sub Sub importData(ByVal filePath As String) Dim targetWorkbook As Workbook, sourceWorkbook As Workbook Dim finalRow As Integer 'Dim targetData as Range Set sourceWorkbook = Workbooks.Open(filePath) With sourceWorkbook.Sheets(1) ' Finds the number of rows in the file selected from the file picker finalRow = .Cells(.Rows.Count, "A").End(xlUp).Row ' Stores the first column targetData = .Range(.Cells(1, 1), .Cells(finalRow, 1)) End With ' Sets the number format 'targetData.NumberFormat = "0.0000000000" With ThisWorkbook.Sheets(1) ' Puts the new data into the current workbook after the last row of data ' in case there is already data in the workbook from a previous import .Range(.Cells(lastRow, 1), .Cells(lastRow + finalRow - 1, 1)) = targetData End With With sourceWorkbook.Sheets(1) ' Gets the rest of the data from the file selected sourceData = .Range(.Cells(1, 2), .Cells(finalRow, 10)) End With With ThisWorkbook.Sheets(1) ' Puts the rest of the data into the current workbook .Range(.Cells(lastRow, 2), .Cells(lastRow + finalRow - 1, 10)) = sourceData End With sourceWorkbook.Save ThisWorkbook.Save sourceWorkbook.Close False End Sub 

fileDialogStart()子文件在最初用于绘制的子文件中调用,在导入数据之后,我将整个第一列的格式设置为m/d/yyy h:mm:ss.000 。 也许这个问题与我打开csv文件作为工作簿的事实有关? 我不确定。 任何帮助将不胜感激,谢谢!

Workbooks.OpenText方法允许您指定每个字段的TextFileColumnDataTypes属性 。 这相当于Range.TextToColumns方法 。 为第一个字段指定xlMDYFormat格式足以使date时间正确(包括其秒数)。

 Sub openCSV() Dim fp As String, fn As String, wbCSV As Workbook fp = Environ("TMP") fn = "datetemp.csv" Debug.Print fp & Chr(92) & fn Workbooks.OpenText Filename:=fp & Chr(92) & fn, DataType:=xlDelimited, _ Tab:=False, Semicolon:=False, Space:=False, _ Other:=False, Comma:=True, FieldInfo:=Array(1, xlMDYFormat), _ Local:=True With ActiveWorkbook With Worksheets(1) With .Columns(1) .NumberFormat = "mm/dd/yyyy hh:mm:ss.000" .AutoFit End With End With End With End Sub 

本地:=真可能不希望取决于你自己的区域设置。 它对我的美国默认值工作正常。

Excel正在做一些奇怪的事情; 但是当我运行这个:

 Sub WTF() Dim FilesToOpen Dim wf As WorksheetFunction Set wf = Application.WorksheetFunction Dim ary, bry, DQ As String DQ = Chr(34) FilesToOpen = Application.GetOpenFilename _ (FileFilter:="Text Files (*.csv), *.csv", Title:="Text Files to Open") Close #1 Open FilesToOpen For Input As #1 j = 1 Do While Not EOF(1) Line Input #1, TextLine ary = Split(TextLine, ",") bry = Split(ary(0), " ") Cells(j, 1).Formula = "=datevalue(" & DQ & bry(0) & DQ & ")+timevalue(" & DQ & bry(1) & DQ & ")" Cells(j, 1).NumberFormat = "mm/dd/yyyy hh:mm:ss.000" For k = 2 To 10 Cells(j, k) = ary(k - 1) Next k j = j + 1 Loop Close #1 End Sub 

在您发布的数据上,我得到:

在这里输入图像说明

我无法解释的是为什么Excel直接打开文件这么差的工作。

也许有人可以解释这一点。