У меня есть подпрограмма для получения результатов запроса и записи их в файл xls.Проблема в том, что я получаю только заголовки столбцов и 2 из 3 строк данных.Что я делаю не так, что последняя строка данных не записывается в файл?
Спасибо!
Public Sub DsiplayQueryandConvertoXls(ByVal ReprtID As Integer, ByVal pgid As Integer, ByVal GroupName As String, ByVal outputPath As String)
Dim i As Integer
Dim strLine As String, filePath, fileName, fileExcel, link
Dim objFileStream As FileStream
Dim objStreamWriter As StreamWriter
'Dim nRandom As Random = New Random(DateTime.Now.Millisecond)
Dim fs As Object, myFile As Object
Dim cnn As SqlConnection = New SqlConnection("Data Source=db;Initial Catalog=productionservicereminder;User Id=user;Password=pass;")
'Create a file name.
If ReprtID = 1 Then
fileExcel = GroupName & "ExtWarrantyReport.xls"
End If
'Set a virtual folder to save the file.
'Make sure that you change the application name to match your folder.
If ReprtID = 1 Then
filePath = outputPath
End If
fileName = filePath & fileExcel
'Use FileStream to create the .xls file.
objFileStream = New FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write)
objStreamWriter = New StreamWriter(objFileStream)
'Use a DataReader to connect to the Pubs database.
cnn.Open()
Dim sql3 As String = "select * from table"
Dim cmd As SqlCommand = New SqlCommand(sql3, cnn)
Dim drr As SqlDataReader
cmd.Parameters.Add(New SqlParameter("@pgid", pgid))
drr = cmd.ExecuteReader()
drr.Read()
'Enumerate the field names and records that are used to build the file.
For i = 0 To drr.FieldCount - 1
strLine = strLine & drr.GetName(i).ToString & Chr(9)
Next
'Write the field name information to file.
objStreamWriter.WriteLine(strLine)
'Reinitialize the string for data.
strLine = ""
'Enumerate the database that is used to populate the file.
While drr.Read()
For i = 0 To drr.FieldCount - 1
strLine = strLine & drr.GetValue(i) & Chr(9)
Next
objStreamWriter.WriteLine(strLine)
strLine = ""
End While
'Clean up.
drr.Close()
cnn.Close()
objStreamWriter.Close()
objFileStream.Close()
End Sub