将csv文件导入datagridview vb.net时,仅显示列文本

时间:2017-11-07 07:57:36

标签: vb.net

这是我的第一个vb.net程序,如果我的问题非常平庸,请耐心等待。

在提到我的问题之前,我的会计程序的机制是,如果用户将数据输入到datagridview并导出它,那么当他重新启动程序时,他可以导入导出的数据。

我已将此.csv文件导入我的datagridview

enter image description here

问题在于,当我导入它时,只有列文本会显示为这样。

enter image description here

这是我用来创建.csv文件的导出代码。

 Private Sub ExportToExcelToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExportToExcelToolStripMenuItem.Click
    Dim msg1 = "Export Successful"
    Dim title = "Excel Export"

    MsgBox(msg1, , title)
    Try
        Dim iexport1 As String = ""
        Dim eexport1 As String = ""
        For Each C As DataGridViewColumn In Income.Columns
            iexport1 &= """" & C.HeaderText & ""","
        Next
        iexport1 = iexport1.Substring(0, iexport1.Length - 1)
        iexport1 &= Environment.NewLine
        For Each R As DataGridViewRow In Income.Rows
            For Each C As DataGridViewCell In R.Cells
                If Not C.Value Is Nothing Then
                    iexport1 &= """" & C.Value.ToString & ""","
                Else
                    iexport1 &= """" & "" & ""","
                End If
            Next
            iexport1 = iexport1.Substring(0, iexport1.Length - 1)
            iexport1 &= Environment.NewLine
        Next

        For Each C As DataGridViewColumn In Expense.Columns
            eexport1 &= """" & C.HeaderText & ""","
        Next
        eexport1 = eexport1.Substring(0, eexport1.Length - 1)
        eexport1 &= Environment.NewLine
        For Each R As DataGridViewRow In Expense.Rows
            For Each C As DataGridViewCell In R.Cells
                If Not C.Value Is Nothing Then
                    eexport1 &= """" & C.Value.ToString & ""","
                Else
                    eexport1 &= """" & "" & ""","
                End If
            Next
            eexport1 = eexport1.Substring(0, eexport1.Length - 1)
            eexport1 &= Environment.NewLine
        Next

        Dim tw As IO.TextWriter = New IO.StreamWriter(path:="C:\Users\S2009516\Desktop\JanuaryIncome.CSV")
        tw.Write(iexport1)
        tw.Close()
        Dim tw2 As IO.TextWriter = New IO.StreamWriter(path:="C:\Users\S2009516\Desktop\JanuaryExpense.CSV")
        tw2.Write(eexport1)
        tw2.Close()
    Catch ex As Exception
    End Try
End Sub

这是我用于导入csv文件的那个。

Private Sub ImportFromExcelToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ImportFromExcelToolStripMenuItem.Click
    Dim expenseload1 As String = "C:\Users\S2009516\Desktop\JanuaryExpense.CSV"
    Dim incomeload1 As String = "C:\Users\S2009516\Desktop\JanuaryIncome.CSV"

    Try
        Dim expensereader1 As New StreamReader(expenseload1, Encoding.Default)
        Dim incomereader1 As New StreamReader(incomeload1, Encoding.Default)
        Dim eline As String = ""
        Dim iline As String = ""
        Do
            eline = expensereader1.ReadLine
            iline = incomereader1.ReadLine
            If eline Is Nothing Then Exit Do
            If iline Is Nothing Then Exit Do
            Dim words() As String = eline.Split(",")
            Dim words2() As String = iline.Split(",")
            Income.Rows.Add("")
            Expense.Rows.Add("")
            For ix As Integer = 0 To 3
                Me.Income.Rows(Income.Rows.Count - 1).Cells(ix).Value = words2(ix)
                Me.Expense.Rows(Income.Rows.Count - 1).Cells(ix).Value = words(ix)
            Next
        Loop
        expensereader1.Close()
        incomereader1.Close()
    Catch ex As Exception
    End Try
End Sub

我不知道为什么会发生这种情况......我已经按照教程视频中的所有步骤进行了操作..请保存我的灵魂......我已经坚持了2天了。

0 个答案:

没有答案
相关问题