2

太字のヘッダー テキストを含む csv ファイルを作成したい.この次のコードは、1 つのヘッダー行を作成しています.しかし、私はこのヘッダーを太字にしたい.

       'Add Response header 

    Response.Clear()
    Response.AddHeader("content-disposition", String.Format("attachment;filename={0}.csv", "check_"))
    Response.Charset = ""
    Response.ContentType = "application/vnd.csv"

    Try
        Dim sb As New StringBuilder()
        'Add Header  dr.GetName(count)     

        For count As Integer = 0 To GridView1.Columns.Count - 1
            If GridView1.Columns(count) IsNot Nothing Then
                sb.Append(GridView1.Columns(count).HeaderText)
            End If
            If count < GridView1.Columns.count - 1 Then
                sb.Append(",")
            End If
        Next
        Response.Write(sb.ToString() + vbLf)
        Response.Flush()

    Catch ex As Exception
        Response.Write(ex.Message)
    End Try
    Response.[End]()
4

2 に答える 2

4

CSV はコンマ (,) で区切られたプレーン テキストです。

それに付属するスタイリングはありません。

于 2012-04-17T06:57:17.960 に答える
1

他の人がすでに述べたように、CSV は書式設定されていない単なるテキストです。

Excel で太字のヘッダーが必要で、ASP.NETGridViewを CSV ファイルにエクスポートしていると想定しているため、他に 2 つのオプションがあります。

  1. の HTML (HTML テーブル) を太字のヘッダーでファイルにレンダリングするGridViewと、Excel はそれを正しく解釈します。
  2. 実際の Excel ファイルを作成します。EPPlusを暖かくお勧めします。ここにサンプルがあります。

最初のアプローチのサンプル:

string attachment = "attachment; filename=Contacts.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End(); 

上記のコードを実行すると、HttpException次のようになります。

タイプ 'GridView' のコントロール 'GridView1' は、runat=server のフォーム タグ内に配置する必要があります。"

このエラーを回避するには、次のコードを追加します。

public override void VerifyRenderingInServerForm(Control control)
{
    // yes, it's correct that this is empty
}
于 2012-04-17T07:27:25.940 に答える