Code to choose the excel file
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "Excel(2007) files (*.xlsx)|*.xlsx|Excel files (*.xls)|*.xls|All files (*.*)|*.*"
OpenFileDialog1.ShowDialog()
lbFilePath.Text = ""
lbFilePath.Text = OpenFileDialog1.FileName
Call ReadExcelFile()
Read Excel File function
Function ReadExcelFile()
Dim da As New OleDbDataAdapter
Dim dt As New DataTable
Dim cmd As New OleDbCommand
Dim xlsConn As OleDbConnection
Dim sPath As String = String.Empty
sPath = Me.lbFilePath.Text
xlsConn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & sPath & ";Extended Properties=Excel 12.0")
Try
xlsConn.Open()
cmd.Connection = xlsConn
cmd.CommandType = CommandType.Text
cmd.CommandText = ("select * from [Sheet1$]")
da.SelectCommand = cmd
da.Fill(dt)
For Each myRow In dt.Rows
'''''ref
TempRefNo = GetNextId("RefId", "AssetRegistration")
Me.TxtRefId.Text = TempRefNo
TempRefNo1 = "AR-" & Format(Val(TempRefNo), "000")
LstastReg.Items.Add(TempRefNo)
LstastReg.Items(LstastReg.Items.Count - 1).SubItems.Add(TempRefNo1)
LstastReg.Items(LstastReg.Items.Count - 1).SubItems.Add(myRow.Item(1))
Me.TxtAstId.Text = GetID(myRow.Item(1), "AssetName", "AssetId", "AssetMaster")
// After SubItems.Add(* The row value to be saved in the excel * )
Next
Catch
MsgBox(ErrorToString)
Me.LstastReg.Items.Clear()
Finally
xlsConn.Close()
xlsConn = Nothing
End Try
Return dt
End Function
0 Comments