将数据表导出到文本文件

将数据表导出到文本文件

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

问题描述

你好我有一个问题 

hello i have a question 

我有一个数据集,我想把它导出到一个txt文件 

i have a dataset and i want to export it to a txt file 

所以我写了这段代码 

so i wrote this piece of code 

 Private Sub DataTable_To_Text_for_Softone(ByVal table As DataTable, ByVal path As String)
        If table.Columns.Count < 0 OrElse table.Rows.Count < 0 Then
            Exit Sub
        End If
        Dim ALLL As String
        Using sw As IO.StreamWriter = New IO.StreamWriter(path)
            For row As Integer = 0 To table.Rows.Count - 1
                For col As Integer = 0 To table.Columns.Count
                    If col = 0 Then
                        ALLL = table.Rows(row).Item(col).ToString.PadRight(5, " ")
                     ElseIf col = 1 Then
                        ALLL = ALLL + table.Rows(row).Item(col).ToString.PadRight(5, " ")
                     ElseIf col = 2 Then
                        ALLL = ALLL + table.Rows(row).Item(col).ToString.PadRight(20, " ")
                    ElseIf col = 3 Then
                        ALLL = ALLL + table.Rows(row).Item(col).ToString.PadRight(20, " ")
                    ElseIf col = 4 Then
                        ALLL = ALLL + table.Rows(row).Item(col).ToString.PadRight(15, " ")
                    ElseIf col = 5 Then
                        ALLL = ALLL + table.Rows(row).Item(col).ToString.PadRight(15, " ")
                    ElseIf col = 6 Then
                        ALLL = ALLL + table.Rows(row).Item(col).ToString.PadRight(15, " ")
                    ElseIf col = 7 Then
                        ALLL = ALLL + table.Rows(row).Item(col).ToString.PadRight(15, " ")
                    ElseIf col = 8 Then
                        ALLL = ALLL + table.Rows(row).Item(col).ToString.PadRight(15, " ")
                   ElseIf col = 9 Then
                        ALLL = ALLL + table.Rows(row).Item(col).ToString.PadRight(5, " ")
                    sw.Write(ALLL)
                    sw.Write(Environment.NewLine)
                    End If
                Next

            Next
        End Using

    End Sub

代码似乎工作正常,因为它成功循环遍历数据表 

但是在每行之后添加新行a得到此结果 

the code seems to work fine since it loops succesfully through the datatable 
but instead after every row to add a new line a get this result 

1  &NBSP; 1&NBSP; &NBSP; 6/11/2018年&NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  增值税  &NBSP;   50.00.73.0009
$
009

  &NBSP; &NBSP; &NBSP; &NBSP; 18&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; 0.00&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; 27&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  增值税



1  &NBSP; 2&NBSP; &NBSP; 6/11/2018年&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;现金和NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   106

  &NBSP; &NBSP; &NBSP; &NBSP; 18&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; -27&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; 0.00&NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  现金



2  &NBSP; 1&NBSP; &NBSP; 6/11/2018年&NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  房间收入  &NBSP; &NBSP; &NBSP; 73.00.00.0063
$
017

  &NBSP; &NBSP; &NBSP; &NBSP; 290&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; 0.00&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; 824.31&NBSP; &NBSP; &NBSP; &NBSP;  房间收入 



2  &NBSP; 2&NBSP; &NBSP; 6/11/2018年&NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  房间收入  &NBSP; &NBSP; &NBSP; 54.00.73.0013
$
017

  &NBSP; &NBSP; &NBSP; &NBSP; 290&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; 0.00&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; 107.19&NBSP; &NBSP; &NBSP; &NBSP;  安排13%

2  &NBSP; 3&NBSP; &NBSP; 6/11/2018年&NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  房间收入  &NBSP; &NBSP; &NBSP; 54.00.73.0013
$
017

  &NBSP; &NBSP; &NBSP; &NBSP; 290&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; 0.00&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; 107.19&NBSP; &NBSP; &NBSP; &NBSP;  房间收入 

1    1    6/11/2018           Accmodation Tax     50.00.73.0009
009
          18             0.00           27             Accmodation Tax

1    2    6/11/2018           Cash                
             106
          18             -27            0.00           Cash

2    1    6/11/2018           Room Revenue        73.00.00.0063
017
          290            0.00           824.31         Room Revenue 

2    2    6/11/2018           Room Revenue        54.00.73.0013
017
          290            0.00           107.19         arrangement 13%
2    3    6/11/2018           Room Revenue        54.00.73.0013
017
          290            0.00           107.19         Room Revenue 

而不是 

知道为什么它会在这些特殊列之后打破行?

any idea why it breaks lines after this particullar columns ?

推荐答案

让我们将其推送到带填充的文件,您可以根据需要添加到此文件中。 mDataTable从数据库获取数据,然后我使用Lambda为所有行/列创建字符串。 

Let's push this to a file with padding, you can add to this as you see fit. mDataTable gets data from a database and then I use Lambda to create a string for all rows/columns. 

Public Sub ExportData()
    Dim ops As New DataOperations
    Dim mDataTable As DataTable = ops.Read
    Dim sb As New StringBuilder

    mDataTable.AsEnumerable().
            Select(Function(row)




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

07-25 20:52