在.NET将dataset输出为.CSV文件的方法...
注意移除','和'回车符'
Imports System.Configuration
Imports System.Data
Imports System.Data.OleDb
Imports Logonexperss.CustomerOnline.DBHelper
Imports System.IO
Public Class excle
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
bind()
End Sub
Private Sub bind()
Dim ds As DataSet
DBHelper.ConnectString = ConfigurationSettings.AppSettings.Item("Database.ConnectionString")
ds = DBHelper.RunSelect("select * from TrialSent")
Dim dt As DataTable = ds.Tables(0)
DataTableToExcel(dt)
'DataGrid1.DataSource = ds
'DataGrid1.DataBind()
End Sub
Public Sub DataTableToExcel(ByVal dt As DataTable)
Try
If dt Is Nothing OrElse dt.Rows.Count < 1 Then
Return
End If
'分隔符
Dim strSplitChar As String = String.Empty
strSplitChar = ","
Dim sw As New System.IO.StringWriter
Dim strTemp As String = String.Empty
Dim num As Integer = dt.Columns.Count - 1
For i As Integer = 0 To num
'输出头
If i <> dt.Columns.Count - 1 Then
strTemp += dt.Columns(i).ColumnName + strSplitChar
Else
strTemp += dt.Columns(i).ColumnName
End If
Next
sw.WriteLine(strTemp)
strTemp = ""
For i As Integer = 0 To dt.Rows.Count - 1
'输出内容 ,注意移除','和'回车符'
For j As Integer = 0 To num
If j <> dt.Columns.Count - 1 Then
strTemp += dt.Rows(i)(j).ToString().Replace(",", "").Replace(Chr(13) & Chr(10), "") + strSplitChar
Else
strTemp += dt.Rows(i)(j).ToString()
End If
Next
sw.WriteLine(strTemp)
strTemp = ""
Next
sw.Close()
Response.AddHeader("Content-Disposition", "attachment; filename=test.csv")
Response.ContentType = "application/ms-excel"
Response.Write(sw)
Response.End()
Catch ex As Exception
Throw ex
End Try
End Sub
End Class
Imports System.Data
Imports System.Data.OleDb
Imports Logonexperss.CustomerOnline.DBHelper
Imports System.IO
Public Class excle
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
bind()
End Sub
Private Sub bind()
Dim ds As DataSet
DBHelper.ConnectString = ConfigurationSettings.AppSettings.Item("Database.ConnectionString")
ds = DBHelper.RunSelect("select * from TrialSent")
Dim dt As DataTable = ds.Tables(0)
DataTableToExcel(dt)
'DataGrid1.DataSource = ds
'DataGrid1.DataBind()
End Sub
Public Sub DataTableToExcel(ByVal dt As DataTable)
Try
If dt Is Nothing OrElse dt.Rows.Count < 1 Then
Return
End If
'分隔符
Dim strSplitChar As String = String.Empty
strSplitChar = ","
Dim sw As New System.IO.StringWriter
Dim strTemp As String = String.Empty
Dim num As Integer = dt.Columns.Count - 1
For i As Integer = 0 To num
'输出头
If i <> dt.Columns.Count - 1 Then
strTemp += dt.Columns(i).ColumnName + strSplitChar
Else
strTemp += dt.Columns(i).ColumnName
End If
Next
sw.WriteLine(strTemp)
strTemp = ""
For i As Integer = 0 To dt.Rows.Count - 1
'输出内容 ,注意移除','和'回车符'
For j As Integer = 0 To num
If j <> dt.Columns.Count - 1 Then
strTemp += dt.Rows(i)(j).ToString().Replace(",", "").Replace(Chr(13) & Chr(10), "") + strSplitChar
Else
strTemp += dt.Rows(i)(j).ToString()
End If
Next
sw.WriteLine(strTemp)
strTemp = ""
Next
sw.Close()
Response.AddHeader("Content-Disposition", "attachment; filename=test.csv")
Response.ContentType = "application/ms-excel"
Response.Write(sw)
Response.End()
Catch ex As Exception
Throw ex
End Try
End Sub
End Class
本文介绍了一种使用Visual Basic .NET将DataSet转换为CSV文件的方法。通过遍历DataSet中的数据并进行适当的格式调整(例如移除逗号和回车符),可以确保数据正确输出到CSV文件中。
696

被折叠的 条评论
为什么被折叠?



