【问题标题】:How to select a specific column in an excel file worksheet and then store them into an SQL Server database using vb.net如何在 Excel 文件工作表中选择特定列,然后使用 vb.net 将它们存储到 SQL Server 数据库中
【发布时间】:2012-09-17 09:30:38
【问题描述】:

你好实际上我只是想将特定的列存储到我现有的数据库中(SQL server 2008)我已经搜索了很多存储它们的方法但我真的没有找到它,任何人都可以帮助我!

这是我要实现的代码

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

    Dim xlApp As Excel.Application
    Dim xlWorkBook As Excel.Workbook
    Dim xlWorkSheet As Excel.Worksheet
    Dim range As Excel.Range
    Dim rCnt As Integer
    Dim cCnt As Integer
    Dim Obj As Object

    xlApp = New Excel.Application
    xlWorkBook = xlApp.Workbooks.Open("C:\Downloads\B2012.xls")

    xlWorkSheet = xlWorkBook.ActiveSheet
    range = xlWorkSheet.UsedRange



    xlWorkBook.Close()
    xlApp.Quit()

    releaseObject(xlApp)
    releaseObject(xlWorkBook)
    releaseObject(xlWorkSheet)
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

【问题讨论】:

  • 可能重复[如何将数据从 Excel 导入 SQL Server 2008 ](stackoverflow.com/questions/12404809/…)
  • 不,但另一个使用的存储过程不像这个!我正在尝试不同的方法来解决这个问题,直到现在我没有尝试使用 OLEDB 连接,但我所做的只是得到excel文件中的数据并将它们存储到我的表中,但有一个条件:存储在数据库中的表中的列必须与excel文件中的列相同,而我想要excel文件中的特定列!!跨度>

标签: .net sql-server vb.net excel excel-2007


【解决方案1】:
Public Function GetExcelDataforSQLInsert(ByVal fname As String) As Boolean
    Dim prxls As String = fname ' excel file and path
    Dim ok As Boolean = True
    If Not System.IO.File.Exists(prxls) Then
            MsgBox(prxls.ToString.Trim + " does not exist.")
            Return False
    End If
    Dim xlApplication As New Application
    Dim xlWrkBook As Workbook
    Dim xlWorksheet As Worksheet
    Dim usedRowsCount As Int32
    xlApplication.Visible = False
    xlApplication.DisplayAlerts = False
    xlWrkBook = xlApplication.Workbooks.Open(prxls)
    xlWorksheet = xlWrkBook.Worksheets(1)
    usedRowsCount = xlWorksheet.UsedRange.Rows.Count  ' gets count of rows
    Dim i As Int32 = 0, j As Int32 = 0
    Dim RowNum As Integer = 0 ' for looping thru the worksheet
    Dim ColNum As Integer = 1
    Dim prodcat As String = ""  ' these will be the fields inserted in  sqlserver table
    Dim prodcde As String = ""
    Dim prod As String = ""
    Dim pack As String = ""
    Dim cocde As String = ""
    Dim upc As String = ""
    Dim retList As ArrayList = Nothing  ' I return errors and output params from sql with an arraylist
    i = 1
    Do While i <= usedRowsCount
        j = getRowType(xlWorksheet, i) ' I am only importing certain rows so check these in a function
        If j = 1 Then ' J tells me which cell type so I know whether to get it or not
            prodcat = xlWorksheet.Cells(i, C).value.ToString.Trim  ' cell I want
        ElseIf j = 2 Then
            RowNum += 1
            prodcde = xlWorksheet.Cells(i, A).value.ToString.Trim
            prod = xlWorksheet.Cells(i, C).value.ToString.Trim
            pack = xlWorksheet.Cells(i, F).value.ToString.Trim
            If String.IsNullOrEmpty(xlWorksheet.Cells(i, D).value) Then
                cocde = ""
            Else
                cocde = xlWorksheet.Cells(i, D).value.ToString.Trim
            End If
            If String.IsNullOrEmpty(xlWorksheet.Cells(i, E).value) Then
                upc = ""
            Else
                upc = xlWorksheet.Cells(i, E).value.ToString.Trim
            End If
' just call a routine to do an insert query with the cells I have selected
            retList = InsertTempXLrow(RowNum, ConnectionString, prodcat, prodcde, prod, pack, cocde, upc)
            If CInt(retList.Item(0)) <> 0 Then  ' return value
                Dim str As String = ""
                str = "ProdCat: " + prodcat + ControlChars.NewLine
                str += "Prodcde: " + prodcde + ControlChars.NewLine
                str += "Product: " + prod + ControlChars.NewLine
                str += "ProdPack: " + pack + ControlChars.NewLine
                str += ControlChars.NewLine
                str += ControlChars.NewLine + "Your changes were cancelled."

                MessageBox.Show(str, "Database Insert Error" + Convert.ToString(retList.Item(0)))
                ok = False
                Exit Do
            End If
        End If
        i += 1
    Loop
    xlWrkBook.Close()
    xlApplication.DisplayAlerts = True
    xlApplication.Quit()
    Return ok
End Function

【讨论】:

  • 我无法输入以下内容并保持格式(我第一次尝试格式化)所以:添加到例程顶部:导入 Microsoft.Office.Interop.Excel
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多