导出TXT记事本格式(数据对齐)
知道的朋友帮帮忙,怎样在数据库中或者是在表格控件中导出下面这种TXT记事本格式
数据怎样对齐啊?
0506301白粥 10.00份 份 0 BZ
0507101白粥(位) 2.00位 位 0 BZW
0500101白灼广东菜心 12.00份 份 0 BZGDCX
[解决办法]
- VB code
'格式化记录集输出到文本文件'假定记录集所有字段都为字符形,而且不存在DBNULL值,不进行错误捕捉public function ExportRecordset(rs as adodb.recordset,strFilePath as string)as long dim i as long dim j as long dim strTemp as string *20 '字段长度不超过19个字符 dim astrLine() as string dim astrData() as string '分配好数据 if(rs.RecordCount>0 )then redim astrLine(rs.recordcount) redim astrData(rs.fields.count-1) '生成字段头 for i=0 to rs.Fields.count-1 strtemp=rs.fields.item(i).name astrdata(i)=strtemp next astrline(0)=join$(astrdata(),vbnullstring) i=1 do while (not rs.eof) for i=0 to rs.Fields.count-1 strtemp=trim$(rs.fields.item(i).value) astrdata(i)=strtemp next astrline(i)=join$(astrdata(),vbnullstring) i=i+1 rs.movenext loop '写入文件 ExportRecordset=writefile (strfilepath, join$(astrline(),vbnullstring) ) end ifend function'将数据写入目标文件private function WriteFile(byval strFilePath as string,strData as string)as boolean dim fn as long dim buff() as byte fn=freefile() buff=strconv(strdata,vbFromUnicode) open strpath for binary as #fn put #fn,,buff close #fn writefile=true end function
[解决办法]
- VB code
Private Sub Command1_Click() Dim TempStr As String Dim TempArray1() As String Dim TempArray2() As String Dim MaxCount() As Long Dim SBSize As Long Dim X As Long Dim Y As Long TempStr = "0506301白粥 10.00份 份 0 BZ" & vbCrLf & _ "0507101白粥(位) 2.00位 位 0 BZW" & vbCrLf & _ "0500101白灼广东菜心 12.00份 份 0 BZGDCX" TempArray1 = Split(TempStr, vbCrLf) For X = 0 To UBound(TempArray1) TempArray2 = Split(TempArray1(X), " ") If X = 0 Then ReDim MaxCount(UBound(TempArray2)) End If For Y = 0 To UBound(TempArray2) SBSize = LenB(StrConv(TempArray2(Y), vbFromUnicode)) If MaxCount(Y) < SBSize Then MaxCount(Y) = SBSize End If Next Y Next X TempStr = "" For X = 0 To UBound(TempArray1) TempArray2 = Split(TempArray1(X), " ") For Y = 0 To UBound(TempArray2) SBSize = LenB(StrConv(TempArray2(Y), vbFromUnicode)) If Y = UBound(TempArray2) Then TempStr = TempStr & TempArray2(Y) Else TempStr = TempStr & TempArray2(Y) & String(MaxCount(Y) - SBSize, " ") & " " End If Next Y If X < UBound(TempArray1) Then TempStr = TempStr & vbCrLf End If Next X MsgBox "你要的数据结果:" & vbCrLf & TempStr, 64, "结果"End Sub