VB.net加载MS excel到DataGridview

时间:2017-08-07 13:01:03

标签: excel vb.net datagridview

我有将Ms excel文件加载到DataGridview中的问题,问题是如果每个单元格中的数据都包含符号" +"," - "," *"," /","!"," @","#"," $","%","&",":",&#34 ;;",&#34 ;'",",","。","?" 。当我将它上传到DataGridview时,我想从我的excel文件中替换那些符号,如何替换它,有人可以帮助我吗?

Imports System.IO

Public Class Form1

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

    OpenFileDialog1.Filter = "Excel Worksheets|*.xls|All File (*.*)|*.*"
    OpenFileDialog1.FilterIndex = 2
    OpenFileDialog1.RestoreDirectory = True

    If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
        TextBox1.Text = OpenFileDialog1.FileName
        Button2.Visible = True
        Button3.Visible = True

        Try
            Dim MyConnection As System.Data.OleDb.OleDbConnection
            Dim DataSet As System.Data.DataSet
            Dim MyCommend As System.Data.OleDb.OleDbDataAdapter
            Dim Path As String = OpenFileDialog1.FileName

            MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Path + ";Extended Properties=Excel 12.0;")
            MyConnection.Open()
            MyCommend = New System.Data.OleDb.OleDbDataAdapter("Select * from [Sheet1$]", MyConnection)

            DataSet = New System.Data.DataSet
            MyCommend.Fill(DataSet)
            DataGridView1.DataSource = DataSet.Tables(0)
            'DataGridView2.DataSource = DataGridView1.DataSource ' Copy from DataGredView 1 to 2
            MyConnection.Close()

            DataGridView1.Show()
            Label4.Text = "It has " & DataGridView1.Rows.Count - 1 & " Element in list."
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End If

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Dim D50 As Integer = 0, D20 As Integer = 0, D10 As Integer = 0, D5 As Integer = 0, Total As Integer = 0

    Dim Cards() As Integer = {50, 20, 10, 5}
    Dim Phone As Integer = 0, Money As Integer = 0
    Dim R As Integer = 0
    Dim X As Integer = 0
    Dim I As Integer
    Dim Z As Integer = 0
    Dim Result As String = ""
    Dim TotalMoney As String = 0

    Dim StrRepPhone As String = ""
    Dim StrRepMoney As String = ""
    Dim StrRep() As String = {"+", "-", "*", "/", "!", "@", "#", "$", "%", "&", ":", ";", "'", ",", ".", "?"}

    For A As Integer = 0 To DataGridView1.Rows.Count - 1

        StrRepPhone = DataGridView1.Rows(A).Cells(0).Value
        StrRepMoney = CInt(DataGridView1.Rows(A).Cells(1).Value)
        For B As Integer = 0 To 15
            Phone = StrRepPhone.Replace(StrRep(B), "")
            Money = StrRepMoney.Replace(StrRep(B), "")
        Next



        'Phone = DataGridView1.Rows(A).Cells(0).Value
        'Money = CInt(DataGridView1.Rows(A).Cells(1).Value)

        '' Find the Total Money while Cells index 1
        TotalMoney += DataGridView1.Rows(A).Cells(1).Value

        '/ Using X = Money Mod 5 to find all number could return 0, like 10 Mod 5= 0
        X = Money Mod 5
        I = 0
        If X = 0 Then
            While Money > 0
                R = CInt(Money \ Cards(I))
                Money = Money Mod Cards(I)
                For Z = 1 To R
                    If R <> 0 Then
                        Result = Result & Phone & " " & Cards(I) & vbCrLf

                        ' //Count the total number of each Card by using Card(I)
                        If Cards(I) = 50 Then
                            D50 += 1
                        ElseIf Cards(I) = 20 Then
                            D20 += 1
                        ElseIf Cards(I) = 10 Then
                            D10 += 1
                        Else
                            D5 += 1
                        End If

                    End If
                Next
                I += 1
            End While
            Label3.Text = "No remaining " & X
        Else
            'Label3.Text = "Remaining " & X ' remaing value after mod
            'Label3.Text = "តម្លៃដែលមិនត្រូវគឺ​" & Money & "​ស្ថិតនៅជួរដេកទី " & DataGridView1.Rows(A).Index & " Remaining " & X
            Label3.Text = "មានតម្លៃដែលបញ្ជូលមិនត្រឹមត្រូវ សូមធ្វើការពិនិត្យឡើងវិញ!"
            Exit Sub
        End If
    Next

    ' Set the Multiline property to true.
    TextBox2.Multiline = True
    'TextBox2.Enabled = False
    TextBox2.ScrollBars = ScrollBars.Vertical ' Add vertical scroll bars to the TextBox control.
    TextBox2.AcceptsReturn = False ' Allow the TAB key to be entered in the TextBox control.
    TextBox2.AcceptsTab = False ' Allow the TAB key to be entered in the TextBox control.
    TextBox2.WordWrap = False ' Set WordWrap to true to allow text to wrap to the next line.
    TextBox2.Text = Result

    Label5.Text = "$50= " & D50
    Label6.Text = "$20= " & D20
    Label7.Text = "$10= " & D10
    Label8.Text = "$05= " & D5
    Label9.Text = "Total Money:$" & TotalMoney

end sub

1 个答案:

答案 0 :(得分:0)

从Excel到DataGridView。

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

        Dim MyConnection As System.Data.OleDb.OleDbConnection
        Dim DtSet As System.Data.DataSet
        Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
        MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='C:\Users\Excel\Desktop\Book1.xls';Extended Properties=Excel 8.0;")
        MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
        MyCommand.TableMappings.Add("Table", "Net-informations.com")
        DtSet = New System.Data.DataSet
        MyCommand.Fill(DtSet)
        DataGridView1.DataSource = DtSet.Tables(0)
        MyConnection.Close()

    End Sub

另外,从DGV导出到Excel。

Imports System.Data.SqlClient
Imports Microsoft.Office.Interop.Excel
Imports Microsoft.Office.Interop

Public Class Form1


    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Dim xlApp As Microsoft.Office.Interop.Excel.Application
        Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
        Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value
        Dim i As Integer
        Dim j As Integer

        'xlApp = New Microsoft.Office.Interop.Excel.Global
        'xlApp = New Microsoft.Office.Interop.Excel.ApplicationClass()
        xlApp = New Excel.Application()
        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets("sheet1")


        For i = 0 To DataGridView1.RowCount - 2
            For j = 0 To DataGridView1.ColumnCount - 1
                For k As Integer = 1 To DataGridView1.Columns.Count
                    xlWorkSheet.Cells(1, k) = DataGridView1.Columns(k - 1).HeaderText
                    xlWorkSheet.Cells(i + 2, j + 1) = DataGridView1(j, i).Value.ToString()
                Next
            Next
        Next

        xlWorkSheet.SaveAs("C:\Users\Excel\Desktop\Book2.xls")
        xlWorkBook.Close()
        xlApp.Quit()

        releaseObject(xlApp)
        releaseObject(xlWorkBook)
        releaseObject(xlWorkSheet)

        MsgBox("C:\Users\Excel\Desktop\Book2.xls")
    End Sub


    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub

End Class