Excel 附加具有相同数据类型但不同列名的CSV文件

Excel 附加具有相同数据类型但不同列名的CSV文件,excel,etl,business-intelligence,powerbi,powerquery,Excel,Etl,Business Intelligence,Powerbi,Powerquery,希望你做得很好。 我需要一些帮助。例如,我有3个csv文件: 1)文件1.csv,包含2条记录/行 firstname | lastname | city | country | emailaddress ----------------------------------------------------- alexf sdfsd mumbai india sdf@sdf.com ---------- asfd sdfsdf toronto canada dfsd

希望你做得很好。 我需要一些帮助。例如,我有3个csv文件:

1)文件1.csv,包含2条记录/行

firstname | lastname | city | country | emailaddress
-----------------------------------------------------
alexf     sdfsd    mumbai india sdf@sdf.com
----------
asfd      sdfsdf   toronto canada dfsd@sdf.com
first-name | last-name | currentcity | currentcountry | email-address
----------------------------------------------------------------------
asdf        sdfkjwl  sydney      australia      sdf@dsffwe.com
----------
lskjdf     sdlfkjlkj delhi       india           sdflkj@sdf.com
fname | lname | usercity | usercountry | email
-----------------------------------------------
sdf   sdflj auckland new zealand sdf@sdf.com
----------
sdfsdf sdf  venice   italy       sdf@dsf.com
2)secondfile.csv,包含2条记录/行

firstname | lastname | city | country | emailaddress
-----------------------------------------------------
alexf     sdfsd    mumbai india sdf@sdf.com
----------
asfd      sdfsdf   toronto canada dfsd@sdf.com
first-name | last-name | currentcity | currentcountry | email-address
----------------------------------------------------------------------
asdf        sdfkjwl  sydney      australia      sdf@dsffwe.com
----------
lskjdf     sdlfkjlkj delhi       india           sdflkj@sdf.com
fname | lname | usercity | usercountry | email
-----------------------------------------------
sdf   sdflj auckland new zealand sdf@sdf.com
----------
sdfsdf sdf  venice   italy       sdf@dsf.com
3)带有2条记录/行的userfile.csv

firstname | lastname | city | country | emailaddress
-----------------------------------------------------
alexf     sdfsd    mumbai india sdf@sdf.com
----------
asfd      sdfsdf   toronto canada dfsd@sdf.com
first-name | last-name | currentcity | currentcountry | email-address
----------------------------------------------------------------------
asdf        sdfkjwl  sydney      australia      sdf@dsffwe.com
----------
lskjdf     sdlfkjlkj delhi       india           sdflkj@sdf.com
fname | lname | usercity | usercountry | email
-----------------------------------------------
sdf   sdflj auckland new zealand sdf@sdf.com
----------
sdfsdf sdf  venice   italy       sdf@dsf.com
现在,我想创建一个单一的csv、excel、mysql或任何数据库表,在这些表中,我希望所有这些记录来自具有不同列/头名称但具有相同数据类型的所有不同csv文件。像这样:

singlecsvfile.csv

first_name | last_name | city | country |     email_address
--------------------------------------------------------
alexf        sdfsd       mumbai   india       sdf@sdf.com
asfd         sdfsdf      toronto canada       dfsd@sdf.com
asdf        sdfkjwl       sydney  australia   sdf@dsffwe.com
lskjdf      sdlfkjlkj     delhi    india      sdflkj@sdf.com
sdf         sdflj         auckland new zealand sdf@sdf.com
sdfsdf      sdf           venice   italy       sdf@dsf.com

实际上,我有50多个这样的文件,它们具有不同的列名,但由于数据源的类型不同,数据类型相同。你会建议我做什么,你会建议什么策略或方式,我应该如何实施。如果可能的话,请给我推荐简单的方法(excel/powerquery/powerBI)或代码(php/sql)以及一些描述。我需要快速或自动化的解决方案,比如数据映射。我找了很多,但找不到任何解决办法。如有建议,将不胜感激。谢谢

超级用户并不是真正的代码编写服务。也就是说,我有一段代码,基本上可以在vba中实现您想要的功能。它有一些评论,所以应该是可管理的。可能需要根据您的文件进行一些调整

Option Explicit
Global first_sheet As Boolean
Global append As Boolean


Sub process_folder()
Dim book_counter As Integer
Dim folder_path As String
Dim pWB As Workbook, sWB As Workbook, sWB_name As String
Dim pWS As Worksheet

    book_counter = 0
    first_sheet = True

    'Flag between appending in one sheet and copying into individual sheets
    append = True

    Set pWB = ActiveWorkbook
    Set pWS = pWB.ActiveSheet

    folder_path = "O:\Active\_2010\1193\10-1193-0015 Kennecott eagle\Phase 8500 - DFN Modelling\4. Analysis & Modelling\Phase 2 - DFN building\Export\fracture_properties\20140205"

    folder_path = verify_folder(folder_path)
    If folder_path = "NULL" Then
        Exit Sub
    End If

    'Get first file to open
    sWB_name = Dir(folder_path, vbNormal)

    'Loop through files
    Do While sWB_name <> ""

        'Open each file
        Workbooks.Open Filename:=folder_path & sWB_name
        Set sWB = Workbooks(sWB_name)

        Call process_workbook(pWB, sWB)

        'close file increment counter
        sWB_name = Dir()
        book_counter = book_counter + 1
    Loop

    'Number of files processed
    MsgBox ("Number of Fragment Files processed: " & book_counter)


End Sub

Sub process_workbook(pWB As Workbook, sWB As Workbook)

       If append Then
        Call append_all_sheets(pWB, sWB, 1)
       Else
        Call copy_all_sheets(pWB, sWB)
       End If

End Sub

Sub copy_all_sheets(pWB As Workbook, sWB As Workbook)
Dim ws As Worksheet

    For Each ws In sWB.Worksheets
        ws.Move After:=pWB.Sheets(pWB.Sheets.count)
    Next ws

End Sub

Sub append_all_sheets(pWB As Workbook, sWB As Workbook, headerRows As Long)

Dim lastCol As Long, lastRow As Long, pasteRow As Long
Dim count As Integer
Dim ws As Worksheet

    For Each ws In sWB.Worksheets
        lastCol = find_last_col(ws)
        lastRow = find_last_row(ws)
        pasteRow = find_last_row(pWB.Sheets(1))


        'Copy entire data range if its the first sheet otherwise leave of the header row
        If first_sheet Then
         '   ws.Range("A1").Resize(lastRow, lastCol).Copy

            pWB.Sheets(1).Range("A" & pasteRow).Resize(lastRow, lastCol).Formula = ws.Range("A1").Resize(lastRow, lastCol).Formula
                'Destination:=pWB.Sheets(1).pasteRow
        Else
            'pWB.Sheets(1).Formula = ws.Range("A1").Offset(headerRows, 0).Resize(lastRow - headerRows, lastCol).Formula
            pWB.Sheets(1).Range("A" & pasteRow).Resize(lastRow - headerRows, lastCol).Formula = ws.Range("A1").Offset(headerRows, 0).Resize(lastRow - headerRows, lastCol).Formula
        End If

        first_sheet = False
    Next ws

    sWB.Close (False)

End Sub

Function find_last_row(ws As Worksheet) As Long

    With ws
        If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
            find_last_row = .Cells.Find(What:="*", _
                          After:=.Range("A1"), _
                          Lookat:=xlPart, _
                          LookIn:=xlFormulas, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlPrevious, _
                        MatchCase:=False).Row
        Else
          find_last_row = 1
        End If
    End With


End Function

Function find_last_col(ws As Worksheet) As Long

    With ws
        If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
            find_last_col = .Cells.Find(What:="*", _
                          After:=.Range("A1"), _
                          Lookat:=xlPart, _
                          LookIn:=xlFormulas, _
                        SearchOrder:=xlByColumns, _
                        SearchDirection:=xlPrevious, _
                        MatchCase:=False).Column
        Else
          find_last_col = 1
        End If
    End With


End Function

Function verify_folder(path As String) As String

    If path = "" Then
        MsgBox ("Enter the Directory of the Fragment simulation files to process")
        verify_folder = "NULL"
        Exit Function
    End If

    If Not PathExists(path) Then
        MsgBox ("Directory does not exist")
        verify_folder = "NULL"
        Exit Function

    End If

    If Right(path, 1) <> "\" Then
            verify_folder = path & "\"
    End If

End Function

Function PathExists(pName) As Boolean
On Error Resume Next
    PathExists = (GetAttr(pName) And vbDirectory) = vbDirectory
End Function
选项显式
作为布尔值的全局第一张表
全局追加为布尔值
子进程_文件夹()
Dim book_计数器为整数
将文件夹路径设置为字符串
将pWB设置为工作簿,将sWB设置为工作簿,将sWB_名称设置为字符串
将pWS设置为工作表
簿记计数器=0
第一张纸=真
'在添加到一个工作表和复制到单个工作表之间的标记
append=True
设置pWB=ActiveWorkbook
设置pWS=pWB.ActiveSheet
文件夹\u path=“O:\Active\\u 2010\1193\10-1193-0015 Kennecott eagle\Phase 8500-DFN建模\4.分析与建模\Phase 2-DFN building\Export\fracture\U properties\20140205”
文件夹路径=验证文件夹(文件夹路径)
如果文件夹路径为NULL,则
出口接头
如果结束
'获取要打开的第一个文件
sWB_name=Dir(文件夹路径,vbNormal)
'循环浏览文件
在sWB_名称“”时执行
'打开每个文件
工作簿。打开文件名:=文件夹路径和sWB\U名称
设置sWB=工作簿(sWB_名称)
调用过程_工作簿(pWB、sWB)
'关闭文件增量计数器
sWB_name=Dir()
簿记计数器=簿记计数器+1
环
'已处理的文件数
MsgBox(“处理的片段文件数:”&book\u计数器)
端接头
子流程_工作簿(pWB作为工作簿,sWB作为工作簿)
如果追加,则
调用附加所有表格(pWB、sWB、1)
其他的
调用副本所有表格(pWB、sWB)
如果结束
端接头
子副本所有工作表(pWB作为工作簿,sWB作为工作簿)
将ws设置为工作表
对于sWB.工作表中的每个ws
ws.Move After:=pWB.Sheets(pWB.Sheets.count)
下一个ws
端接头
子附加所有工作表(pWB作为工作簿,sWB作为工作簿,headerRows作为长)
调暗lastCol为长、lastRow为长、粘贴ROW为长
将计数设置为整数
将ws设置为工作表
对于sWB.工作表中的每个ws
lastCol=查找最后一个col(ws)
lastRow=查找最后一行(ws)
粘贴行=查找最后一行(印刷电路板图纸(1))
'复制整个数据范围,如果它位于头行的第一个工作表之外
如果是第一张,那么
'ws.Range(“A1”).Resize(lastRow,lastCol).复制
pWB.Sheets(1).范围(“A”和粘贴行).调整大小(lastRow,lastCol).公式=ws.Range(“A1”).调整大小(lastRow,lastCol).公式
'目的地:=pWB.Sheets(1).pasteRow
其他的
'pWB.Sheets(1).Formula=ws.Range(“A1”).Offset(headerRows,0)。Resize(lastRow-headerRows,lastCol)。Formula
pWB.Sheets(1).Range(“A”和PasterRow).Resize(lastRow-headerRows,lastCol).公式=ws.Range(“A1”).偏移量(headerRows,0).Resize(lastRow-headerRows,lastCol).公式
如果结束
第一张纸=假
下一个ws
sWB.关闭(错误)
端接头
函数find_last_row(ws As Worksheet)尽可能长
与ws
如果Application.WorksheetFunction.CountA(.Cells)为0,则
查找最后一行=.Cells.find(内容:=“*”_
之后:=.范围(“A1”)_
看:=xlPart_
LookIn:=xl公式_
搜索顺序:=xlByRows_
搜索方向:=xlPrevious_
MatchCase:=False)。行
其他的
查找最后一行=1
如果结束
以
端函数
函数find_last_col(ws As Worksheet)尽可能长
与ws
如果Application.WorksheetFunction.CountA(.Cells)为0,则
查找最后一列=.Cells.find(What:=“*”_
之后:=.范围(“A1”)_
看:=xlPart_
LookIn:=xl公式_
SearchOrder:=xlByColumns_
搜索方向:=xlPrevious_
MatchCase:=False)。列
其他的
查找最后一列=1
如果结束
以
端函数
函数验证\u文件夹(路径为字符串)为字符串
如果路径为“”,则
MsgBox(“输入要处理的碎片模拟文件的目录”)
验证\u folder=“NULL”
退出功能
如果结束
如果路径不存在(路径),则
MsgBox(“目录不存在”)
验证\u folder=“NULL”
退出功能
如果结束
如果正确(路径1)“\”则
验证\u folder=path&“\”
如果结束
端函数
函数路径存在(pName)为布尔值
出错时继续下一步
PathExists=(GetAttr(pName)和vbDirectory)=vbDirectory
端函数

超级用户并不是真正的代码编写服务。也就是说,我有一段代码,基本上可以在vba中实现您想要的功能。它有一些评论,所以应该是可管理的。可能需要根据您的文件进行一些调整

Option Explicit
Global first_sheet As Boolean
Global append As Boolean


Sub process_folder()
Dim book_counter As Integer
Dim folder_path As String
Dim pWB As Workbook, sWB As Workbook, sWB_name As String
Dim pWS As Worksheet

    book_counter = 0
    first_sheet = True

    'Flag between appending in one sheet and copying into individual sheets
    append = True

    Set pWB = ActiveWorkbook
    Set pWS = pWB.ActiveSheet

    folder_path = "O:\Active\_2010\1193\10-1193-0015 Kennecott eagle\Phase 8500 - DFN Modelling\4. Analysis & Modelling\Phase 2 - DFN building\Export\fracture_properties\20140205"

    folder_path = verify_folder(folder_path)
    If folder_path = "NULL" Then
        Exit Sub
    End If

    'Get first file to open
    sWB_name = Dir(folder_path, vbNormal)

    'Loop through files
    Do While sWB_name <> ""

        'Open each file
        Workbooks.Open Filename:=folder_path & sWB_name
        Set sWB = Workbooks(sWB_name)

        Call process_workbook(pWB, sWB)

        'close file increment counter
        sWB_name = Dir()
        book_counter = book_counter + 1
    Loop

    'Number of files processed
    MsgBox ("Number of Fragment Files processed: " & book_counter)


End Sub

Sub process_workbook(pWB As Workbook, sWB As Workbook)

       If append Then
        Call append_all_sheets(pWB, sWB, 1)
       Else
        Call copy_all_sheets(pWB, sWB)
       End If

End Sub

Sub copy_all_sheets(pWB As Workbook, sWB As Workbook)
Dim ws As Worksheet

    For Each ws In sWB.Worksheets
        ws.Move After:=pWB.Sheets(pWB.Sheets.count)
    Next ws

End Sub

Sub append_all_sheets(pWB As Workbook, sWB As Workbook, headerRows As Long)

Dim lastCol As Long, lastRow As Long, pasteRow As Long
Dim count As Integer
Dim ws As Worksheet

    For Each ws In sWB.Worksheets
        lastCol = find_last_col(ws)
        lastRow = find_last_row(ws)
        pasteRow = find_last_row(pWB.Sheets(1))


        'Copy entire data range if its the first sheet otherwise leave of the header row
        If first_sheet Then
         '   ws.Range("A1").Resize(lastRow, lastCol).Copy

            pWB.Sheets(1).Range("A" & pasteRow).Resize(lastRow, lastCol).Formula = ws.Range("A1").Resize(lastRow, lastCol).Formula
                'Destination:=pWB.Sheets(1).pasteRow
        Else
            'pWB.Sheets(1).Formula = ws.Range("A1").Offset(headerRows, 0).Resize(lastRow - headerRows, lastCol).Formula
            pWB.Sheets(1).Range("A" & pasteRow).Resize(lastRow - headerRows, lastCol).Formula = ws.Range("A1").Offset(headerRows, 0).Resize(lastRow - headerRows, lastCol).Formula
        End If

        first_sheet = False
    Next ws

    sWB.Close (False)

End Sub

Function find_last_row(ws As Worksheet) As Long

    With ws
        If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
            find_last_row = .Cells.Find(What:="*", _
                          After:=.Range("A1"), _
                          Lookat:=xlPart, _
                          LookIn:=xlFormulas, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlPrevious, _
                        MatchCase:=False).Row
        Else
          find_last_row = 1
        End If
    End With


End Function

Function find_last_col(ws As Worksheet) As Long

    With ws
        If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
            find_last_col = .Cells.Find(What:="*", _
                          After:=.Range("A1"), _
                          Lookat:=xlPart, _
                          LookIn:=xlFormulas, _
                        SearchOrder:=xlByColumns, _
                        SearchDirection:=xlPrevious, _
                        MatchCase:=False).Column
        Else
          find_last_col = 1
        End If
    End With


End Function

Function verify_folder(path As String) As String

    If path = "" Then
        MsgBox ("Enter the Directory of the Fragment simulation files to process")
        verify_folder = "NULL"
        Exit Function
    End If

    If Not PathExists(path) Then
        MsgBox ("Directory does not exist")
        verify_folder = "NULL"
        Exit Function

    End If

    If Right(path, 1) <> "\" Then
            verify_folder = path & "\"
    End If

End Function

Function PathExists(pName) As Boolean
On Error Resume Next
    PathExists = (GetAttr(pName) And vbDirectory) = vbDirectory
End Function
Op