简体   繁体   English

如何在Excel文件工作表中选择特定列,然后使用vb.net将它们存储到SQL Server数据库中

[英]How to select a specific column in an excel file worksheet and then store them into an SQL Server database using vb.net

hello Actually I just want to store just specific columns into my excisting database(SQL server 2008) I have searched a lot of a way to store them but I really didn't find it, can any body help me please! 你好,实际上我只想将特定的列存储到现有数据库(SQL Server 2008)中,我已经搜索了很多存储它们的方法,但我真的没有找到它,请问有什么机构可以帮助我!

Here is my code that I want to implement 这是我要实现的代码

    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
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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何判断是否使用VB.NET打开了某个Excel文件? - How to tell if a certain Excel file is open using VB.NET? 通过TableAdapter vb.net将日期插入SQL Server数据库 - Insert date into SQL Server database via TableAdapter vb.net 使用vb.net在大型excel文件上执行SQL查询的最佳方法是什么? - What's the best way to execute SQL Query on large excel file using vb.net? 使用vb.net在SQL Server数据库中插入数据时出错 - Error while inserting data in the sql server database using vb.net 我正在尝试访问Excel文件以使用ASP.NET和VB.NET将其导出到SQL数据库,但出现以下错误 - I'm trying to access Excel file to export it to SQL database with ASP.NET and VB.NET but getting following error 如何使用vb.net将嵌入式项目资源(Excel工作表)保存到用户的桌面 - How to use vb.net to save an embedded project resource (excel worksheet) to a user's desktop 如何使用vb.net检索具有合并单元格的复杂Excel文件并另存为xml文件? - How to retrieve complex excel file with merged cells and save as xml file using vb.net? 使用vb.net提取html文件中的特定文本 - Pulling out specific text in an html file using vb.net 如何使用 VB.NET 获取 Excel 文件的确切行数? - How to get the exact number of rows of an Excel file with VB.NET? 如何使用VB.Net在Excel的工作表中打开和填充.XML文件? - How do I open and populate a .XML file in a sheet in Excel using VB.Net?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM