xlsx到csv:日期格式从DD.MM.YYYY更改为DD.MM.YY

时间:2015-12-16 18:08:32

标签: excel date csv format

我绝望了所以我希望有人可以帮助我。 我正在将Excel工作表(.xlsx)导出为CSV文件(.csv)。 该表包含各种类型的信息,包括一些日期。 日期在Excel工作表中格式化为DD.MM.YYYY。 但是,在执行导出到CSV文件(文件>另存为> CSV文件)时,Excel会将日期格式更改为DD.MM.YY. 我需要将CSV文件中的数据导入R,日期必须是DD.MM.YYYY格式。

知道如何强制Excel保持格式化吗? 非常感谢和亲切的问候

1 个答案:

答案 0 :(得分:0)

您可以根据需要调整此短宏:

Option Explicit
Sub CSV_Maker()
   Dim r As Range
   Dim sOut As String, k As Long, M As Long
   Dim N As Long, nFirstRow As Long, nLastRow As Long
   Dim MyFilePath As String, MyFileName As String
   Dim fs, a, mm As Long
   Dim separator As String

   ActiveSheet.UsedRange
   Set r = ActiveSheet.UsedRange
   nLastRow = r.Rows.Count + r.Row - 1
   nFirstRow = r.Row
   separator = ","

   MyFilePath = "C:\TestFolder\"
   MyFileName = "whatever"
   Set fs = CreateObject("Scripting.FileSystemObject")
   Set a = fs.CreateTextFile(MyFilePath & MyFileName & ".csv", True)

   For N = nFirstRow To nLastRow
       k = Application.WorksheetFunction.CountA(Cells(N, 1).EntireRow)
       sOut = ""
       If k = 0 Then
           sOut = vbCrLf
       Else
           M = Cells(N, Columns.Count).End(xlToLeft).Column
           For mm = 1 To M
               sOut = sOut & Cells(N, mm).Text & separator
           Next mm
           sOut = Left(sOut, Len(sOut) - 1)
       End If
       a.writeline (sOut)
   Next

   a.Close
End Sub