안녕하세요. 사용자가 데이터베이스에서 데이터를 다운로드 할 수 있도록 허용하여 데이터베이스에 액세스하지 않고 조작 할 수 있도록 노력하고 있습니다. 지금까지이 파일을 만들었지 만 클라이언트 PC에 다운로드해야합니다. 어떤 팁?데이터 테이블을 CSV 파일로 다운로드
Protected Sub ExportDataTableToCSV(cmdText As String)
Try
Dim sqlcon_Local As New SqlClient.SqlConnection
sqlcon_Local.ConnectionString = ConfigurationManager.ConnectionStrings("Portal").ConnectionString
Dim objCmd As New SqlCommand(cmdText, sqlcon_Local)
Dim da As SqlDataAdapter = New SqlDataAdapter(objCmd)
Dim dt As DataTable = New DataTable
da.Fill(dt)
Dim filename As String = Server.MapPath("~/download.csv")
Dim sw As New StreamWriter(filename, False)
Dim iColCount As Integer = dt.Columns.Count
For i As Integer = 0 To iColCount - 1
sw.Write(dt.Columns(i))
If i < iColCount - 1 Then
sw.Write(",")
End If
Next
sw.Write(sw.NewLine)
For Each dr As DataRow In dt.Rows
For i As Integer = 0 To iColCount - 1
If Not Convert.IsDBNull(dr(i)) Then
sw.Write(dr(i).ToString())
End If
If i < iColCount - 1 Then
sw.Write(",")
End If
Next
sw.Write(sw.NewLine)
Next
sw.Close()
Response.Clear()
Response.ContentType = "application/csv"
Response.AddHeader("Content-Disposition", "attachment; filename=download.csv")
Response.WriteFile(filename)
Response.Flush()
Response.End()
Catch ex As Exception
End Try
End Sub
무엇이 잘못 되었나요? 모든 입력을 환영합니다.
시간 내 주셔서 감사합니다.
이것은 작동하지 않았습니다. 아이디어에 감사드립니다. – Ja77aman