2012-02-22 10 views
0

に、私は次のコードを使用してExcelにGridViewコントロールを輸出していたGridViewのエクスポートにExcelファイルに情報を追加します。は、ASP.NET C#の

protected void export_OnClick(object sender, EventArgs e) 
{ 

    string style = @"<style> .text { mso-number-format:\@; } </style> "; 
    // Let's hide all unwanted stuffing 
    GridView1.AllowPaging = false; 
    GridView1.AllowSorting = false; 

    // Let's bind data to GridView 
    BindGrid(); 

    //Change the color back to white 
    GridView1.HeaderRow.Style.Add("background-color", "#ffffff"); 


    //Apply color to the header 
    for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++) 
    { 
     GridView1.HeaderRow.Cells[i].Style.Add("background-color", "#e0e0e0"); 
    } 

    // Let's output the Gridview 
    Response.Clear(); 
    Response.ContentType = "application/vnd.xls"; 
    Response.AddHeader("content-disposition", "attachment;filename=" + reportid + ".xls"); 

    StringWriter swriter = new StringWriter(); 
    HtmlTextWriter hwriter = new HtmlTextWriter(swriter); 

    GridView1.RenderControl(hwriter); 

    Response.Write(style); 
    Response.Write(swriter.ToString()); 
    Response.End(); 

} 

今、私は最初の行、日付に名を追加する必要があり&第2行の時間、場合によっては第3行の他の情報。実際のGridViewデータとこの情報の間に行を残したいと思います。これは可能ですか?もしそうなら、あなたはとても親切で、私はこれをどのように達成できるかのいくつかの例を提供しますか?ありがとうございました。

答えて

0

ちょうどそれを追加してください。それぞれの後にキャリッジリターンを追加して、Excelを次の行に進めます(タブを使用して次のセルに進む)。

Response.Write(style); 
Response.Write("Pretty Excel Export File\n"); 
Response.Write("Generated on " + DateTime.Now.ToShortDateString() + "\n\n"); 
Response.Write(swriter.ToString()); 
Response.End(); 
+0

タブにはどうすればよいですか? – jorame

+0

"\ t"はタブを表示します。 – Malk

+0

\ rまたは\ tは機能していません。私が\ rを使うと次の行にはそれ以上は行きませんし、\ tを使うと次のセルに移動しません。 – jorame