知道的朋友帮帮忙,怎样在数据库中或者是在表格控件中导出下面这种TXT记事本格式
数据怎样对齐啊?0506301白粥                    10.00份  份  0      BZ        
0507101白粥(位)                 2.00位  位  0      BZW       
0500101白灼广东菜心            12.00份  份  0      BZGDCX   

解决方案 »

  1.   


    '格式化记录集输出到文本文件
    '假定记录集所有字段都为字符形,而且不存在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
    楼主,看到我这么用心敲代码的份上,把分都给我吧~~~~~~~~~~~~~~
      

  2.   


    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
      

  3.   

    我是菜鸟,请帮我看下
    怎样在表格控件中将数据导出这这种TXT文本格式?
    数据对齐?下面的地址是QQ文件中转站的地址:请你们下载下来来帮我看下,感谢各位高手了
    http://51.dc.ftn.qq.com/ftn_handler/84abfb096d473a98e93f9f98d00c14a65a370cc2d496d4a3f820a1934f99cd19338cd99b9017323d14abe51f04622d2b7435c66f42e534b5f513f9d71950a583/数据导出.rar?k=76306335bf63cb9de540c3184732054d54040754045605521a530505501f030100534e0d5850564f530852565602550702025307613e37a8ca8ebe80dd81c14c455111355c&&txf_fid=26181dcd5e9ee015e79b60327d63d2deb55da256
      

  4.   


    Private Sub Command1_Click()
    'Open "菜谱表.txt" For Output As #1
    '
    'For i = 1 To VSFlexGrid1.Rows - 1
    'Print #1, Trim(VSFlexGrid1.TextMatrix(i, 0)) & Trim(VSFlexGrid1.TextMatrix(i, 1)) & Trim(VSFlexGrid1.TextMatrix(i, 2))
    'Next i
    'Close #1
    '
    '
    '请问怎样将格式对齐啊? 请对照附件“菜品表”的格式
    Dim X As Long
    Dim Y As Long
    Dim MaxCount() As Long
    Dim SBSize As Long
    Dim TempStr As String
    Dim fs As Integer'取得每列中最长的字节数
    ReDim MaxCount(VSFlexGrid1.Cols - 1)
    For X = 1 To VSFlexGrid1.Rows - 1
       For Y = 0 To VSFlexGrid1.Cols - 1
          SBSize = LenB(StrConv(VSFlexGrid1.TextMatrix(X, Y), vbFromUnicode))  '取得字符串字节数
          If MaxCount(Y) < SBSize Then
             MaxCount(Y) = SBSize
          End If
       Next Y
    Next X'再根据每行最长的字节数处理每格字符串应该填补多少个空格符
    fs = FreeFile
    Open "菜谱表.txt" For Output As #fs
    For X = 1 To VSFlexGrid1.Rows - 1
       TempStr = ""
       For Y = 0 To VSFlexGrid1.Cols - 1
          SBSize = LenB(StrConv(VSFlexGrid1.TextMatrix(X, Y), vbFromUnicode))  '取得当前字符串字节数
          TempStr = TempStr & VSFlexGrid1.TextMatrix(X, Y) & String(MaxCount(Y) - SBSize, " ") & "   "
       Next Y
       Print #fs, TempStr
    Next X
    Close #fs
    End Sub
      

  5.   

    非常,非常的感谢两位,特别是SupermanKing