Use ADO and the Jet OLEDB provider, which can read the contents of excel files without actually opening Excel.
Example:
'*******************************
Example:
'*******************************
Public Sub ConvertXLSToTab(ByVal xlsFileName As String, ByVal tabTextOutputFile As String)
Dim connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & xlsFileName & "';Extended Properties='Excel 12.0;HDR=YES';"
Dim strSQL As String = "SELECT * FROM [Sheet1$]"
Dim excelConnection As OleDb.OleDbConnection = New OleDb.OleDbConnection(connectionString)
excelConnection.Open()
Dim dbCommand As OleDbCommand = New OleDbCommand(strSQL, excelConnection)
Dim dataAdapter As OleDbDataAdapter = New OleDbDataAdapter(dbCommand)
Dim ds As New DataSet
dataAdapter.Fill(ds, "dTable")
'DataGridView1.DataSource = ds.Tables("dTable").DefaultView
Dim strLine As String
Dim objStreamWriter As StreamWriter
objStreamWriter = File.CreateText(tabTextOutputFile)
For Each row As DataRow In ds.Tables("dTable").Rows
Dim column As DataColumn
strLine = ""
For Each column In ds.Tables("dTable").Columns
If (strLine <> "") Then strLine = strLine & vbTab
strLine = strLine & row(column).ToString()
Next column
objStreamWriter.WriteLine(strLine)
Next row
excelConnection.Close()
objStreamWriter.Close()
End Sub
Dim connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & xlsFileName & "';Extended Properties='Excel 12.0;HDR=YES';"
Dim strSQL As String = "SELECT * FROM [Sheet1$]"
Dim excelConnection As OleDb.OleDbConnection = New OleDb.OleDbConnection(connectionString)
excelConnection.Open()
Dim dbCommand As OleDbCommand = New OleDbCommand(strSQL, excelConnection)
Dim dataAdapter As OleDbDataAdapter = New OleDbDataAdapter(dbCommand)
Dim ds As New DataSet
dataAdapter.Fill(ds, "dTable")
'DataGridView1.DataSource = ds.Tables("dTable").DefaultView
Dim strLine As String
Dim objStreamWriter As StreamWriter
objStreamWriter = File.CreateText(tabTextOutputFile)
For Each row As DataRow In ds.Tables("dTable").Rows
Dim column As DataColumn
strLine = ""
For Each column In ds.Tables("dTable").Columns
If (strLine <> "") Then strLine = strLine & vbTab
strLine = strLine & row(column).ToString()
Next column
objStreamWriter.WriteLine(strLine)
Next row
excelConnection.Close()
objStreamWriter.Close()
End Sub