开发者

Missing last row when writing query results to xls file

I have a routine built to take query results and write them to an xls file. The problem is that I am only getting the column headers and 2 of the 3 rows of data. What am I doing wrong that the last row of data is not being written to the file?

Thanks!

   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 c开发者_JS百科hange 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


You need to make sure you call Close on any streams you open. Flush is usefull too.

EDIT:

See the Using statement - it's ensures your IDisposable gets Disposed

Using file =  File.OpenWrite("myfile.txt")

   file.WriteLine("Hello World!")

End Using


Dispose and close the stream so that the last buffer will be written

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜