[英]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.