从DataGridView导入和导出csv文件

时间:2018-04-23 15:47:48

标签: vb.net csv import export

那里很高,我目前正在为一个大学项目制作一小块软件。该项目的目标是让用户输入成分和信息,然后他们就可以用这些成分制作膳食。

我遇到了一个问题,虽然我在加载我的csv文件之后,我已经使用该软件添加一个新的成分我得到了错误:" 系统。 IndexOutOfRangeException:'索引超出了数组的范围。' "它突出显示了代码部分" newRow(" Protien")= columns(1)"现在我想是因为当它从最近保存的csv文件中加载数据时,它顶部有一行填充了标题,是否有任何方法可以保存csv文件而不包括数据表中的标题?

csv文件中的代码:

 'Sets up the data table that will be used to store the ingredients and their information.'
    With ingredientTable
        .Columns.Add("Name", System.Type.GetType("System.String"))
        .Columns.Add("Protien", System.Type.GetType("System.Decimal"))
        .Columns.Add("Fat", System.Type.GetType("System.Decimal"))
        .Columns.Add("Salt", System.Type.GetType("System.Decimal"))
        .Columns.Add("Carbs", System.Type.GetType("System.Decimal"))
        .Columns.Add("Calories", System.Type.GetType("System.Decimal"))
    End With

    'Loads in the information from the CSV file to display all the previouly saved ingredients'
    Dim fileReader As New IO.StreamReader("C:\Users\Grant\Desktop\FitnessAppNew\Save Files\Saved_Ingredients.csv", System.Text.Encoding.Default)
    Dim ingredientString As String = ""

    Do

        ingredientString = fileReader.ReadLine
        If ingredientString Is Nothing Then Exit Do
        'Reads what is on the CSV file and sets up the columns and the rows.' 
        Dim columns() As String = ingredientString.Split(",")
        Dim newRow As DataRow = ingredientTable.NewRow
        newRow("Name") = columns(0)
        newRow("Protien") = columns(1)
        newRow("Fat") = columns(2)
        newRow("Salt") = columns(3)
        newRow("Carbs") = columns(4)
        newRow("Calories") = columns(5)
        ingredientTable.Rows.Add(newRow)
    Loop
    fileReader.Close()

    DataGridView1.DataSource = ingredientTable
    Me.Text = ingredientTable.Rows.Count & "rows"

保存CSV文件的代码:

Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click

#Region "Save ingredients"
    Dim csvFile As String = String.Empty

    csvFile = csvFile.TrimEnd(",")
    csvFile = csvFile & vbCr & vbCrLf

    'Used to ge the rows
    For Each row As DataGridViewRow In DataGridView1.Rows
        'Used to get each cell in the row
        For Each cell As DataGridViewCell In row.Cells
            csvFile = csvFile & cell.FormattedValue & ","
        Next
        csvFile = csvFile.TrimEnd(",")
        csvFile = csvFile & vbCr & vbCrLf
    Next
    My.Computer.FileSystem.WriteAllText("C:\Users\Grant\Desktop\FitnessAppNew\Save Files\Saved_Ingredients.csv", csvFile, False)


#End Region

2 个答案:

答案 0 :(得分:0)

您必须尝试此代码。

OpenButton

Dim OpenFileDialog1 As New OpenFileDialog()
    Dim constr As String
    Dim con As OleDb.OleDbConnection

    Try

        OpenFileDialog1.Filter = "Excel Files | *.xlsx; *.xls; *.xlsm;"

        If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            Me.txtOpen.Text = OpenFileDialog1.FileName

            constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + txtOpen.Text + ";Excel 12.0 Xml;HDR=YES"
            con = New OleDb.OleDbConnection(constr)
            con.Open()

            cboSheet.DataSource = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
            cboSheet.DisplayMember = "TABLE_NAME"
            cboSheet.ValueMember = "TABLE_NAME"
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

加载按钮

Dim constr As String
    Dim dt As DataTable
    Dim con As OleDbConnection
    Dim sda As OleDbDataAdapter
    Dim row As DataRow

    Try
        constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + txtOpen.Text + ";Excel 12.0 Xml;HDR=YES"
        con = New OleDbConnection(constr)
        sda = New OleDbDataAdapter("Select * from [" + cboSheet.SelectedValue + "]", con)
        dt = New DataTable
        sda.Fill(dt)

        For Each row In dt.Rows
            DataGridView3.DataSource = dt

        Next
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

我希望它会奏效! :)

答案 1 :(得分:0)

以下是一些要考虑的选项,用于将数据从CSV加载到DGV。

Imports System.Data.SqlClient
Imports System.IO
Imports Microsoft.VisualBasic.FileIO
Imports System.Data
Imports System.Data.Odbc
Imports System.Data.OleDb


Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click


        Dim headers = (From header As DataGridViewColumn In DataGridView1.Columns.Cast(Of DataGridViewColumn)() Select header.HeaderText).ToArray
        Dim rows = From row As DataGridViewRow In DataGridView1.Rows.Cast(Of DataGridViewRow)() Where Not row.IsNewRow Select Array.ConvertAll(row.Cells.Cast(Of DataGridViewCell).ToArray, Function(c) If(c.Value IsNot Nothing, c.Value.ToString, ""))
        Dim str As String = ""
        Using sw As New IO.StreamWriter("C:\Users\Excel\Desktop\OrdersTest.csv")
            sw.WriteLine(String.Join(",", headers))
            'sw.WriteLine(String.Join(","))
            For Each r In rows
                sw.WriteLine(String.Join(",", r))
            Next
            sw.Close()
        End Using

    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        'Dim m_strConnection As String = "server=Excel-PC\SQLEXPRESS;Initial Catalog=Northwind;Trusted_Connection=True;"

        'Catch ex As Exception
        '    MessageBox.Show(ex.ToString())
        'End Try

        'Dim objDataset1 As DataSet()
        'Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Dim da As OdbcDataAdapter
        Dim OpenFile As New System.Windows.Forms.OpenFileDialog ' Does something w/ the OpenFileDialog
        Dim strFullPath As String, strFileName As String
        Dim tbFile As New TextBox
        ' Sets some OpenFileDialog box options
        OpenFile.Filter = "CSV Files (*.csv)|*.csv|All files (*.*)|*.*" ' Shows only .csv files
        OpenFile.Title = "Browse to file:" ' Title at the top of the dialog box

        If OpenFile.ShowDialog() = DialogResult.OK Then ' Makes the open file dialog box show up
            strFullPath = OpenFile.FileName ' Assigns variable
            strFileName = Path.GetFileName(strFullPath)

            If OpenFile.FileNames.Length > 0 Then ' Checks to see if they've picked a file

                tbFile.Text = strFullPath ' Puts the filename in the textbox

                ' The connection string for reading into data connection form
                Dim connStr As String
                connStr = "Driver={Microsoft Text Driver (*.txt; *.csv)}; Dbq=" + Path.GetDirectoryName(strFullPath) + "; Extensions=csv,txt "

                ' Sets up the data set and gets stuff from .csv file
                Dim Conn As New OdbcConnection(connStr)
                Dim ds As DataSet
                Dim DataAdapter As New OdbcDataAdapter("SELECT * FROM [" + strFileName + "]", Conn)
                ds = New DataSet

                Try
                    DataAdapter.Fill(ds, strFileName) ' Fills data grid..
                    DataGridView1.DataSource = ds.Tables(strFileName) ' ..according to data source

                    ' Catch and display database errors
                Catch ex As OdbcException
                    Dim odbcError As OdbcError
                    For Each odbcError In ex.Errors
                        MessageBox.Show(ex.Message)
                    Next
                End Try

                ' Cleanup
                OpenFile.Dispose()
                Conn.Dispose()
                DataAdapter.Dispose()
                ds.Dispose()

            End If
        End If

    End Sub

    Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
        Dim tblReadCSV As New DataTable()

        tblReadCSV.Columns.Add("FName")
        tblReadCSV.Columns.Add("LName")
        tblReadCSV.Columns.Add("Department")

        Dim csvParser As New TextFieldParser("C:\Users\Excel\Desktop\Employee.txt")

        csvParser.Delimiters = New String() {","}
        csvParser.TrimWhiteSpace = True
        csvParser.ReadLine()

        While Not (csvParser.EndOfData = True)
            tblReadCSV.Rows.Add(csvParser.ReadFields())
        End While

        Dim con As New SqlConnection("Server=Excel-PC\SQLEXPRESS;Database=Northwind;Trusted_Connection=True;")
        Dim strSql As String = "Insert into Employee(FName,LName,Department) values(@Fname,@Lname,@Department)"
        'Dim con As New SqlConnection(strCon)
        Dim cmd As New SqlCommand()
        cmd.CommandType = CommandType.Text
        cmd.CommandText = strSql
        cmd.Connection = con
        cmd.Parameters.Add("@Fname", SqlDbType.VarChar, 50, "FName")
        cmd.Parameters.Add("@Lname", SqlDbType.VarChar, 50, "LName")
        cmd.Parameters.Add("@Department", SqlDbType.VarChar, 50, "Department")

        Dim dAdapter As New SqlDataAdapter()
        dAdapter.InsertCommand = cmd
        Dim result As Integer = dAdapter.Update(tblReadCSV)

    End Sub

End Class