在.NET将dataset输出为.CSV文件的方法...

在.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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值