ADO 读写文本文件

    ' 创建配置文件
   
   
    Open ThisWorkbook.Path & "schema.ini" For Append As #1
   
        Print #1, "[table2.csv] "
        Print #1, "Format=CSVDelimited "
        Print #1, "ColNameHeader=False"
        Print #1, "MaxScanRows=0"
        Print #1, "Col1=工号   Text   Width   50  "
        Print #1, "Col2=姓名   Text   Width   50  "
        Print #1, "Col3=入职日期   DateTime"
        Print #1, "Col4=班别   Text   Width   50  "
        Print #1, "Col5=职位   Text   Width   50  "
        Print #1, "Col6=分值补贴   Single"
        Print #1, "Col7=计件分钟   Single"
        Print #1, "col8=上班分钟   Single"
        Print #1, "col9=效率   Single"
        Print #1, " "
        Print #1, "[table1.csv]"
        Print #1, "Format=CSVDelimited  "
        Print #1, "ColNameHeader=False"
        Print #1, "MaxScanRows=0"
        Print #1, "Col1=c1   Text   Width   50  "
        Print #1, "Col2=c2   Text   Width   50  "
        Print #1, "Col3=c3   Text   Width   50  "
        Print #1, "Col4=c4   Text   Width   50  "
        Print #1, "Col5=c5   Text   Width   50  "
        Print #1, "Col6=工号   Text   Width   50  "
        Print #1, "Col7=c7   Text   Width   50  "
        Print #1, "Col8=款号   Text   Width   50  "
        Print #1, "Col9=c9   Text   Width   50  "
        Print #1, "Col10=c10   Text   Width   50  "
        Print #1, "Col11=c11   Text   Width   50  "
        Print #1, "Col12=c12   Text   Width   50  "
        Print #1, "Col13=c13   Text   Width   50  "
        Print #1, "Col14=c14   Text   Width   50  "
        Print #1, "Col15=c15   Text   Width   50  "
        Print #1, "Col16=c16   Text   Width   50  "
        Print #1, "Col17=车种   Text   Width   50  "
      
   
    Close #1


    On Error Resume Next
   
    Dim cnn As New ADODB.Connection
    cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ThisWorkbook.Path & ";Extended Properties='text;HDR=No;FMT=Fixed';"
   
    If cnn.State = 0 Then
        Set cnn = Nothing
        MsgBox ("数据库连接失败!")
        Exit Sub
    End If
   
    Dim rec As New ADODB.Recordset
   
    rec.Open "select distinct t2.*, (t1.c1 + '-' + t1.c2 + '-1') as 更新时间, t1.款号, t1.车种 from table2.csv as t2" _
        & " left join table1.csv as t1 on t1.工号 = t2.工号" _
        & " order by t2.职位, t2.班别, t2.工号, t1.款号", cnn

原文地址:https://www.cnblogs.com/lbnnbs/p/4785011.html