在以下腳本中,我將.xls轉換爲.csv文件。 到目前爲止是這種情況:OleDBDataReader:未給出一個或多個所需參數的值
Const cDelimiter As Char = "~"c 'Delimiter for the new CSV File
Const fileHasHeader As String = "YES"
Dim sSheetName As String = String.Empty
Dim sConnection As String =
String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0;HDR={1};IMEX=1""", fullPathSource, fileHasHeader)
'
Try
Using oleExcelConnection As New OleDb.OleDbConnection(sConnection)
oleExcelConnection.Open()
Using dtTablesList = oleExcelConnection.GetSchema("Tables")
If dtTablesList.Rows.Count > 0 Then
sSheetName = dtTablesList.Rows(0)("TABLE_NAME").ToString
End If
' Check if the data sheet has a worksheet(table)
If sSheetName <> "" Then
Dim oleExcelCommand As OleDb.OleDbCommand = oleExcelConnection.CreateCommand()
oleExcelCommand.CommandText = "Select * From [" & sSheetName & "]"
oleExcelCommand.CommandType = CommandType.Text
Dim oleExcelReader As OleDb.OleDbDataReader = oleExcelCommand.ExecuteReader
Dim dtCols As DataTable = oleExcelConnection.GetOleDbSchemaTable()
For Each elem In dtCols.Columns
Dim string123 As String = (elem.ToString)
Next
'Dim command As New OleDb.OleDbCommand(insertSQL)
'Date.Now.ToString().Replace(":", "-").Replace(".", "-"))
Using objStreamWriter As New IO.StreamWriter(String.Format("{0}.csv", fullPathTarget))
'Row Count not in use so far
Dim countRow As Integer = 0
Dim strToRow As String = Nothing
'Read the XLS(x) file until end
While oleExcelReader.Read()
'Empty String for next run
Dim rowString As String = ""
'Check dynamically for length of rows
For i As Integer = 0 To oleExcelReader.FieldCount - 1 Step 1
'Here would be the place tó remove any unwanted delimiters within the data
'If oleExcelReader.GetValue(i).ToString.Contains(",") Then
'End If
'Append String to write end the end of each row
rowString = rowString & oleExcelReader.GetValue(i).ToString() & cDelimiter
Next
'Write data to file
objStreamWriter.WriteLine(rowString)
countRow += countRow
End While
End Using
'End using will close all open connections
End If
End Using
'End using will close all open connections
End Using
'End using will close all open connections
現在我想添加一個過濾器,刪除空行: 所以我換成這行
oleExcelCommand.CommandText = "Select * From [" & sSheetName & "]"
隨着那個(也正在):
oleExcelCommand.CommandText = "Select * From [" & sSheetName & "] WHERE [APPLICATION] <> ''"
另一個變化(也在工作):
oleExcelCommand.CommandText = "Select * From [" & sSheetName & "] WHERE [APPLICATION] <> '' AND [USERNAME] <> ''"
但這種崩潰:
oleExcelCommand.CommandText = "Select * From [" & sSheetName & "] WHERE [APPLICATION] <> '' OR [USERNAME] <> ''"
爲什麼我不能在這個whereclause使用OR。這不是簡單的SQL?
我會在線上得到異常(「沒有給出一個或多個必需參數的值」)。
Dim oleExcelReader As OleDb.OleDbDataReader = oleExcelCommand.ExecuteReader
是否與剛工作' ... WHERE [USERNAME] <>''「'? – topshot
@topshot是的,它似乎是信號詞OR。我不明白... – Ulpin