代码之家  ›  专栏  ›  技术社区  ›  Steven Smethurst

在Excel或OpenOffice的公共列上联接两个电子表格

  •  27
  • Steven Smethurst  · 技术社区  · 14 年前

    我有两个带有公共列的csv文件,我想将公共列上的表“连接”在一起。

    例如:用“b”等于“result”联接“a”。如果一个表的键值在另一个表中不存在,则将其保留为空。

    == Table A ==        == Table B ==        == Table result ==
    Name  ,Age           Name  ,Sex           Name ,Age ,Sex
    Bob   ,37     +      Bob   ,Male     =>   Bob  ,37  ,Male
    Steve ,12            Steve ,Male          Steve,12  ,Male
    Kate  , 7                                 Kate , 7  , 
                         Sara  ,Female        Sara ,    ,Female 
    

    我知道如何处理SQL数据库,但我从未使用过“excel”或“openoffice.org calc”。

    建议?

    3 回复  |  直到 6 年前
        1
  •  32
  •   dwenaus    13 年前

    在Excel中, vlookup 可以做你要求的一部分。具体来说,您可以使用vlookup执行左或右外部联接,但不能执行完全外部联接(如表结果)。

    要对上面的示例进行外部联接,请将以下内容添加到“表B”的C2中(或复制“表B”,然后执行此操作):

    =vlookup(
        a2, # the cell value from the current table to look up in the other table
        table_a!$1:$174832718, # the other table
                               # don't manually type this--select the entire 
                               # other table while the cursor is editing this
                               # cell, then add the "$"s--Excel doesn't
                               # automatically add them
                               # (the syntax here is for different sheets in
                               # the same file, but Excel will fill this in 
                               # correctly for different files as well)
        2, # the column to get from the other table (A=1, B=2, etc.)
        FALSE) # FALSE=only get exact matches TRUE=find approx. matches if no exact match
    

    然后您应该能够展开它来处理多行和多个导入的列。

        2
  •  8
  •   BeemerGuy    14 年前

    在Excel中,您使用 VLOOKUP 为了这个。
    假设您在Excel的A列和B列中列出了表A中的数据。
    表B中的数据列在E列和F列中。
    现在,转到C列的第一行并输入:

    =VLOOKUP(A:A,E:F,2,FALSE) 
    

    这告诉它尝试将A列与E列相匹配,并在第2列中找到靠近找到它的内容,然后将其放入C列。
    现在自动填充C列中的其余行以匹配其余数据。

        3
  •  2
  •   endriju    7 年前

    如果可以使用Excel,则有“从Excel文件查询”功能:

    • 定义主表的名称-表A(公式选项卡->定义名称)
    • 定义辅助表的名称-表B
    • 转到“数据”选项卡,选择“从其他源”,并从下拉列表中选择“从Microsoft查询”
    • 选择csv文件并确认要手动合并列
    • 在下面的“从Excel文件查询”窗口中,将表A的“名称”列拖放到表B的“名称”列中-将创建这些列之间的链接
    • 进入“文件”菜单,单击“将数据返回MS Office Excel”,系统将弹出“导入数据”对话框。
    • 选择要导入匹配数据的工作表
    • 单击“确定”-您应该看到与两个表中的列匹配的数据

    或者,如果您不介意将您的csv文件上传到在线服务,您可以使用例如 http://www.gridoc.com/join-tables 并使用拖放(免责声明:我是该工具的作者)加入电子表格。

    希望这有帮助。