-1

私のプロジェクトでは、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

次の図に示すように、出力が表示されています。しかし、ヘッダーを太字にして、列幅を広げたいと思っています。私を助けてください。

ここに画像の説明を入力

4

2 に答える 2

2

それはいけません。CSVファイル形式はデータのみの形式です。フォントや列幅など、スタイルに関連するものを設定する方法はありません。

さらに、あなたのコードがすべてのデータを正しく処理しているとは思いません。たとえば、データ内にコンマがある場合や二重引用符がある場合は、特別な手順が必要です。これは、C#でCSVファイルを作成するために公開したコードです

于 2012-04-18T05:13:02.823 に答える
0

CSV ファイルに加えて、またはその代わりに、書式設定された Excel ドキュメントを作成する場合は、Excel 相互運用ライブラリを参照してください。

http://msdn.microsoft.com/en-us/library/bb386107%28v=vs.90%29.aspx

http://support.microsoft.com/kb/301982

http://msdn.microsoft.com/en-us/library/aa188489%28office.10%29.aspx

于 2012-04-18T10:46:54.767 に答える