首页 > 解决方案 > C# excel 到 datagridview 从 VB 转换

问题描述

我有 VB 代码可以从 VB 中的 excel 表中获取信息,这需要在 C# 中。我尝试了很多次,但都失败了。我能够将 excel 上传到 C# 中的 datagridview,但是获取我想要的列和信息失败了。

当我开始为 Datagridview 设置列并通过 excel 行获取信息时。

    Dim connString As String = ""
   connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + 
   TextBox2.Text 
    + "';Extended Properties=""Excel 12.0;HDR=Yes;IMEX=2"""
    Dim query As String = "SELECT * FROM [Servers$]"
    Dim conn As New OleDbConnection(connString)

    conn.Open()

    Dim cmd As New OleDbCommand(query, conn)
    Dim da As New OleDbDataAdapter(cmd)
    Dim ds As New DataSet()

    ds.Clear()
    DataGridView1.DataSource = ds
    DataGridView1.DataSource = Nothing



    da.Fill(ds)

    DataGridView1.AutoGenerateColumns = False
    DataGridView1.ColumnCount = 5
    DataGridView1.Columns(0).DataPropertyName = "INTERNAL USE ONLY"
    DataGridView1.Columns(1).DataPropertyName = "F3"
    DataGridView1.Columns(2).DataPropertyName = "F5"
    DataGridView1.Columns(3).DataPropertyName = "F12"
    DataGridView1.DataSource = ds.Tables(0)


    Dim rowindex As String
    Dim mtCell As Integer = 0
    Dim row As DataGridViewRow = New DataGridViewRow()

    For rowNo As Integer = DataGridView1.Rows.Count - 2 To 0 Step -1
        row = DataGridView1.Rows(rowNo)
        Try
            For j = 0 To row.Cells.Count - 2
                If row.Cells(j).Value Is Nothing OrElse row.Cells(j).Value Is DBNull.Value Then
                    mtCell += 1
                End If
            Next

            If mtCell = row.Cells.Count - 1 Then
                DataGridView1.Rows.RemoveAt(rowNo)

            End If
            mtCell = 0
        Catch ex As Exception
            Exit For
        End Try
    Next rowNo
    If RadioButton7.Checked Then
        For rowNo As Integer = DataGridView1.Rows.Count - 2 To 0 Step -1
            row = DataGridView1.Rows(rowNo)
            Try
                For j = 0 To row.Cells.Count - 2
                    If row.Cells.Item(2).Value = "Production" Then
                        mtCell += 1
                    End If
                Next
                For j = 0 To row.Cells.Count - 2
                    If row.Cells.Item(2).Value = "UAT" Then
                        mtCell += 1
                    End If
                Next
                For j = 0 To row.Cells.Count - 2
                    If row.Cells.Item(2).Value = "Pre-Production" Then
                        mtCell += 1
                    End If
                Next
                For j = 0 To row.Cells.Count - 2
                    If row.Cells.Item(2).Value = "Development" Then
                        mtCell += 1
                    End If
                Next
                For j = 0 To row.Cells.Count - 2
                    If row.Cells.Item(0).Value = "Server Name" Then
                        mtCell += 1
                    End If
                Next
                For j = 0 To row.Cells.Count - 2
                    If row.Cells.Item(2).Value = "Disaster Recovery" Then
                        mtCell += 1
                    End If
                Next
                If mtCell = row.Cells.Count - 1 Then
                    rowindex = row.Index.ToString()
                    DataGridView1.Rows.RemoveAt(rowindex)
                    DataGridView1.Rows.RemoveAt(0)
                End If
                mtCell = 0
            Catch ex As Exception
                Exit For
            End Try
        Next rowNo
    End If`

标签: c#excelvb.net

解决方案


推荐阅读