excel - 在导入 Excel 之前过滤 CSV 文件

标签 excel csv

我想导入一个如下所示的 CSV 文件(逗号是分隔符):

x,y

这里,x 代表一个用户 ID,y 代表我要提取的值。

其次,我有一个 Excel 文件,它的第一列中的用户 ID 相似但少得多。我只想导入 Excel 文件中包含的那些用户的 y 值。

有谁知道这是怎么做到的吗?

最佳答案

您可以使用 ADO。大致:

Dim cn As Object
Dim rs As Object
Dim strFile As String
Dim strCon As String
Dim strSQL As String
Dim TextInput As String

''This is not the best way to refer to the workbook
''you want, but it is very convenient for notes
''It is probably best to use the name of the workbook.

strFile = ActiveWorkbook.FullName

''Note that if HDR=No, F1,F2 etc are used for column names,
''if HDR=Yes, the names in the first row of the range
''can be used.
''
''This is the ACE connection string, you can get more
''here : http://www.connectionstrings.com/excel

strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFile _
& ";Extended Properties=""Excel 12.0 Xml;HDR=YES"";"

TextInput = "[Text;FMT=Delimited;HDR=Yes;IMEX=2;DATABASE=Z:\docs]"

''Late binding, so no reference is needed
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")

cn.Open strCon

strSQL = "SELECT a.ID,a.Data " _
       & "FROM " & TextInput & ".[TestIn.csv] a " _
       & "INNER JOIN [Sheet1$] b ON a.ID=b.ID" _


rs.Open strSQL, cn, 3, 3


''Pick a suitable empty worksheet for the results

Worksheets("Sheet3").Cells(2, 1).CopyFromRecordset rs

''Tidy up
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing

关于excel - 在导入 Excel 之前过滤 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8526905/

相关文章:

c# - 从多个 excel 文档中读取数据并将它们写入另一个 excel 文档

excel - 为什么返回自身作为默认属性的对象会挂起 Excel 并使调试器崩溃?

python - Tkinter 用 {} 显示文本

mysql - 从 S3 文件构建临时表到 Redshift

r - write.csv 精度 R

python - 在 colaboratory 中从驱动器加载 xlsx 文件

excel - 使用 ObjPtr(Me) 返回自定义类实例的名称?

c# - 如何在 C# 中从 System.Drawing.Color 转换为 Excel.ColorFormat?更改评论颜色

r - dplyr 链中的 write.csv()

java - 从逗号分隔时转义英寸符号