将数据从数据表导出到文本。 VB.NET [英] Exporting data from data table to text. VB.NET

查看:56
本文介绍了将数据从数据表导出到文本。 VB.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个用于群发电子邮件的Windows窗体应用程序。我的联系人数据存储在数据表Customer中。现在我想将我的客户数据导出到文本文件。我有我的代码,但我一直想出一个空文件。我调试了代码并看到
它实际上正在读取数据,但它没有写入文本文件。以下是我的代码。有人可以告诉我我做错了吗?

 Dim ds As DataSet = MassEMailDataSet()
Dim dt As DataTable = ds.Tables (" Customer")
Dim filename As String =" C:\\\\\\\\\\\\\\\
Dim sepChar As String =","
Dim writer As StreamWriter

writer = New StreamWriter(filename)

Dim sep As String =""
Dim builder As New Text.StringBuilder

'标题行
For each col As DataColumn in dt.Columns
builder.Append(sep).Append(col。 ColumnName)
sep = sepChar
下一个
writer.WriteLine(builder.ToString())

'行行
For Each row As DataRow in dt .Rows
sep =""
For each col As DataColumn in dt.Columns
builder.Append(sep).Append(row(col.ColumnName))
sep = sepChar
Next
writer .WriteLine(builder.ToString())
builder.Clear()
Next
MsgBox(" Export done!")

解决方案

嗨朋友,


很抱歉我迟到的回复。


我无法测试你的代码,因为项目中有一些未知变量,如果你想将数据中的数据导出到文本,我建议你可以参考下面这个成功的演示,在这个演示中,我在我的代码中模拟一个数据表。

 Imports System.IO 
Imports System.Text

Public Class Form2
Private Sub Button1_Click(sender As Object,e As EventArgs)Handles Button1.Click
Dim T_Ge nder As New DataTable()
T_Gender.Columns.AddRange(New DataColumn(){New DataColumn(" GenderID"),New DataColumn(" GenderName")})

T_Gender。 Rows.Add(New Object(){" 1","Man"})
T_Gender.Rows.Add(New Object(){" 2"," Female"})
T_Gender.Rows.Add(New Object(){" 3"," Secret"})

Dim filename As String =" D:\\\\\\\\\\\\\

Dim _nRowIndex As Integer = 1
使用_objWriter作为新StreamWriter(filename,False,Encoding.UTF8)
For Each row As DataRow In T_Gender.Rows
_nRowIndex + = 1
For Each col As DataColumn In T_Gender.Columns
_objWriter.WriteLine(row(col.ColumnName).ToString())
Next
Next
End使用
结束子
结束类


¥b $ b

希望这有帮助!


最好的问候,


Stanly


I am creating a windows forms application for mass e-mailing. My contact data is stored in a data table Customer. Now I want to export my Customer data to a text file. I have my code but I keep coming up with an empty file. I debugged the code and saw that it is actually reading the data but it's not written to the text file. Below is my code. Can someone please tell me what I am doing wrong?

            Dim ds As DataSet = MassEMailDataSet()
            Dim dt As DataTable = ds.Tables("Customer")
            Dim filename As String = "C:\EMail\ExportTest.txt"
            Dim sepChar As String = ","
            Dim writer As StreamWriter

            writer = New StreamWriter(filename)

            Dim sep As String = ""
            Dim builder As New Text.StringBuilder

            'header line
            For Each col As DataColumn In dt.Columns
                builder.Append(sep).Append(col.ColumnName)
                sep = sepChar
            Next
            writer.WriteLine(builder.ToString())

            'row lines
            For Each row As DataRow In dt.Rows
                sep = ""
                For Each col As DataColumn In dt.Columns
                    builder.Append(sep).Append(row(col.ColumnName))
                    sep = sepChar
                Next
                writer.WriteLine(builder.ToString())
                builder.Clear()
            Next
            MsgBox("Export done!")

解决方案

Hi friend,

Sorry for my late reply.

I can not test your code because there are some unknown variable in your project, and if you want to export the data within datatable to text, I suggest you can refer to the following successful demo, in this demo, I simulate a datatable in my code.

Imports System.IO
Imports System.Text

Public Class Form2
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim T_Gender As New DataTable()
        T_Gender.Columns.AddRange(New DataColumn() {New DataColumn("GenderID"), New DataColumn("GenderName")})

        T_Gender.Rows.Add(New Object() {"1", "Man"})
        T_Gender.Rows.Add(New Object() {"2", "Female"})
        T_Gender.Rows.Add(New Object() {"3", "Secret"})

        Dim filename As String = "D:\Log\ExportTest.txt"

        Dim _nRowIndex As Integer = 1
        Using _objWriter As New StreamWriter(filename, False, Encoding.UTF8)
            For Each row As DataRow In T_Gender.Rows
                _nRowIndex += 1
                For Each col As DataColumn In T_Gender.Columns
                    _objWriter.WriteLine(row(col.ColumnName).ToString())
                Next
            Next
        End Using
    End Sub
End Class


Hope this helps!

Best Regards,

Stanly


这篇关于将数据从数据表导出到文本。 VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆