import excel problem

  • Thread starter Thread starter monemas
  • Start date Start date
M

monemas

Guest
hi

Use this code to import data from Excel
The code works if the file does not have a large number of data

error message after waiting
The CLR has been unable to transition from COM context 0x3caf88 to COM context 0x3cb0f8 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.


Try
Dim openFileDialog1 As System.Windows.Forms.OpenFileDialog
openFileDialog1 = New System.Windows.Forms.OpenFileDialog
With openFileDialog1
.DefaultExt = ".xls"
.AddExtension = True
.Filter = "Excel Worksheets|*.xls; *.xlsx; *.xlsm"
If .ShowDialog = Windows.Forms.DialogResult.OK Then
filenameB = (CType(.FileName, String))
End If
End With
If filenameB <> "" Then

Dim SelectStatement As String = "SELECT * FROM [Sheet1$]"
Using cn As New OleDbConnection With {.ConnectionString = Connection.NoHeaderConnectionString(filenameB)}
Using cmd As New OleDbCommand With {.Connection = cn, .CommandText = SelectStatement}
cn.Open()

Try
dt.Load(cmd.ExecuteReader)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
'DATAG_CLIEN.DataSource = dt
For i = 1 To dt.Rows.Count - 1
DATAG_CLIEN.Rows.Add(New String() {dt.Rows(i).Item(0).ToString, dt.Rows(i).Item(1).ToString})
Next
End Using
End Using
Else
MsgBox("file not found")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try


code

Continue reading...
 
Back
Top