Convert XLS to tab delimited.

Use ADO and the Jet OLEDB provider, which can read the contents of excel files without actually opening Excel.

Example:

'*******************************
Public  Sub ConvertXLSToTab( ByVal xlsFileName  As  StringByVal 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

转载于:https://www.cnblogs.com/yangbin990/archive/2006/09/25/514477.html

你可能感兴趣的:(Convert XLS to tab delimited.)