Home > Software engineering >  VB.net OLEDB connection Excel file
VB.net OLEDB connection Excel file

Time:11-12

 Public Function ExcTansposeAbf (ByVal strCollection As List (Of String), ByVal strTableInExcel As String) As Array 
Dim BRR (0 To 1, 0 To 1) As String
For intIndexInCollection As Integer=0 To strCollection. Count - 1
If Strings. Right (strCollection (intIndexInCollection), 4) Like XLS "* *" Then
Dim cnString As String="provider=Microsoft. ACE. The OLEDB. 12.0. Data Source="& amp; StrCollection (intIndexInCollection) & amp; "; Extended Properties='Excel 12.0; HDR=No. IMEX=0 ';"
Dim cnDataset As New DataSet
Using cnCollection As New OleDbConnection (cnString)
Using cnAdapter As New OleDbDataAdapter (" select * from [" & amp; strTableInExcel & amp; "$]", cnCollection)
Try
CnAdapter. The Fill (cnDataset strTableInExcel)
Catch the ex As System. InvalidOperationException
BRR (0, 0)="Error_Oledb_InvalidOperationException"
Return BRR
Catch the ex As System. Data. The OleDb. OleDbException
The Continue For
Catch the ex As System. The Runtime. InteropServices. SEHException
BRR (0, 0)="Error_Oledb_InteropServices. SEHException
"Return BRR
The Finally
CnCollection. The Dispose ()
CnAdapter. The Dispose ()
End the Try
End Using
End Using
Dim arr (0 To cnDataset. Tables (strTableInExcel) Rows. The Count - 1, 0 To cnDataset. Tables (strTableInExcel). The Columns. The Count - 1) As String
Try
For I As Integer=0 To cnDataset. Tables (strTableInExcel) Rows. The Count - 1
For j As Integer=0 To cnDataset Tables (strTableInExcel). The Columns. The Count - 1
If Not IsDBNull (cnDataset. Tables (strTableInExcel). Rows (I). The Item (j)) Then
Arr (I, j)=cnDataset. Tables (strTableInExcel). Rows (I). The Item (j)
End the If
Next
Next
If Not arr (0, 18) Like "* *" of the People's Republic of China customs declaration forms of import cargo Then arr (0, 0)="Err_Arr_RangeS2Lost
"Return arr
Catch
BRR (0, 0)="Error_Oledb_DataSetToArray"
Return BRR
The Finally
CnDataset. The Dispose ()
End the Try
End the If
Next
BRR (0, 0)="Error_Oledb_CantFindExcel"
Return BRR
End the Function


Parameters one list, 2 it is Sheet name,
Function is used in a pile of Excel file to find the right the Excel file, and translated into an array, for the use of another function, because another function parameter is an array, so it was useless DataTable.
Pray god to improve the code of this pile of looking at let a person afflictive, looked uncomfortable, difficult execution and so on,
  • Related