'DataTalbe输出到TXT/CSV文件
Dim fs As FileStream = New FileStream(path, FileMode.Create, FileAccess.Write)
Dim sw As StreamWriter = New StreamWriter(fs, System.Text.Encoding.Default)
'列名
For i = 0 To dataDt.Columns.Count - 1
data = data + dataDt.Columns(i).ColumnName.ToString
If i < dataDt.Columns.Count - 1 Then
data = data + "," 'CSV文件逗号分隔
End If
Next
sw.WriteLine(data) '打印列名
'行数据
For i = 0 To dt.Rows.Count - 1
data = ""
For j = 0 To dt.Columns.Count - 1
data = data + dt.Rows(i)(j).ToString.Trim
If j < dt.Columns.Count - 1 Then
data = data + ","
End If
Next
sw.WriteLine(data)
Next
'Format函数的使用
Dim no As Integer = 1
Dim res As String = Format(no, "000") '打印为 "001",还可以使用小数点形式
'取字符串长度
System.Text.Encoding.Default.GetByteCount("字符串")