2012-04-18 23 views
-1

在我的項目中,我創建了一個CSV文件,但我想更改它的設計。請幫助我:我如何設計一個使用VB的CSV文件?

Private Sub ExportDataToCSV() 
    Dim fileName As String = "CheckRegistrationStatus_" & Format(Now, "yyyyMMddhhmms") & ".csv" 
    HttpContext.Current.Response.Clear() 
    ' Set the response headers to fit our CSV file 
    HttpContext.Current.Response.ContentType = "text/plain" 
    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" & fileName) 
    Using writer As New System.IO.StreamWriter(HttpContext.Current.Response.OutputStream) 
     Dim columnHeader As String = String.Empty 
     For i As Integer = 0 To grd1.Columns.Count - 1 
      columnHeader += grd1.Columns(i).HeaderText & IIf(i < grd1.Columns.Count - 1, ",", "").ToString() 
     Next 
     writer.WriteLine(columnHeader) 
     'writer.WriteLine(AddCSVHeaderRow()) ' Only if you need custom headers to be added 
     ' Add all the data rows 
     For Each row As GridViewRow In grd1.Rows 
      writer.WriteLine(GetCSVLine(row.Cells)) 
     Next 
    End Using 
    ' End the current response. Otherwise, excel will open with the whole page inside. 
    HttpContext.Current.Response.End() 
End Sub 
Private Shared Function GetCSVLine(ByVal cellsToAdd As TableCellCollection) As String 
    Dim line As String = String.Empty 
    Dim isFirst As Boolean = True 
    For Each cell As TableCell In cellsToAdd 
     If Not isFirst Then 
      line += "," 
     End If 
     isFirst = False 
     line += """" & Replace(cell.Text, "&nbsp;", "") & """" 
    Next 
    Return line 
End Function 

正在顯示輸出,如下圖所示。但是我想使標題粗體並擴大列寬。請幫幫我。

enter image description here

回答

2

你不能。 CSV文件格式是純數據格式。它沒有提供設置字體,列寬或與樣式相關的其他任何方法。

此外,我不認爲你的代碼正確處理所有的數據。例如,如果數據中有逗號或雙引號,則需要執行特殊步驟。 Here's some code I published for creating CSV files in C#

+0

感謝的很多喬納森伍德給我的建議! – Neeraj 2012-04-18 06:07:49

相關問題