更快地创建 CSV 文件
我通过获取数据表然后循环该数据表并写入 CSV 文件的每一行来创建 CSV 文件。我的数据源通常包含大约 65,000 行。从浏览器下载此过程需要几分钟时间。问题是在本地和开发上它不会花费太长时间,但在客户端上它们通常会超时。
有没有更快的方法来生成这个文件?
Function GenerateCSVFile() As String
Dim stuPro As New studentProvider.StudentProvider
Dim emailCenter As New EmailCenter
Dim strFileName As String = System.IO.Path.GetRandomFileName().Replace(".", "")
Dim strResult As String = ""
Dim dtStudent As Data.DataTable
Dim paymentYear As String = ""
dtStudent = stuPro.generateDataFile()
If dtStudent.Rows.Count > 0 Then
Using sw As New System.IO.StreamWriter(Server.MapPath("Temp/" + strFileName + ".csv"))
Try
Dim lineValue As String = ""
lineValue += "Academic Year, StudentID, SSN, First, Middle, Last"
sw.WriteLine(lineValue)
For i As Integer = 0 To dtStudent.Rows.Count - 1
lineValue = dtStudent.Rows(i)("fy").ToString
lineValue += "," & dtStudent.Rows(i)("uniq_stu_id").ToString
lineValue += "," & dtStudent.Rows(i)("ssn").ToString
lineValue += "," & dtStudent.Rows(i)("fname").ToString
lineValue += "," & dtStudent.Rows(i)("mname").ToString
lineValue += "," & dtStudent.Rows(i)("lname").ToString
sw.WriteLine(lineValue)
Next
Catch ex As Exception
strResult += ex.ToString
Finally
sw.Close()
End Try
End Using
Dim strFriendlyName As String = Date.Now.ToString("MM-dd-yyyy") & ".csv"
If String.IsNullOrEmpty(strResult) Then
Dim fs As System.IO.FileStream = Nothing
fs = System.IO.File.Open(Server.MapPath("Temp/" + strFileName + ".csv"), System.IO.FileMode.Open)
Dim btFile(fs.Length) As Byte
fs.Read(btFile, 0, fs.Length)
fs.Close()
With Response
.AddHeader("Content-disposition", "attachment;filename=" & strFriendlyName)
.ContentType = "application/octet-stream"
.BinaryWrite(btFile)
.End()
End With
End If
Else
strResult = "No records found for specified academic year"
End If
Return strResult
End Function
更新代码
Function GenerateCSVFile() As String
Dim startDate As Date = Date.Now
Dim enddate As Date = Nothing
Dim stuPro As New studentProvider.StudentProvider
Dim emailCenter As New EmailCenter
Dim strFileName As String = System.IO.Path.GetRandomFileName().Replace(".", "")
Dim strResult As String = ""
Dim dtStudent As Data.DataTable
Dim paymentYear As String = ""
dtStudent = stuPro.generateDataFile(Session("VendorID"), txtAcademicYear.Text.Trim)
If dtStudent.Rows.Count > 0 Then
With Response
Dim strFriendlyName As String = Date.Now.ToString("MM-dd-yyyy") & ".csv"
.AddHeader("Content-disposition", "attachment;filename=" & strFriendlyName)
.ContentType = "application/octet-stream"
Dim lineValue As StringBuilder = New StringBuilder
lineValue.Append("Academic Year, StudentID, SSN, First, Middle, Last")
.Write(lineValue.ToString)
For i As Integer = 0 To dtStudent.Rows.Count - 1
lineValue = New StringBuilder
lineValue.Append(dtStudent.Rows(i)("fy").ToString)
lineValue.Append("," & dtStudent.Rows(i)("uniq_stu_id").ToString)
lineValue.Append("," & dtStudent.Rows(i)("ssn").ToString)
lineValue.Append("," & dtStudent.Rows(i)("fname").ToString)
lineValue.Append("," & dtStudent.Rows(i)("mname").ToString)
lineValue.Append("," & dtStudent.Rows(i)("lname").ToString)
.Write(lineValue.ToString)
Next
enddate = Date.Now
MsgBox(DateDiff(DateInterval.Second, startDate, enddate))
.End()
End With
Else
strResult = "No records found for specified academic year"
End If
Return strResult
End Function
I create a CSV file by getting a datatable then looping through that datatable and writing eachline of a CSV file. My data source usually has around 65,000 rows in it. This process takes several minutes to download from the browser. The problem is that locally and on dev it doesn't take too long, but on the client they usually timeout.
Is there a faster way to generate this file?
Function GenerateCSVFile() As String
Dim stuPro As New studentProvider.StudentProvider
Dim emailCenter As New EmailCenter
Dim strFileName As String = System.IO.Path.GetRandomFileName().Replace(".", "")
Dim strResult As String = ""
Dim dtStudent As Data.DataTable
Dim paymentYear As String = ""
dtStudent = stuPro.generateDataFile()
If dtStudent.Rows.Count > 0 Then
Using sw As New System.IO.StreamWriter(Server.MapPath("Temp/" + strFileName + ".csv"))
Try
Dim lineValue As String = ""
lineValue += "Academic Year, StudentID, SSN, First, Middle, Last"
sw.WriteLine(lineValue)
For i As Integer = 0 To dtStudent.Rows.Count - 1
lineValue = dtStudent.Rows(i)("fy").ToString
lineValue += "," & dtStudent.Rows(i)("uniq_stu_id").ToString
lineValue += "," & dtStudent.Rows(i)("ssn").ToString
lineValue += "," & dtStudent.Rows(i)("fname").ToString
lineValue += "," & dtStudent.Rows(i)("mname").ToString
lineValue += "," & dtStudent.Rows(i)("lname").ToString
sw.WriteLine(lineValue)
Next
Catch ex As Exception
strResult += ex.ToString
Finally
sw.Close()
End Try
End Using
Dim strFriendlyName As String = Date.Now.ToString("MM-dd-yyyy") & ".csv"
If String.IsNullOrEmpty(strResult) Then
Dim fs As System.IO.FileStream = Nothing
fs = System.IO.File.Open(Server.MapPath("Temp/" + strFileName + ".csv"), System.IO.FileMode.Open)
Dim btFile(fs.Length) As Byte
fs.Read(btFile, 0, fs.Length)
fs.Close()
With Response
.AddHeader("Content-disposition", "attachment;filename=" & strFriendlyName)
.ContentType = "application/octet-stream"
.BinaryWrite(btFile)
.End()
End With
End If
Else
strResult = "No records found for specified academic year"
End If
Return strResult
End Function
Updated Code
Function GenerateCSVFile() As String
Dim startDate As Date = Date.Now
Dim enddate As Date = Nothing
Dim stuPro As New studentProvider.StudentProvider
Dim emailCenter As New EmailCenter
Dim strFileName As String = System.IO.Path.GetRandomFileName().Replace(".", "")
Dim strResult As String = ""
Dim dtStudent As Data.DataTable
Dim paymentYear As String = ""
dtStudent = stuPro.generateDataFile(Session("VendorID"), txtAcademicYear.Text.Trim)
If dtStudent.Rows.Count > 0 Then
With Response
Dim strFriendlyName As String = Date.Now.ToString("MM-dd-yyyy") & ".csv"
.AddHeader("Content-disposition", "attachment;filename=" & strFriendlyName)
.ContentType = "application/octet-stream"
Dim lineValue As StringBuilder = New StringBuilder
lineValue.Append("Academic Year, StudentID, SSN, First, Middle, Last")
.Write(lineValue.ToString)
For i As Integer = 0 To dtStudent.Rows.Count - 1
lineValue = New StringBuilder
lineValue.Append(dtStudent.Rows(i)("fy").ToString)
lineValue.Append("," & dtStudent.Rows(i)("uniq_stu_id").ToString)
lineValue.Append("," & dtStudent.Rows(i)("ssn").ToString)
lineValue.Append("," & dtStudent.Rows(i)("fname").ToString)
lineValue.Append("," & dtStudent.Rows(i)("mname").ToString)
lineValue.Append("," & dtStudent.Rows(i)("lname").ToString)
.Write(lineValue.ToString)
Next
enddate = Date.Now
MsgBox(DateDiff(DateInterval.Second, startDate, enddate))
.End()
End With
Else
strResult = "No records found for specified academic year"
End If
Return strResult
End Function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您正在写入一个临时文件,读入该文件,然后他们将该文件的内容写入响应中。跳过临时文件并一次一行直接写入响应。这将使浏览器不会认为您的服务器超时,使速度更快,并减少应用程序消耗的内存量。
之后,研究如何对此 Web 请求启用缓存,以便在多个用户在短时间内请求时 ASP.NET 不必重新创建 CSV。
You are writing to a temp file, reading that file in, and they writing the contents of that file into the response. Skip the temp file and write directly to the response one line at a time. That will keep the browser from thinking your server is timing out, make things faster, and reduce the amount of memory consumed by your app.
After that, look into how to enable caching on this web request so that ASP.NET won't have to recreate the CSV if multiple users ask for it in a short period of time.
有一些选项可以加快速度:
String.Join 示例:
There are some options to speed this up:
String.Join example:
您应该使用 StringBuilder 而不是串联。
You should be using StringBuilder instead of concatenation.
除了 @Robert Levy 的建议之外,请注意如何使用字符串变量。在这些行上使用 stringbuilder 会更好:
In addition to @Robert Levy's suggestions, be careful how you use string variables. You would be better served using a stringbuilder on these lines:
您可以研究的一件事是生产者/消费者设计模式。这可以让您做的是让一个(或多个)线程向包含需要写入 csv 文件的数据的队列提供数据,并让另一个线程(或多个)执行实际写入操作。
One thing that you could look into is a producer/consumer design pattern. what this could allow you to do is have one (or more) thread(s) feeding a queue containing the data that needs to be written into the csv file and another thread (or more than one) that does the actual writing.