可以参考一下RecordSet的GetString方法:dim rsData as adodb.recordset dim strTxt as adodb.recordset'省略初始化recordset代码 rsData.Open "select 客户编号,客户名称,应付金额 from..." 'select要返回的记录 strTxt="客户编号,客户名称,应付金额" & vbcrlf '列头 strTxt=rs.GetString(adClipString, , , vbCrLf) '放到字符串中 '写到文件里 Open "c:\test.txt" For Output As #1 Print #1, strTxt Close #1
楼主可以参照我的部门代码(是将一个EXCEL文件中的数据转换成用友文本接口): Open "c:\UFIDA.txt" For Output As #1 '文本文件头 Print #1, "填制凭证,V800" For i_Row = 1 To ar_Tmp.RecordCount
If Trim(ar_Tmp![Doc No]) <> "" And ar_Tmp![L Debit] + ar_Tmp![L Credit] <> 0 Then '制单日期 If Not IsNull(ar_Tmp![Eff Date]) Then If IsDate(ar_Tmp![Eff Date]) Then Tmp_dBill_Date = Format(ar_Tmp![Eff Date], "YYYY-MM-DD") Else Tmp_dBill_Date = Format(Now(), "YYYY-MM-DD") End If Else Tmp_dBill_Date = Format(Now(), "YYYY-MM-DD") End If '凭证类别字 Tmp_cSign = Trim(txtcSign.Text) '业务(凭证)号(取EXCEL中的后四位) Tmp_iNo_ID = Right(ar_Tmp![Doc No], 4) '附单据数 默认为0 Tmp_iDoc = 0 '摘要(最长为30) '应对方公司要求,摘要栏写批次名称 'If Not IsNull(ar_Tmp![Doc Dec]) Then If Not IsNull(ar_Tmp![Name]) Then If LenB(ar_Tmp![Name]) > 60 Then Tmp_cDigest = Left(ar_Tmp![Name], 60) Else Tmp_cDigest = Trim(ar_Tmp![Name]) End If Else Tmp_cDigest = " " End If '摘要过滤逗号 Tmp_cDigest = FmtStr_QiMei(Tmp_cDigest)
'科目编码 If Trim(ar_Tmp!Segment4) <> "000" Then Tmp_cCode = Trim(ar_Tmp!Segment3) & Trim(ar_Tmp!Segment4) Else Tmp_cCode = Trim(ar_Tmp!Segment3) End If '金额借方 If Not (IsNull(ar_Tmp![L Debit]) Or ar_Tmp![L Debit] = "") Then Tmp_Md = ar_Tmp![L Debit] Else Tmp_Md = 0 End If '金额贷方 If Not (IsNull(ar_Tmp![L Credit]) Or ar_Tmp![L Credit] = "") Then Tmp_Mc = ar_Tmp![L Credit] Else Tmp_Mc = 0 End If '数量 默认为0 Tmp_nDs = 0 '外币与汇率 If Trim(ar_Tmp![Currency Code]) = "RMB" Then Tmp_mD_f = 0 Tmp_nFrat = 0 Else '因外币错方或贷方总有一方对0,所以外币金额可以为借方+贷方,而不必判断是借方或贷方 Tmp_mD_f = ar_Tmp![Entered Debit] + ar_Tmp![Entered Credit] 'Tmp_nFrat = ar_Tmp![Currency Conversion Rate SUM] '汇率由本币/原币得到 If Tmp_mD_f = 0 Then Tmp_nFrat = 0 Else Tmp_nFrat = (ar_Tmp![L Debit] + ar_Tmp![L Credit]) / Tmp_mD_f End If End If '制单人姓名 Tmp_cBill = Trim(txtcBill.Text) '客户编号 If Not (IsNull(ar_Tmp![Customer Number]) Or ar_Tmp![Customer Number] = "" Or ar_Tmp![Customer Number] = "0") Then Tmp_cCus_ID = Trim(ar_Tmp![Customer Number]) Else Tmp_cCus_ID = "" End If '供应商编号 If Not (IsNull(ar_Tmp![Vendor Number]) Or ar_Tmp![Vendor Number] = "" Or ar_Tmp![Vendor Number] = "0") Then Tmp_cSup_ID = Trim(ar_Tmp![Vendor Number]) Else Tmp_cSup_ID = "" End If '部门 If Not (IsNull(ar_Tmp!Segment2) Or ar_Tmp!Segment2 = "" Or ar_Tmp!Segment2 = "0000") Then Tmp_cDept_ID = Trim(ar_Tmp!Segment2) Else Tmp_cDept_ID = "" End If '个人 Tmp_cPerson_ID = "" 'Text1 '应对方公司要求,text1栏写入船票号码 Tmp_cText1 = ar_Tmp![Doc No] & "" If Len(Trim(Tmp_cText1)) > 10 Then Tmp_cText1 = Mid(Trim(Tmp_cText1), 3, 10) End If 'Text2 Tmp_cText2 = ar_Tmp!Src & "" If Len(Trim(Tmp_cText2)) > 10 Then Tmp_cText2 = Left(Trim(Tmp_cText2), 10) End If Tmp_cCode = Trim(Tmp_cCode)
直接导出成文本啊。
根本用不着编程。QQ:5507350
dim strTxt as adodb.recordset'省略初始化recordset代码
rsData.Open "select 客户编号,客户名称,应付金额 from..." 'select要返回的记录
strTxt="客户编号,客户名称,应付金额" & vbcrlf '列头
strTxt=rs.GetString(adClipString, , , vbCrLf) '放到字符串中
'写到文件里
Open "c:\test.txt" For Output As #1
Print #1, strTxt
Close #1
'文本文件头
Print #1, "填制凭证,V800"
For i_Row = 1 To ar_Tmp.RecordCount
If Trim(ar_Tmp![Doc No]) <> "" And ar_Tmp![L Debit] + ar_Tmp![L Credit] <> 0 Then
'制单日期
If Not IsNull(ar_Tmp![Eff Date]) Then
If IsDate(ar_Tmp![Eff Date]) Then
Tmp_dBill_Date = Format(ar_Tmp![Eff Date], "YYYY-MM-DD")
Else
Tmp_dBill_Date = Format(Now(), "YYYY-MM-DD")
End If
Else
Tmp_dBill_Date = Format(Now(), "YYYY-MM-DD")
End If
'凭证类别字
Tmp_cSign = Trim(txtcSign.Text)
'业务(凭证)号(取EXCEL中的后四位)
Tmp_iNo_ID = Right(ar_Tmp![Doc No], 4)
'附单据数 默认为0
Tmp_iDoc = 0
'摘要(最长为30)
'应对方公司要求,摘要栏写批次名称
'If Not IsNull(ar_Tmp![Doc Dec]) Then
If Not IsNull(ar_Tmp![Name]) Then
If LenB(ar_Tmp![Name]) > 60 Then
Tmp_cDigest = Left(ar_Tmp![Name], 60)
Else
Tmp_cDigest = Trim(ar_Tmp![Name])
End If
Else
Tmp_cDigest = " "
End If
'摘要过滤逗号
Tmp_cDigest = FmtStr_QiMei(Tmp_cDigest)
'科目编码
If Trim(ar_Tmp!Segment4) <> "000" Then
Tmp_cCode = Trim(ar_Tmp!Segment3) & Trim(ar_Tmp!Segment4)
Else
Tmp_cCode = Trim(ar_Tmp!Segment3)
End If
'金额借方
If Not (IsNull(ar_Tmp![L Debit]) Or ar_Tmp![L Debit] = "") Then
Tmp_Md = ar_Tmp![L Debit]
Else
Tmp_Md = 0
End If
'金额贷方
If Not (IsNull(ar_Tmp![L Credit]) Or ar_Tmp![L Credit] = "") Then
Tmp_Mc = ar_Tmp![L Credit]
Else
Tmp_Mc = 0
End If
'数量 默认为0
Tmp_nDs = 0
'外币与汇率
If Trim(ar_Tmp![Currency Code]) = "RMB" Then
Tmp_mD_f = 0
Tmp_nFrat = 0
Else
'因外币错方或贷方总有一方对0,所以外币金额可以为借方+贷方,而不必判断是借方或贷方
Tmp_mD_f = ar_Tmp![Entered Debit] + ar_Tmp![Entered Credit]
'Tmp_nFrat = ar_Tmp![Currency Conversion Rate SUM]
'汇率由本币/原币得到
If Tmp_mD_f = 0 Then
Tmp_nFrat = 0
Else
Tmp_nFrat = (ar_Tmp![L Debit] + ar_Tmp![L Credit]) / Tmp_mD_f
End If
End If
'制单人姓名
Tmp_cBill = Trim(txtcBill.Text)
'客户编号
If Not (IsNull(ar_Tmp![Customer Number]) Or ar_Tmp![Customer Number] = "" Or ar_Tmp![Customer Number] = "0") Then
Tmp_cCus_ID = Trim(ar_Tmp![Customer Number])
Else
Tmp_cCus_ID = ""
End If
'供应商编号
If Not (IsNull(ar_Tmp![Vendor Number]) Or ar_Tmp![Vendor Number] = "" Or ar_Tmp![Vendor Number] = "0") Then
Tmp_cSup_ID = Trim(ar_Tmp![Vendor Number])
Else
Tmp_cSup_ID = ""
End If
'部门
If Not (IsNull(ar_Tmp!Segment2) Or ar_Tmp!Segment2 = "" Or ar_Tmp!Segment2 = "0000") Then
Tmp_cDept_ID = Trim(ar_Tmp!Segment2)
Else
Tmp_cDept_ID = ""
End If
'个人
Tmp_cPerson_ID = ""
'Text1
'应对方公司要求,text1栏写入船票号码
Tmp_cText1 = ar_Tmp![Doc No] & ""
If Len(Trim(Tmp_cText1)) > 10 Then
Tmp_cText1 = Mid(Trim(Tmp_cText1), 3, 10)
End If
'Text2
Tmp_cText2 = ar_Tmp!Src & ""
If Len(Trim(Tmp_cText2)) > 10 Then
Tmp_cText2 = Left(Trim(Tmp_cText2), 10)
End If
Tmp_cCode = Trim(Tmp_cCode)
Print #1, Tmp_dBill_Date & "," & Tmp_cSign & "," & Tmp_iNo_ID & "," & Tmp_iDoc _
& "," & Tmp_cDigest & "," & Tmp_cCode & "," & Tmp_Md & "," & Tmp_Mc & "," _
& Tmp_nDs & "," & Tmp_mD_f & "," & Tmp_nFrat & "," & Tmp_cBill & ",,,," _
& Tmp_cDept_ID & "," & Tmp_cPerson_ID & "," & Tmp_cCus_ID & "," _
& Tmp_cSup_ID & ",,," & Tmp_cText1 & "," & Tmp_cText2 & ""
End If
ar_Tmp.MoveNext
Next i_Row
Pbr.Visible = False
'==========导出文本文件内容 end ==========
Close #1