在 Excel 或 OpenOffice 中的公共列上合并两个电子表格

发布于 2024-10-02 13:08:41 字数 560 浏览 1 评论 0 原文

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

例如:将“A”与“B”连接等于“结果”。如果一个表具有另一个表中不存在的键值,则将其保留为空白。

== 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”执行此操作

建议?

I have two CSV files with a common column and I want to "Join" the tables together on the common column.

For example: Join 'A' with 'B' equals 'Result'. If a one table has a key value that does not exist on in the other table its just left as blank.

== 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 

I know how to do this with an SQL database but I have never done it with "Excel" or "OpenOffice.org Calc"

Suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

也只是曾经 2024-10-09 13:08:41

在 Excel 中,vlookup 可以完成您所要求的部分操作。具体来说,您可以使用 vlookup 执行左外连接或右外连接,但不能使用完整外连接(如表结果)。

要为上面的示例执行外部联接,请将以下内容添加到“Table B”的 C2(或复制“Table 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

然后您应该能够扩展它以处理多行和多个导入列。

In Excel, vlookup can do part of what you're asking. Specifically, you can use vlookup to do a left or right outer join, but not a full outer join (like your table result).

To do an outer join for your example above, add the following to the C2 of "Table B" (or copy "Table B" and then do this):

=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

You should then be able to expand it to deal with multiple rows and multiple imported columns.

苏别ゝ 2024-10-09 13:08:41

在 Excel 中,您可以使用 VLOOKUP 来实现此目的。
假设您在 Excel 的 A 列和 B 列中列出了表 A 中的数据。
表 B 中的数据列在 E 列和 F 列中。
现在,转到 C 列中的第一行并输入:

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

这告诉它尝试将 A 列与 E 列匹配,并抓取靠近我们找到它的位置的第二列中的所有内容并将其放置在 C 列中。
现在自动填充 C 列中的其余行以匹配其余数据。

In Excel, you use VLOOKUP for that.
Assume you have the data in Table A listed in columns A and B in Excel.
And the data in Table B list in columns E and F.
Now, go to the first row in column C and enter:

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

This tells it to try to match column A with column E, and grab whatever is in the 2nd column near where we found it and place it in column C.
Now autofill the rest of the rows in column C to match the rest of the data.

一抹苦笑 2024-10-09 13:08:41

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

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

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

希望这有帮助。

If you can use Excel, there is a Query from Excel Files function:

  • Define name for primary table - Table A (Formulas tab -> Define name)
  • Define name for secondary table - Table B
  • Go to Data tab, select "From Other Sources", and from the dropdown, select "From Microsoft Query"
  • Select your CSV file and confirm that you want to merge the columns manually
  • In the following window "Query from Excel Files", drag&drop the Name column of Table A into the Name column of Table B - a link between these columns will be created
  • Go to File menu, click "Return Data to MS Office Excel", an Import Data dialog will pop up
  • Select the sheet into which you would like the matched data to be imported
  • Click OK - you should see matched data with columns from both tables

Or if you don't mind uploading your CSV files to an online service, you can use for example http://www.gridoc.com/join-tables and join the spreadsheets using drag&drop (Disclaimer: I am author of the tool).

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文