【问题标题】:Reading row of data from excel从excel中读取一行数据
【发布时间】:2014-03-29 09:48:15
【问题描述】:

我需要从我的 excel 中读取行并将其插入 SQl 服务器。插入时我会检查那些行是否存在,根据那个标志 ll b 返回到前端。

到目前为止,我已经进行了逐列阅读。请任何人帮助我从 excel 中读取数据行,并在 VB.Net 中插入 SQL 服务器

需要使用数据表和数据集。 在 VB.NET 中

公开课表1

'#Region "Decleration"
'Dim exApp As Microsoft.Office.Interop.Excel.Application
'Dim exSheet As Microsoft.Office.Interop.Excel.Worksheet
'Dim exCell As Microsoft.Office.Interop.Excel.Range
'Dim dataset1 As New DataSet
'Dim rCount As Integer = 0
'Dim cCount As Integer = 0
'Dim objValues As Object
'Dim totalRecords As Integer = 0

'#End Region

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim objExcel As Excel.Application
    Dim objWorkBook As Excel.Workbook
    Dim objWorkSheets As Excel.Worksheet
    Dim ExcelSheetName As String = ""

    '   objExcel = CreateObject("Excel.Application")
    '   objWorkBook = objExcel.Workbooks.Open("C:\Users\Jidh\Desktop\tttttttttttttttttt.xlsx")


End Sub

Private Sub OpenFileDialog_FileOk(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles FileUpload1.FileOk

End Sub

Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs)

End Sub

Private Sub btnChoseDoc_Click(sender As System.Object, e As System.EventArgs) Handles btnChoseDoc.Click
    If FileUpload1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
        Try

            Dim DS As DataSet
            Dim MyCommand As OleDb.OleDbDataAdapter
            Dim MyConnection As OleDb.OleDbConnection

            Dim dtExcelSchema As DataTable

            MyConnection = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Jidh\Desktop\tttttttttttttttttt.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES"";")

            MyConnection.Open()

            dtExcelSchema = MyConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
            Dim SheetName As String = dtExcelSchema.Rows(0)("TABLE_NAME").ToString()
            MyConnection.Close()

            MyConnection.Open()

            ' Select the data from Sheet1 of the workbook.
            MyCommand = New OleDb.OleDbDataAdapter("Select * from [" & SheetName & "]", MyConnection)
            DS = New System.Data.DataSet()
            MyCommand.Fill(DS)
            grvRecords.DataSource = DS.Tables(0).DefaultView
            MyConnection.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        Finally


        End Try



    End If

End Sub

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

结束类

【问题讨论】:

  • 你现在有什么代码?提出问题并希望有人为您工作是没有意义的 - 表明您至少尝试过。
  • 我试过了,你看看
  • 这就是我的意思,显示您的代码,以便我们帮助您完成它。将快速浏览并通知您。
  • 好的,我已经编译了你的代码,它似乎运行良好。它添加了 Sheet1 的每一列和每一行。我没有对您的代码进行任何更改。你能具体说明你遇到了什么错误吗?

标签: vb.net datatable


【解决方案1】:

所以我编译了你的代码,它似乎可以很好地从 Excel 中提取所有数据。当然,如果您不希望第一行数据成为您的列标题,那么您只需将 HDR 更改为 NO 即可解决该问题。接下来,您要求将其放入数据集中,您的代码已经这样做了。然后很容易将其传输到数据表中,您就拥有了。

现在,您要求将其放入 SQL 中的部分。有几种不同的方法。您可以将所有数据放入 XML 文档中,然后让 SQL 存储过程读取并插入它。接下来,您可以遍历数据表中的每一行并以这种方式添加。我采用了我认为最好的方法,基于 MSDN (MSDN - Bulk Copy Data into SQL)。

告诉我你的情况。

    Public YourDatatableWithExcelData As New System.Data.DataTable

Private Sub btnChoseDoc_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    If FileUpload1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
        Try

            Dim DS As DataSet
            Dim MyCommand As OleDb.OleDbDataAdapter
            Dim MyConnection As OleDb.OleDbConnection

            Dim dtExcelSchema As System.Data.DataTable

            MyConnection = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\dawid\Desktop\Book1.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES"";")

            MyConnection.Open()

            dtExcelSchema = MyConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
            Dim SheetName As String = dtExcelSchema.Rows(0)("TABLE_NAME").ToString()
            MyConnection.Close()

            MyConnection.Open()

            ' Select the data from Sheet1 of the workbook.
            MyCommand = New OleDb.OleDbDataAdapter("Select * from [" & SheetName & "]", MyConnection)
            DS = New System.Data.DataSet()
            MyCommand.Fill(DS)
            grvRecords.DataSource = DS.Tables(0).DefaultView
            YourDatatableWithExcelData = DS.Tables(0)
            MyConnection.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        Finally


        End Try



    End If

End Sub

Private Sub InsertIntoSQL(sender As Object, e As EventArgs) Handles Button2.Click

    Using destinationConnection As SqlConnection = New SqlConnection("Data Source=servername;" & "Initial Catalog=databasename;" & "User ID=username;" & "Password=userpassword;")

        destinationConnection.Open()

        Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(destinationConnection)
            bulkCopy.DestinationTableName = "dbo.YourTableNameToCopyDataTo"
            Try
                bulkCopy.WriteToServer(YourDatatableWithExcelData)
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End Using
    End Using
End Sub

我们已经创建了一个名为YourDatatableWithExcelData 的全局声明数据表,它被分配了数据集的值,然后我们使用 Button2.Click 在 InsertIntoSQL 函数中调用该数据表。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-08
    • 1970-01-01
    • 2011-12-21
    • 1970-01-01
    相关资源
    最近更新 更多