[求助]ado连接sql把一个表内的信息导出为txt文本,每个字段有固定的长度左对齐,长度不够的用空格补齐,我不知道null的怎么对齐。
我要把sql2000数据库中的一个商品信息表的内容导出到txt文本。要有固定的格式。就是每个字段有固定的长度。左对齐。长度不够的用空格补齐。但是有的字段是null,我就怎么也对不齐了。希望哪位高手给帮忙看看。最好给我一段代码。我是菜鸟不太懂。下面是我的代码和我的执行结果和正确结果的对比。麻烦大家了。急。Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim num As Integer
Dim out As String
Dim i As Integer
Private Sub Command1_Click()
rs.MoveFirst
For i = 1 To num
out = out & rs.Fields(0) & rs.Fields(1) & rs.Fields(2) & rs.Fields(3) & rs.Fields(4) & rs.Fields(5) & Space(8 - Len(rs.Fields(5))) & rs.Fields(6) & Space(rs.Fields(6)) & vbCrLf
rs.MoveNext
Next i
Open "d:\text.txt" For Output As #1
Print #1, out
Close #1
MsgBox "文件被成功导出" & Text1.Text & "条", vbOKOnly
End Sub
Private Sub Form_Load()
num = 0
out = ""
Set cn = New ADODB.Connection
cn.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;User ID=sa;Data Source=hb"
cn.Open
Set rs = cn.Execute("select item_no ,cast(item_subno as char(15)),cast(item_name as char(40)),cast(item_size as char(20)) ,cast(unit_no as char(4)),price, sale_price from t_bd_item_info")
rs.MoveFirst
Do While Not rs.EOF()
num = num + 1
rs.MoveNext
Loop
Text1.Text = num
End Sub
下面是我的执行结果:(有一个字段是null就不能对齐了)
04902001 14302001 粽子 个 1.2 1.5
05101001 25030001 精装纯牛奶 250g 瓶 1.8 2.2
下面是正确的结果:
04902001 14302001 粽子 个 1.2 1.5
05101001 25030001 精装纯牛奶 250g 瓶 1.8 2.2